https://github.com/phpbench/container
Simple, extensible and configurable dependency injection container.
https://github.com/phpbench/container
Last synced: 9 months ago
JSON representation
Simple, extensible and configurable dependency injection container.
- Host: GitHub
- URL: https://github.com/phpbench/container
- Owner: phpbench
- License: mit
- Created: 2016-04-06T13:07:18.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-10-30T13:41:02.000Z (about 2 years ago)
- Last Synced: 2025-04-25T15:58:59.863Z (9 months ago)
- Language: PHP
- Size: 26.4 KB
- Stars: 15
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PHPBench Service Container
==========================
[](https://travis-ci.org/phpbench/container)
Simple, extensible dependency injection container with parameters and service
tagging. Implements [container
interop](https://github.com/container-interop/container-interop).
Simple usage
------------
```php
$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).
```php
$container = new Container(
[
MyExtension::class
],
[
'foo.bar' => 'my_new_value',
]
);
$container->init(); // will trigger loading of the extensions.
```
```php
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'
];
}
}
```