v0.2.0
0.2.0.0WOK Service provider and dependency injection
MIT
The Requires
by SĂ©bastien ALEXANDRE
WOK Service provider and dependency injection
This library is lightweight dependency injection., (*1)
Diclaimer : This component is part of the WOK (Web Operational Kit) framework. It however can be used as a standalone library., (*3)
It is recommanded to install that component as a dependency using Composer :, (*4)
composer require wok/services
You're also free to get it with git or by direct download while this package has no dependencies., (*5)
git clone https://github.com/web-operational-kit/services.git
While a lot of dependencies injectors have been developed (such as pimple/pimple, or league/container), this library roadmap has some specificities :, (*6)
Let's see this in usage., (*7)
``` php use \WOK\Services\Services; $services = new Services();, (*8)
// Sample with Doctrine/Cache as cache
service.
$services->addService('cache', function() {, (*9)
return new \Doctrine\Common\Cache\FilesystemCache( './var/cache');
});, (*10)
// Far far away ..., (*11)
// Retrieve the service $cache = $services->getService('cache');, (*12)
## With parameters ``` php use \WOK\Services\Services; $services = new Services(); // Sample with Symfony/Translation as `translator` service and the locale code as parameter $services->addService('translator', function($locale) { $translator = new \Symfony\Component\Translation\Translator($locale); $translator->addLoader('ini', new \Symfony\Component\Translation\Loader\IniFileLoader()); $files = new DirectoryIterator($path); foreach($files as $resource) { if($resource->getExtension() != 'ini') { continue; } // Only accepted files as `domain.locale.(ini|yml)` $basename = $resource->getBasename(); $domain = mb_substr($basename, 0, mb_strpos($basename, '.')); $translator->addResource($resource->getExtension(), $resource->getPathname(), $locale, $domain); } return $translator; }); // Far far away ... $translator = $services->getService('translator', array('fr_FR'));
WOK Service provider and dependency injection
MIT