flotzilla / container
PHP FIG PSR-11 container implementation
2.2.2
2020-03-10 23:57 UTC
Requires
- php: ^7.1
- psr/container: ^1.0
Requires (Dev)
- phpstan/phpstan: ^0.12.11
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-04-11 14:03:47 UTC
README
Container
Dependency injection PHP FIG PSR-11 container implementation
Requirements
php > 7.1
Install
via Composer
$ composer require flotzilla/container
Usage
Init service container with constructor parameters
$container = new \flotzilla\Container\Container( [ 'EmptyTestClassDI' => EmptyTestClass::class, // class without dependencies, init by classname 'TestClassDI' => [TestClass::class, 'message'], // class with constructor string parameter 'TestClassDI2' => function () { // closure, that returns new class instance return new TestClass('test'); }, 'ClosureDI' => function ($x, $y) { // closure, that returns sum result return $x + $y; }, 'TestClassWithDependecyDI' => [TestClassWithDependecy::class, 'TestClassDI'] // class with dependency of another service ] );
Or with setter
use \flotzilla\Container\Container; $container = new Container(); $container->set('LoggerDI', function () { return new Logger();}); $container->set('ClosureDI', function ($x, $y) { return $x + $y;}); $container->set('EmptyTestClassDI', ClassWithoutConstructor::class); $container->set('ClassDIWithDependency', [SomeClass::class, 'message']); $container->set('AnotherClassWithDIDependency', [TestClass::class, 'LoggerDI']);
Get your component in code
$logger = $container->get('LoggerDI');
Get your closure with arguments
$container->set('ClosureDI', function ($x, $y) { return $x + $y;}); $logger = $container->getWithParameters('ClosureDI', [1, 2]); // will return 3
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.