library symfony-console-di
Use symfony console with DI
fezfez/symfony-console-di
Use symfony console with DI
- Wednesday, December 21, 2016
- by fezfez
- Repository
- 3 Watchers
- 3 Stars
- 3,803 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 1 Forks
- 0 Open issues
- 2 Versions
- 0 % Grown
symfony-console-di
, (*1)
Symfony console with dependency injection capability and lazy loading, (*2)
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 !