zendframework / zend-pimple-config

Pimple container configurator based on ZF ServiceManager configuration
BSD 3-Clause "New" or "Revised" License
6 stars 3 forks source link
pimple psr-11 zend-framework zend-servicemanager

zend-pimple-config

Repository abandoned 2019-12-31

This repository has moved to laminas/laminas-pimple-config.

Build Status Coverage Status

This library provides utilities to configure a PSR-11 compatible Pimple container using zend-servicemanager configuration, for purposes of usage within Expressive.

Installation

Run the following to install this library:

$ composer require zendframework/zend-pimple-config

Configuration

To get a configured PSR-11 Pimple container, do the following:

<?php
use Zend\Pimple\Config\Config;
use Zend\Pimple\Config\ContainerFactory;

$factory = new ContainerFactory();

$container = $factory(
    new Config([
        'dependencies' => [
            'services'          => [],
            'invokables'        => [],
            'factories'         => [],
            'aliases'           => [],
            'delegators'        => [],
            'extensions'        => [],
            'shared'            => [],
            'shared_by_default' => true,
        ],
        // ... other configuration
    ])
);

The dependencies sub associative array can contain the following keys:

Please note, that the whole configuration is available in the $container on config key:

$config = $container->get('config');

extensions

The extensions configuration is only available with the Pimple container. If you are using Aura.Di or zend-servicemanager, you can use delegators instead. It is recommended to use delegators if you'd like to keep the highest compatibility and might consider changing the container library you use in the future.

An extension factory has the following signature:

use Psr\Container\ContainerInterface;

public function __invoke(
    $service,
    ContainerInterface $container,
    $name
);

The parameters passed to the extension factory are the following:

Here is an example extension factory:

use Psr\Container\ContainerInterface;

class ExtensionFactory
{
    public function __invoke($service, ContainerInterface $container, $name)
    {
        // do something with $service

        return $service;
    }
}

You can also return a different instance from the extension factory:

use Psr\Container\ContainerInterface;

class ExtensionFactory
{
    public function __invoke($service, ContainerInterface $container, $name)
    {
        return new Decorator($service);
    }
}

Please note that when configuring extensions, you must provide a list of extension factories for the service, and not a single extension factory name:

new Config([
    'dependencies' => [
        'invokables' => [
            'my-service' => MyInvokable\Service::class,
        ],
        'extensions' => [
            'my-service' => [
                Extension1Factory::class,
                Extension2Factory::class,
                // ...
            ],
        ],
    ],
]);

Service extensions are called in the same order as defined in the list.

Using with Expressive

Replace contents of config/container.php with the following:

<?php

use Zend\Pimple\Config\Config;
use Zend\Pimple\Config\ContainerFactory;

$config  = require __DIR__ . '/config.php';
$factory = new ContainerFactory();

return $factory(new Config($config));