- PSR-11 full featured implementation.
- Allows autowire via file finder and/or configuration file.
- Allows delegate lookup and resolve dependencies from other containers.
- Allows scalar types injection via parameters bag.
- Detects circular references.
- Supports aliasing.
- Supports closures.
- Supports PSR-16 cache.
Container can be initialized with file finder.
// autowire all classes in Samples directory
$fileFinder = (new \Borodulin\Finder\ClassFinder())
->addPath(__DIR__.'/../Samples');
// build container
$container = (new \Borodulin\Container\ContainerBuilder())
->setClassFinder($fileFinder)
->build();
Container can be initialized via configuration file.
./config/definitions.php
:
<?php
return [
'test.bar' => Bar::class,
'test.foo' => function (Bar $bar) {
return new Foo($bar);
},
];
// build container
$container = (new \Borodulin\Container\ContainerBuilder())
->setConfig(require(__DIR__ . '/config/definitions.php'))
->build();
After the container is built, objects can be obtained via get()
:
$object = $container->get('test.foo');
“users SHOULD NOT pass a container into an object, so the object can retrieve its own dependencies. Users doing so are using the container as a Service Locator. Service Locator usage is generally discouraged.”