dev-master
9999999-dev https://github.com/Webapper/silex-d3iD3I (Dynamic Decorator Dependency Injection) is a replacement of Pimple
WTFPL
The Requires
- php >=5.4.0
by Csaba Dobai (Assarte)
dependency injection container
D3I (Dynamic Decorator Dependency Injection) is a replacement of Pimple
D3I (Dynamic Decorator Dependency Injection) is a replacement of Pimple which I suggest to use for Silex-alike frameworks., (*1)
The Container objects are for keeping services and other values (eg. configuration setting values). You can access
for all values by using property access (arrow-style: ->key
) and array item access (brackets-style: [key]
)., (*2)
When you accessing an item as a property and its a Provider, the provider will be called and you will give its service:, (*3)
$container = new Container(); $container->my_service = Provider::Create(function(Container $c) {...return $service;})->share(); // registering a service $service = $container->my_service; // getting the unique instance of the "my_service" service
This is for encapsulating a service to be registered. You can get, share, protect, or extend a service by using this class., (*4)
Getting a service:, (*5)
$service = $provider($container);
Sharing a service:, (*6)
$provider->share();
Protecting a service will always returning a new instance of a service when getting from container:, (*7)
$provider->protect();
Extending a service:, (*8)
$extendedProvider = $provider->extend(function(Container $c, $s) { // ...using of parent container $foo = $c['foo'] + 1; // ...using of parent service $s->setFoo($foo); return $s; });
...or using a provider extension:, (*9)
$container->my_extension = Provider::Create(function(Container $c, $s=null) { // checking whether if extending a service or not: if ($s === null) return new Service(); // returning a simple service rather than extending it // ...using of parent container $foo = $c['foo'] + 1; // ...using of parent service $s->setFoo($foo); return $s; // returns the extended service })->share(); ... $extendedProvider = $container['my_service']->extend($container['my_extension']);
This last way of extending is useful when you want to use an extension as a standalone service too., (*10)
D3I uses a special accessing style, let we call as D3I queries. You can use it by both property and array access but we suggest to use it only by array access., (*11)
By D3I queries you can access to sub-items by using a single-line query as a container key, without the need of accessing it by level to level. Useful when you want to refer to services or settings in a setting and want to access to the referred item quickly., (*12)
For example, let we have a configuration setting, creating a service based on the setting, and accessing a setting by using a D3I query:, (*13)
$container = Container::Create([ 'my_service' => [ 'settings' => [ 'welcome_str' => 'Hello %s!' ] ] ]); class MyService { public $settings; function __construct($settings) { $this->settings = $settings; } function getWelcome($to = 'World') { // getting the welcome_str property by querying service return sprintf($container['@my_service.settings.welcome_str'], $to); } } $container->my_service = Provider::Create(function(Container $c) { // getting original settings by accessing the mutated original value of this provider return new MyService($c['!my_service.settings']); }) ->mutate($container['my_service']) // mutate the original value before of overlapping it ->share(); echo $container->my_service->getWelcome(); // -> "Hello World!"
You can also use "#" (hash mark) for annotate an integer key of an array., (*14)
Please, feel free to contribute!, (*15)
Choosed licence for this project is , therefore see it to know the conditions. Thank you!, (*16)
D3I (Dynamic Decorator Dependency Injection) is a replacement of Pimple
WTFPL
dependency injection container