adrianfalleiro / slim-cli-runner
Run command line tasks for Slim PHP
Installs: 72 592
Dependents: 2
Suggesters: 0
Security: 0
Stars: 41
Watchers: 6
Forks: 13
Open Issues: 3
Requires
- php: >=7.1
- php-di/php-di: ^6.0||^7.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- slim/psr7: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-11-19 02:13:50 UTC
README
Create and run command line tasks for the Slim PHP micro-framework
Installation
Installation
composer require adrianfalleiro/slim-cli-runner ^3.1
For Slim 3 support install version 2.6 or lower
Register Middleware
Register the middleware in middleware.php
$app->add(new adrianfalleiro\SlimCliRunner\CliRunner::class);
Define, Register and run your tasks
Task definition
Tasks are classes which extend CliAction
and have a public command()
method.
You can inject dependencies through the class constructor.
use Psr\Http\Message\ResponseInterface as Response; use adrianfalleiro\SlimCliRunner\CliAction; use Psr\Log\LoggerInterface; class ExampleCliAction extends CliAction { public function __construct(LoggerInterface $logger) { $this->logger = $logger; } protected function action(): Response { $arg0 = $this->resolveArg(0); $this->logToConsole("arg 0 is {$arg0}"); return $this->respond(); } }
Tasks registration
Add a new key in your settings.php
definitions file called commands
and list your tasks.
To define a default task (For use when no command name is provided) add a new task with __default
as key
return function (ContainerBuilder $containerBuilder) { // Global Settings Object $containerBuilder->addDefinitions([ 'settings' => [ ... ], 'commands' => [ '__default' => \Namespace\To\Task::class 'SampleTask' => \Namespace\To\Task::class ] ]); };
Run Tasks
There are multiple ways of doing this:
Directly via command line:
php public/index.php SampleTask arg1 arg2 arg3
Via composer:
composer.json
{ /*...*/ "config": { "process-timeout" : 0 }, "scripts": { /*...*/ "cli": "php public/index.php" } }
The command
composer cli SampleTask argument1 argument2 argument3
Examples
An example project can be found in the examples/
folder