I’m with xerkus here. I’ve written different “single file” scripts, but in the end symfony/console
is the best if your cli scripts evolve. And it’s amazingly easy to setup. What I’ve done is to let the ServiceManager create the Symfony console app, so my CLI entry point (at bin/console
) looks like this:
#!/usr/bin/env php
<?php
declare(strict_types=1);
use Laminas\Mvc\Application as ZfApp;
use Symfony\Component\Console\Application as ConsoleApp;
chdir(dirname(__DIR__));
require dirname(__DIR__) . '/vendor/autoload.php';
$zfApp = ZfApp::init(require dirname(__DIR__) . '/config/application.config.php');
/** @var ConsoleApp $consoleApp */
$consoleApp = $zfApp->getServiceManager()->get(ConsoleApp::class);
return $consoleApp->run();
As you can see, I build the Laminas App (called ZfApp
here, it’s pre-Laminas), get the ServiceManager from there and the Symfony console from the ServiceManager.
The Service Factory looks like this
<?php
declare(strict_types=1);
namespace Eventjet\Factory;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
final class ConsoleApplicationFactory
{
public function __invoke(ContainerInterface $container): Application
{
$app = new Application(
'Eventjet Console Legacy System',
'1.0'
);
foreach ($this->createCommands($container) as $command) {
$app->add($command);
}
return $app;
}
/**
* @return Command[]
*/
private function createCommands(ContainerInterface $container): array
{
$commandNames = $this->config($container)['ej-console']['commands'];
return array_map(
static function (string $commandName) use ($container) {
return $container->get($commandName);
},
$commandNames
);
}
/**
* @return mixed[]
*/
private function config(ContainerInterface $container): array
{
return $container->get('config');
}
}
That way, I can just create a new Console Class, register as class-string in the config under the [‘ej-console’][‘commands’] config key and have it availabe right away
The config would look something like that:
return [
'ej-console' => [
'commands' => [
SomeCommand::class,
],
],
];
Hope that helps