A contract for ioc-containers, (*1)
Do not use this library as long it is not 1.0., (*2)
This project provides a documentation for the api and the behavior of three different concerns:, (*3)
Lets say you wan't to build a rule-based service-dispatcher (or an http-router like silex), that invoke registered closures only if a certain condition is met:, (*4)
$serviceDispatcher = new ServiceDispatcher(); $serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function () { /* do something every hour */ }); $serviceDispatcher->run();
It would be fun, if I already had the domain-objects I need to work with. This would look like this:, (*5)
$serviceDispatcher = new ServiceDispatcher(); $serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) { /* do something every hour */ $businessObject->doSomething(); }); $serviceDispatcher->run();
$serviceDispatcher
should not be aware of a BusinessObject
directly. But the ServiceDispatcher
may know of an generic way to call a callable
(like a closure
) and resolve those parameters by a component outside of ServiceDispatcher
's scope. How this is archived is not a concern of the ServiceDispatcher
. It just happens somehow., (*6)
The goal could be archived with an Dependency-Injection-Container. There are different ioc-containers out there with quite different interfaces (my current favorite is PHP-DI). So we need a common interface to pass an instance around which is aware of how to instantiate our domain-objects so that we could directly use them:, (*7)
$container = new Container(require 'config/di-cfg.php'); $serviceDispatcher = new ServiceDispatcher($container); $serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) { /* do something every hour */ $businessObject->doSomething(); }); $serviceDispatcher->run();
Now the run-method in the ServiceDispatcher
-implementation could simply look like this:, (*8)
use Ioc\MethodInvoker; class ServiceDispatcher { /** @var MethodInvoker */ private $methodInvoker; /* ... */ /** @param MethodInvoker $methodInvoker */ public function __construct(MethodInvoker $methodInvoker) { $this->methodInvoker = $methodInvoker; } /* ... */ public function run() { foreach($this->registry as $serviceName => $service) { if($service['lockUntil'] > time()) { continue; } try { $this->methodInvoker->invoke($service['fn'], array('serviceName' => $serviceName)); } finally { $service['lockUntil'] = time() + $service['timeout']; } } } }
A InstanceContainer
is mostly useful when in subjection to a di-container only a single instance of an object should be used. This is slightly different to the use of a singleton-pattern since you can have multiple di-containers with different configurations that may inject different implementations for the provided interfaces. For implementation details, look at the phpdoc-blocks., (*9)
A ObjectFactory
is mostly useful in common factories to create entities. For implementation details, look at the phpdoc-blocks., (*10)
Invokes a callable
method, function or closure and resolve the required parameters automatically of not already
provided. For implementation details, look at the phpdoc-blocks., (*11)