suin/cakephp-subcommand-injector

CakePHP Subcommand Injector 允许自动将 Task 类添加为 Shell 类的子命令。

1.0.2 2018-05-02 16:10 UTC

This package is auto-updated.

Last update: 2024-09-06 09:45:41 UTC


README

travis-ci-badge packagist-dt-badge license-badge release-version-badge code-climate-maintainability-badge code-climate-test-coverage-badge php-version-badge

CakePHP Subcommand Injector 允许自动将 Task 类添加为 Shell 类的子命令。

功能

  1. 自动将子命令添加到 shell。
  2. 无需为每个 Task 编写管道代码。

工作原理

  1. 从特定目录查找 shell 任务类。
  2. 自动将它们添加到 shell 作为子命令。

安装

$ composer require suin/cakephp-subcommand-injector

示例

class ExampleShell extends \Cake\Console\Shell
{
    /**
     * @var SubcommandInjector
     */
    private $subcommandInjector;

    public function __construct(
        \Cake\Console\ConsoleIo $io = null,
        \Cake\ORM\Locator\LocatorInterface $locator = null
    )
    {
        parent::__construct($io, $locator);

        // 1. Initialize subcommand injector as a member of Shell class
        $this->subcommandInjector = SubcommandInjector::build(
            // Define how you find task classes:
            new TaskClassesConformingToPsr4(
                __DIR__ . '/my-app/src/',
                'MyApp\\',
                __DIR__ . '/my-app/src/*/*Task.php'
            ),
            // Define mapping rules between task class and subcommand name:
            new NamingRuleByPatternMatching(
                'MyApp\\{module_name}\\{task_name}Task',
                '{module_name}:{task_name}'
            )
        );
    }

    public function initialize()
    {
        // 2. Add tasks to this shell
        $this->subcommandInjector->addTasksTo($this);
        parent::initialize();
    }

    public function getOptionParser()
    {
        $parser = parent::getOptionParser();
        // 3. Add subcommands to this shell's parser
        return $this->subcommandInjector->addSubcommandsTo($this, $parser);
    }
}

查看更多示例,请访问 ./example 文件夹。

变更日志

请参阅 CHANGELOG 了解更多详细信息。

贡献

请参阅 CONTRIBUTING 了解更多详细信息。