bigbit / smart-di
SmartContainer evolved from ODDIN ExampleContainer
v2.1.0
2020-03-31 17:00 UTC
Requires
- php: ^7.4.0
- bigbit/di-bootstrap: ^2.0.0
- bigbit/di-meta: ^1.0
- psr/container: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.11.15
- phpunit/phpunit: ^8.2
- symfony/cache: ^4.4
Suggests
- bigbit/oddin: ~2.0.0
Provides
- psr/container-implementation: 1.0.0
README
SmartContainer evolved from ODDIN example.
Dependencies for php7.4
Installation
composer require bigbit/smart-di
Usage
Creating instance using static createDefault factory method.
use Symfony\Component\Cache\Simple\ArrayCache; /** @var Psr\SimpleCache\CacheInterface */ $cache = new ArrayCache(); $container = SmartContainer::createDefault($cache);
Overriding service definitions
By default, all definitions are discovered automatically.
To force a service factory callable into container, use define method.
$container->define(SomeClass::class, function(ContainerInterface $container) { return new SomeClass( $container->get(SomeDependency::class) ); });
Adding primitive dependencies
In some circumstances, service requires primitive value in constructor.
SomeClass service can look like this.
class SomeClass { public function __construct(SomeService $service, string $primitive, $mixed) { } }
Adding primitive by value or callable.
use BigBIT\SmartDI\Interfaces\SmartContainerInterface; use Psr\Container\ContainerInterface; /** @var SmartContainerInterface $container */ $container->definePrimitive(SomeClass::class, 'primitive', 'someValue'); $container->definePrimitive(SomeClass::class, 'mixed', function(ContainerInterface $container) { return 'anotherValue'; } );