Extensions¶
PHPBench allows you to create your own extensions, allowing you to register new:
Create a new extension package¶
Note
This is optional if you do not wish to distribute your extension you can skip this step.
Create a new project, for example:
$ composer create-project my-phpbench-extension
Include PHPBench as a dev-depenency:
$ composer require phpbench/phpbench --dev
Create the Dependency Injection Extension¶
First, create a dependency injection container extension:
class AcmeExtension implements ExtensionInterface
{
public function configure(OptionsResolver $resolver): void
{
}
public function load(Container $container): void
{
}
}
Then it can be registered in phpbench.json
:
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"core.extensions": [
"PhpBench\\Examples\\Extension\\AcmeExtension"
]
}
Registering and Retrieving Services¶
You can register new services which will be integrated with PHPBench via _tags_.
For example, to register a new Command: with a configuration parameter:
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 => []
]);
}
}
Note that:
The container is a PSR-11 container. You can get any registered service with
$container->get(<< service ID here >>)
.The parameter name is prefixed with the name of the extension (
acme.
) This will help prevent configuration conflicts.A “tag” is used to integrate the new command with PHPBench.
You can activate and use your extension as follows phpbench.json
:
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"extensions": [
"PhpBench\Examples\Extension\AcmeExtension"
],
"acme.number_of_cats": 8
}