fratily / container
v0.7.1
2019-08-18 10:44 UTC
Requires
- php: ^7.2
- fratily/reflection: ^0.4.2
- psr/container: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2025-03-18 22:23:05 UTC
README
This is simple Dependency Injection Container.
Install
$ composer require fratily/container
Usage
use Fratily/Container/Builder/AbstractProvider; use Fratily/Container/Builder/ContainerBuilder; use Fratily/Container/ContainerFactory; class Foo { public function __construct( LoggerInterface $logger, SplQueue $queue, string $prefix ) {/* ... */} public function setCache(CacheInterface $cache): void {/* ... */} public function lock(): void {/* ... */} } class SampleProvider extends AbstractProvider { public function build(ContainerBuilder $builder): void { $builder->injection(Foo::class) ->addArgument("queue", new LazyNew(SplQueue::class)) ->addArgument(3, new LazyGetParameter("parameter.prefix")) ->addSetter("setCache", new LazyGet(CacheInterface::class)) ; $builder->parameter("parameter.prefix") ->setValue("this.is.prefix") ; $builder->service("service.foo") ->setValueByClass(Foo::class) ; $builder->service(LoggerInterface::class) ->setValueByFactory( function () { return new Logger(); } ) ; $builder->service(CacheInterface::class) ->setValueByFactory( function () { return new Cache(); } ) ; } public function modify(Container $container): void { $container->get("service.foo")->lock(); } } $factory = new ContainerFactory(); $factory->add(new SampleProvider()); $container = $factory->create(); $foo = $container->get("service.foo");