borodulin / container
PSR Container
dev-master
2020-05-18 10:43 UTC
Requires
- php: ^7.3
- borodulin/finder: ^1.0
- opis/closure: ^3.5
- psr/cache: ^1.0
- psr/container: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.23
- phpunit/phpcov: ^7.0
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2024-10-18 20:28:27 UTC
README
Features
- 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.
Using container
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.”