CommandsΒΆ

Commands let you add custom CLI commands to PHPBench. For example we can add a command which prints cats to the console output.

Create a new Symfony Command similar to the following:

namespace PhpBench\Examples\Extension\Command;

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

class CatsCommand extends Command
{
    /**
     * @var int
     */
    private $numberOfCats;

    public function __construct(int $numberOfCats)
    {
        $this->numberOfCats = $numberOfCats;
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('cats');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln(str_repeat('🐈', $this->numberOfCats));

        return 0;
    }
}

Register it with the DI extension:

class AcmeExtension implements ExtensionInterface
{
    private const PARAM_NUMBER_OF_CATS = 'acme.number_of_cats';
    public function configure(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            self::PARAM_NUMBER_OF_CATS => 7
        ]);
    }

    public function load(Container $container): void
    {
        $container->register(CatsCommand::class, function (Container $container) {
            return new CatsCommand($container->getParameter(self::PARAM_NUMBER_OF_CATS));
        }, [
            ConsoleExtension::TAG_CONSOLE_COMMAND => []
        ]);
    }
}

You can then run your command:

$ phpbench cats
🐈🐈🐈🐈🐈🐈🐈