wok/services

WOK Service provider and dependency injection

v1.1.1 2017-03-21 15:54 UTC

This package is auto-updated.

Last update: 2024-04-25 05:48:22 UTC


README

This library is lightweight dependency injection.

SensioLabsInsight

Diclaimer : This component is part of the WOK (Web Operational Kit) framework. It however can be used as a standalone library.

Install

It is recommanded to install that component as a dependency using Composer :

composer require wok/services

You're also free to get it with git or by direct download while this package has no dependencies.

git clone https://github.com/web-operational-kit/services.git

Features

While a lot of dependencies injectors have been developed (such as pimple/pimple, or league/container), this library roadmap has some specificities :

  • The Services component is a dependency injector (usual).
  • Depencies instances MUST be cached throughout the Services object life (that's the dependency injection pattern).
  • Dependencies instances constructors CAN accept parameters without having any trouble with the previous points.

Let's see this in usage.

Usage

Basic usage

use \WOK\Services\Services;
$services = new Services();

// Sample with Doctrine/Cache as `cache` service.
$services->addService('cache', function() {

    return new \Doctrine\Common\Cache\FilesystemCache(
        './var/cache');

});


// Far far away ...


// Retrieve the service
$cache = $services->getService('cache');

With parameters

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'));