fezfez / symfony-console-di
Use symfony console with DI
Installs: 3 814
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- symfony/console: ^2.8|^3.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is auto-updated.
Last update: 2022-05-16 18:42:38 UTC
README
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 !