suin/cakephp-subcommand-injector

CakePHP Subcommand Injector make it possible that automatical adding Task classes as subcommands of a Shell class.

1.0.2 2018-05-02 16:10 UTC

This package is auto-updated.

Last update: 2024-11-06 10:08:26 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 make it possible that automatical adding Task classes as subcommands of a Shell class.

Features

  1. Automatically add subcommands to a shell.
  2. You don't have to write pipe code for each Tasks.

How it works

  1. Find shell task classes from a specific directory.
  2. Automatically add them to a shell as subcommands.

Installation

$ composer require suin/cakephp-subcommand-injector

Examples

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);
    }
}

See more examples, visit ./example folder.

Changelog

Please see CHANGELOG for more details.

Contributing

Please see CONTRIBUTING for more details.