phpbench / container
Simple, configurable, service container.
Installs: 6 558 570
Dependents: 4
Suggesters: 0
Security: 0
Stars: 15
Watchers: 4
Forks: 6
Open Issues: 1
Requires
- psr/container: ^1.0|^2.0
- symfony/options-resolver: ^4.2 || ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.52
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-10-30 02:16:05 UTC
README
Simple, extensible dependency injection container with parameters and service tagging. Implements container interop.
Simple usage
$container = new Container(); $container->register('foobar', function (Container $container) { return new \stdClass(); });
Extending and Tagging
Extension classes should be passed as the first argument to the container (the user configuration is the second argumnet).
$container = new Container( [ MyExtension::class ], [ 'foo.bar' => 'my_new_value', ] ); $container->init(); // will trigger loading of the extensions.
class MyExtension implements ExtensionInterface { public function load(Container $container) { $container->register('my_service', function (Container $container) { $service = new MyService( $container->getParameter('foo_bar'), $container->get('some_other_service') ); foreach ($container->getServiceIdsForTag('tag') as $serviceId => $params) { $service->add($container->get($serviceId)); } return $service; }); $container->register('tagged_service', function (Container $container) { return new MyService( $container->getParameter('foo_bar'), $container->get('some_other_service') ); }, [ 'tag' => [ 'param1' => 'foobar' ]); } /** * Return the default parameters for the container. * * @return array */ public function getDefaultConfig() { return [ 'foo_bar' => 'this is foo' ]; } }