quanta / container
Minimalist dependency injection container implementing Psr-11
0.6.0
2021-03-17 16:01 UTC
Requires
- php: >=7.4
- psr/container: ^1.0
Requires (Dev)
- eloquent/phony-kahlan: ^4.0
- kahlan/kahlan: ^5.0
- phpstan/phpstan: ^0.12.27
README
This package provides a minimalist dependency injection container implementing Psr-11.
Getting started
Require php >= 7.4
Installation composer require quanta/container
Run tests ./vendor/bin/kahlan
Usage
<?php // instantiation using an associative array of factories // (any iterable value can be used to define factories) $container = Quanta\Container::factories([ SomeService::class => fn ($container) => new SomeService( $container->get(SomeDependency::class), ), SomeDependency::class => fn () => new SomeDependency, 'throwing' => function () { throw new Exception('some exception'); }, ]); // true $container instanceof Psr\Container\ContainerInterface; // true $container->has(SomeService::class); // false $container->has('not.defined'); // new SomeService(new SomeDependency) $container->get(SomeService::class); // true $container->get(SomeService::class) === $container->get(SomeService::class); // No 'not.defined' entry in the container try { $container->get('not.defined'); } catch (Quanta\Container\NotFoundException $e) { echo $e->getMessage() . "\n"; } // The factory producing the 'throwing' container entry has thrown an uncaught exception // some exception try { $container->get('throwing'); } catch (Quanta\Container\ContainerException $e) { echo $e->getMessage() . "\n"; echo $e->getPrevious()->getMessage() . "\n"; }