Slim CLI Runner
Create and run command line tasks for the Slim PHP micro-framework, (*1)
Installation
Installation, (*2)
composer require adrianfalleiro/slim-cli-runner ^3.1
For Slim 3 support install version 2.6 or lower, (*3)
Register Middleware, (*4)
Register the middleware in middleware.php
, (*5)
$app->add(new adrianfalleiro\SlimCliRunner\CliRunner::class);
Define, Register and run your tasks
Task definition, (*6)
Tasks are classes which extend CliAction
and have a public command()
method., (*7)
You can inject dependencies through the class constructor., (*8)
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, (*9)
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, (*10)
return function (ContainerBuilder $containerBuilder) {
// Global Settings Object
$containerBuilder->addDefinitions([
'settings' => [
...
],
'commands' => [
'__default' => \Namespace\To\Task::class
'SampleTask' => \Namespace\To\Task::class
]
]);
};
Run Tasks, (*11)
There are multiple ways of doing this:
Directly via command line:, (*12)
php public/index.php SampleTask arg1 arg2 arg3
Via composer:
composer.json, (*13)
{
/*...*/
"config": {
"process-timeout" : 0
},
"scripts": {
/*...*/
"cli": "php public/index.php"
}
}
The command, (*14)
composer cli SampleTask argument1 argument2 argument3
Examples
An example project can be found in the examples/
folder, (*15)