Container
Lazy and naive container for the dependency injection.
Require PHP >=7.0., (*1)
Use the flyweight design pattern to store a single instance of injectable classes., (*2)
, (*3)
Installation
Simply add a dependency on mpstyle/container to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on MpStyle Container:, (*4)
{
"require-dev": {
"mpstyle/container": "1.*.*"
}
}
or using console:, (*5)
composer require "mpstyle/container=1.*.*"
Usages
Simple usage of the container:, (*6)
interface Foo extends Injectable {}
class Dummy implements Injectable {}
class Bar implements Foo {
public $dummy;
public function __construct(Dummy $d){ $this->dummy = $d; }
}
$container = new Container();
// add an instance:
$container->addInstance(Foo::class, new Bar());
// or add a definition:
$container->addInstance(Foo::class, Bar::class);
// retrieve an object:
$foo = $container->getInstance(Foo::class);
// $foo is an instance of Bar, and $dummy property of Bar is initialized as an instance of Dummy.
Closure
It's possible to pass a Closure to the container which contains the logic to instantiate an object:, (*7)
UniqueContainer::get()->addClosure( Foo::class, function ( Dummy $d ): Foo
{
return new Bar( $d );
} );
/* @var $serviceB ServiceB */
$foo = UniqueContainer::get()->getInstance( Foo::class );
INI File
It's possible to create a container using a PHP file collecting definitions:, (*8)
definitions.ini:, (*9)
mpstyle\container\dummy\Foo = mpstyle\container\dummy\Bar
In your PHP code:, (*10)
$path = 'definitions.ini';
$container = Container::fromIni($path);
$foo = $container->getInstance(Foo::class);
// $foo is an instance of Bar.
Closure or object are not supported using INI file., (*11)
PHP File
It's possible to create a container using a PHP file collecting configurations:, (*12)
definitions.php:, (*13)
<?php
return [
Foo::class => Bar::class
];
In your PHP code:, (*14)
$path = 'definitions.php';
$container = Container::fromPHP($path);
$this->assertTrue($container->existsKey(Foo::class));
$foo = $container->getInstance(Foo::class);
Singleton instance
Using the wrapper of singleton instance:, (*15)
interface Foo extends Injectable {}
class Dummy implements Injectable {}
class Bar implements Foo {
public $dummy;
public function __construct(Dummy $d){ $this->dummy = $d; }
}
// add an instance:
UniqueContainer::get()->addInstance( Foo::class, new Bar(new Dummy()) );
// or add a definition:
UniqueContainer::get()->addDefinition( Foo::class, Bar::class );
// retrieve an object:
$foo = UniqueContainer::get()->getInstance(Foo::class);
// $foo is an instance of Bar, and $dummy property of Bar is initialized as an instance of Dummy.
Version
- 1.5.1: Removed unused classes.
- 1.5.0: Improved performance and tests.
- 1.4.0: Added support to load definitions from INI file and to load a container from PHP configuration file.
- 1.3.1: Little fixes.
- 1.3.0: Improved performance and stability, deprecated Container#get(string $key) method, use Container#getInstance(string $key) instead.
- 1.2.0: Added Closure support to the container.
- 1.1.0
- 1.0.0