fezfez/symfony-console-di

This package is abandoned and no longer maintained. The author suggests using the fezfez/symfony-console-di package instead.
There is no license information available for the latest version (0.1.0) of this package.

Use symfony console with DI

0.1.0 2016-02-09 22:53 UTC

This package is auto-updated.

Last update: 2022-05-16 18:42:38 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

Symfony console with dependency injection capability and lazy loading

Sample

use Fezfez\SymfonyDiConsole\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ACommand implements Command
{
    private string $dependency;

    public function __construct(string $dependency)
    {
        $this->dependency = $dependency;
    }

    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->write($this->dependency . 'hola' . $input->getArgument('hi') . $input->getOption('anoption'));

        return 1;
    }
}
use Fezfez\SymfonyDiConsole\Command;
use Fezfez\SymfonyDiConsole\CommandDefinition;
use Fezfez\SymfonyDiConsole\CommandFactory;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class TestCommandFactory implements CommandFactory
{
    public function createCommand(ContainerInterface $container): Command
    {
        echo 'Not call !';
        return new ACommand('hola');
    }

    public function configure(): CommandDefinition
    {
        echo 'call !';
        $dto = new CommandDefinition('test', 'this is a sample');
        $dto->addArgument(new InputArgument('hi'));
        $dto->addOption(new InputOption('anoption'));

        return $dto;
    }
}
$application = new Application('My app');
$application->add(CommandBuilder::build(TestCommandFactory::class, $container));
$application->run();
// output : call !