PHP code example of fezfez / symfony-console-di

1. Go to this page and download the library: Download fezfez/symfony-console-di library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fezfez / symfony-console-di example snippets



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 !