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
Requires
- php: >=7.1
- cakephp/cakephp: ^3.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.9
- phpunit/phpunit: ^6.5
- suin/livexample: ^1.2
- suin/php-cs-fixer-rules: ^1.0
This package is auto-updated.
Last update: 2024-10-06 10:00:58 UTC
README
CakePHP Subcommand Injector make it possible that automatical adding Task classes as subcommands of a Shell class.
Features
- Automatically add subcommands to a shell.
- You don't have to write pipe code for each Tasks.
How it works
- Find shell task classes from a specific directory.
- 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.