Hello
For information :
Laminas MVC Skeleton Application
laminas/laminas-cli 1.0.2
symfony/console v4.4.21
I would like to use the dependencies in the class of a command as indicated in the laminas/laminas-cli documentation:
// config/autoload/dependencies.global.php
return [
'dependencies' => [
'factories' => [
Application\Command\HelloWorldCommand::class => Application\Command\HelloWorldCommandFactory::class,
],
],
];
However, these dependencies are never taken:
root@srv:/opt/lamtest# ./vendor/bin/laminas helloworld toto
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Application\Command\HelloWorldCommand::__construct(), 0 passed in /opt/lamtest/vendor/laminas/laminas-cli/src/AbstractContainerCommandLoader.php on line 96 and exactly 1 expected in /opt/lamtest/module/Application/src/Command/HelloWorldCommand.php:20
Stack trace:
#0 /opt/lamtest/vendor/laminas/laminas-cli/src/AbstractContainerCommandLoader.php(96): Application\Command\HelloWorldCommand->__construct()
#1 /opt/lamtest/vendor/laminas/laminas-cli/src/AbstractContainerCommandLoader.php(60): Laminas\Cli\AbstractContainerCommandLoader->createCommand()
#2 /opt/lamtest/vendor/laminas/laminas-cli/src/ContainerCommandLoaderNoTypeHint.php(25): Laminas\Cli\AbstractContainerCommandLoader->getCommand()
#3 /opt/lamtest/vendor/symfony/console/Application.php(548): Laminas\Cli\ContainerCommandLoaderNoTypeHint->get()
#4 /opt/lamtest/vendor/symfony/console/Application.php(641): Symfony\Component\Console\Application->has()
#5 /opt/lamtest/vendor/symfony/co in /opt/lamtest/module/Application/src/Command/HelloWorldCommand.php on line 20
I have no idea what to do to fix this.
Can you please help me?
Thank you so much.
========================================================================
My files :
> // config/autoload/global.php
> return [
> 'lstSrv' => ['11', '22', '33'],
> 'laminas-cli' => [
> 'commands' => [
> 'helloworld' => Application\Command\HelloWorldCommand::class,
> ],
> ],
> ];
// module/Apllication/src/Command/HelloWorlCommandFactory.php
namespace Application\Command;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Application\Command\HelloWorldCommand;
class HelloWorldCommandFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('Config');
$lstSrv = $config['lstSrv'];
return new HelloWorldCommand($lstSrv);
}
}
// module/Apllication/src/Command/HelloWorlCommand.php
namespace Application\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
final class HelloWorldCommand extends Command
{
private $lst = 'nok';
public function __construct($lst)
{
$this->lst = $lst[0];
}
protected function configure()
{
$this->addArgument('message', InputArgument::REQUIRED, 'Greeting Message');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$message = $input->getArgument('message');
$output->writeln(sprintf('<info>Hello to world (' . $this->lst . ') : %s<info>! ', $message));
return 0;
}
}