定制 glhd/conveyor-belt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

glhd/conveyor-belt

Composer 安装命令:

composer require glhd/conveyor-belt

包简介

关键字:

README 文档

README

Build Status Latest Stable Release MIT Licensed Follow @cmorrell.com on bsky

Conveyor Belt

Conveyor Belt provides all the underlying mechanics necessary to write artisan commands that process lots of data efficiently.

Quickly process 1000's of records

Screencast of default behavior

Get verbose output when necessary

Screencast of verbose behavior

Step through execution & log operations if needed

Screencast of step behavior

See what data is changed by your commands

Screencast of diff behavior

And so much more

Screencast of help view

Installation

# composer require glhd/conveyor-belt

Usage

To use Conveyor Belt, use one of the conveyor belt traits in your Laravel command:

Databases

  • \Glhd\ConveyorBelt\IteratesIdQuery — use this if your underlying query can be ordered by id (improves performance)
  • \Glhd\ConveyorBelt\IteratesQuery — use this if your query is not ordered by id

Files

  • \Glhd\ConveyorBelt\IteratesSpreadsheet — use this to read CSV or Excel files
  • \Glhd\ConveyorBelt\IteratesJson — use this to read JSON files or JSON API data

Other

  • \Glhd\ConveyorBelt\IteratesEnumerable — use this to work with any generic data source

Configuration

Most commands can be configured by setting public properties on the command itself. For example, if you want to enable exception handling, you would add public $collect_exceptions = true; to your command. Each config option can also be managed by overriding a function (if you need more dynamic control over its value). See the source of each trait to find the appropriate function name.

Common for all commands

  • $collect_exceptions — set to true to have your command continue to run if an exception is triggered (the exception will be printed at the end of command execution)
  • $row_name — set this to customize command output (e.g. if you're operating on User models you could set this to "user")
  • $row_name_plural — the plural of $row_name (usually not necessary, as we use Str::plural for you)

IteratesQuery

  • $chunk_size — the number of database records to load at one time
  • $use_transaction — whether to run the whole command inside a database transaction (can cause locking issues if your command runs for a long time)

IteratesIdQuery

The IteratesIdQuery trait accepts all the options that IteratesQuery does, as well as:

  • $id_column — the name of your ID column (if it is not "id")
  • $id_alias — the alias to your ID column in your query

IteratesSpreadsheet

  • $use_headings — whether to treat the first row of each sheet as headings
  • $preserve_empty_rows — whether empty rows should be included
  • $format_dates — whether date columns should be formatted (typically you don't need this because Conveyor Belt automatically converts date cells to Carbon instances for you)
  • $filename — the file to load (only set if this is not dynamic in any way, which is unusual)
  • $excel_temp_directory — set if you need to customize where temp files are stored
  • $field_delimiter — change this if you need to import non-standard CSV files (e.g. tab-delimited)
  • $field_enclosure — change this if you need to import non-standard CSV files (that don't use the " character)
  • $spreadsheet_encoding — change this if you're dealing with non-UTF-8 data
  • $heading_format — Change this to any Str:: function to change the format of your array keys ("snake" by default)

IteratesJson

  • $filename — the file to load (only set if this is not dynamic in any way, which is unusual)
  • $json_endpoint — the JSON endpoint to query for data (use getJsonEndpoint to set this dynamically)
  • $json_pointer — use this to iterate over nested JSON data (see spec)

Examples

Database Example

class ProcessUnverifiedUsers extends Command
{
  use \Glhd\ConveyorBelt\IteratesIdQuery;
  
  // By setting $collect_exceptions to true, we tell Conveyor Belt to catch
  // and log exceptions for display, rather than aborting execution
  public $collect_exceptions = true;
  
  // First, set up the query for the data that your command will operate on.
  // In this example, we're querying for all users that haven't verified their emails.
  public function query()
  {
    return User::query()
      ->whereNull('email_verified_at')
      ->orderBy('id');
  }
  
  // Then, set up a handler for each row. Our example command is either going to
  // remind users to verify their email (if they signed up recently), or queue
  // a job to prune them from the database.
  public function handleRow(User $user)
  {
    // The `progressMessage()` method updates the progress bar in normal mode,
    // or prints the message in verbose/step mode
    $this->progressMessage("{$user->name} <{$user->email}>");
    
    $days = $user->created_at->diffInDays(now());
    
    // The `progressSubMessage()` method adds additional context. If you're in
    // normal mode, this gets appended to the `progressMessage()`. In verbose or
    // step mode, this gets added as a list item below your `progressMessage()`
    $this->progressSubMessage('Registered '.$days.' '.Str::plural('day', $days).' ago…');
    
    // Sometimes our command trigger exceptions. Conveyor Belt makes it easy
    // to handle them and not have to lose all our progress
    ThirdParty::checkSomethingThatMayFail();
    
    if (1 === $days) {
      $this->progressSubmessage('Sending reminder');
      Mail::send(new EmailVerificationReminderMail($user));
    }
    
    if ($days >= 7) {
      $this->progressSubmessage('Queuing to be pruned');
      PruneUnverifiedUserJob::dispatch($user);
    }
  }
}

File Example

class ProcessSignUpSheet extends Command
{
  use \Glhd\ConveyorBelt\IteratesSpreadsheet;
  
  // Conveyor Belt will automatically pick up a "filename" argument. If one
  // is missing you can set a $filename property or implement the getSpreadsheetFilename method
  protected $signature = 'process:sign-up-sheet {filename}';
  
  public function handleRow($item)
  {
    // $item is an object keyed by the spreadsheet headings in snake_case,
    // so for example, the following CSV:
    //
    // Full Name, Sign Up Date, Email
    // Chris Morrell, 2022-01-02, chris@mailinator.com
    //
    // Will result in a full_name, sign_up_date, and email property
    // on the $item object. You can change from snake case to any other
    // string helper format by setting $heading_format
  }
}

The IteratesJson trait works exactly the same as the IteratesSpreadsheet trait, just with different configuration options.

glhd/conveyor-belt 适用场景与选型建议

glhd/conveyor-belt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 121.63k 次下载、GitHub Stars 达 149, 最近一次更新时间为 2022 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 glhd/conveyor-belt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 glhd/conveyor-belt 我们能提供哪些服务?
定制开发 / 二次开发

基于 glhd/conveyor-belt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 121.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 149
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 149
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-28