2017 © Pedro Peláez
 

library zend-phpdi-config

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

image

elie29/zend-phpdi-config

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  • Wednesday, July 4, 2018
  • by elie29
  • Repository
  • 2 Watchers
  • 3 Stars
  • 144 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

zend-phpdi-config

Build Status Coverage Status, (*1)

Introduction

zend-phpdi-config acts as a bridge to configure a PSR-11 compatible PHP-DI container using service manager configuration. It can be used with Laminas and Mezzio starting from v6.0.0, (*2)

This library uses autowiring technique, cache compilation and cache definitions as defined in PHP-DI., (*3)

Configuration

Service Manager Configuration, (*4)

To get a configured PSR-11 PHP-DI container, do the following:, (*5)

<?php

use Elie\PHPDI\Config\Config;
use Elie\PHPDI\Config\ContainerFactory;

$factory = new ContainerFactory();

$container = $factory(
    new Config([
        'dependencies' => [
            'services'   => [],
            'invokables' => [],
            'autowires'  => [], // A new key added to support PHP-DI autowire technique
            'factories'  => [],
            'aliases'    => [],
            'delegators' => [],
        ],
        // ... other configuration

        // Enable compilation
        Config::DI_CACHE_PATH => __DIR__, // Folder path

        // Write proxies to file : cf. https://php-di.org/doc/lazy-injection.html
        Config::DI_PROXY_PATH => __DIR__, // Folder path

        // Disable autowire (enabled by default)
        Config::USE_AUTOWIRE => false

        // Enable cache
        Config::ENABLE_CACHE_DEFINITION => false, // boolean, true if APCu is activated
    ])
);

The dependencies sub associative array can contain the following keys:, (*6)

  • services: an associative array that maps a key to a specific service instance or service name.
  • invokables: an associative array that map a key to a constructor-less service; i.e., for services that do not require arguments to the constructor. The key and service name usually are the same; if they are not, the key is treated as an alias. It could also be an array of services.
  • autowires: an array of service with or without a constructor; PHP-DI offers an autowire technique that will scan the code and see what are the parameters needed in the constructors. Any aliases needed should be created in the aliases configuration.
  • factories: an associative array that maps a service name to a factory class name, or any callable. Factory classes must be instantiable without arguments, and callable once instantiated (i.e., implement the __invoke() method).
  • aliases: an associative array that maps an alias to a service name (or another alias).
  • delegators: an associative array that maps service names to lists of delegator factory keys, see the Expressive delegators documentation for more details.

N.B.: The whole configuration -- unless dependencies -- is merged in a config key within the $container:, (*7)

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

CLI command to add a new autowire entry

Configuration image, (*8)

The cli command add-autowires-entry creates the configuration file if it doesn't exist otherwise it adds the entry to the autowires key., (*9)

Example of adding ConsoleHelper to a config.php:, (*10)

./vendor/bin/add-autowires-entry config.php "Laminas\\Stdlib\\ConsoleHelper"
  [DONE] Changes written to config.php
  

Using with Expressive

Replace contents of config/container.php with the following:, (*11)

<?php

declare(strict_types = 1);

use Elie\PHPDI\Config\Config;
use Elie\PHPDI\Config\ContainerFactory;

// Protect variables from global scope
return call_user_func(function () {

    $config = require __DIR__ . '/config.php';

    $factory = new ContainerFactory();

    // Container
    return $factory(new Config($config));
});

Example of a ConfigProvider class

<?php

class ConfigProvider
{

    /**
     * Returns the configuration array
     */
    public function __invoke(): array
    {
        return [
            'dependencies' => $this->getDependencies()
        ];
    }

    /**
     * Returns the container dependencies
     */
    public function getDependencies(): array
    {
        return [
            'autowires' => [
                UserManager::class
            ]
        ];
    }
}

Where UserManager depends on Mailer as follow:, (*12)

class UserManager
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function register($email, $password)
    {
        $this->mailer->mail($email, 'Hello and welcome!');
    }
}

class Mailer
{
    public function mail($recipient, $content)
    {
    }
}

Switching back to another container

To switch back to another container is very easy:, (*13)

  1. Create your factories with __invoke function
  2. Replace autowires key in ConfigProvider by factories key, then for each class name attach its correspondent factory.

PSR 11 and Interop\Container\ContainerInterface

V4.x supports as well Interop\Container\ContainerInterface, (*14)

Migration guides

The Versions

04/07 2018

dev-master

9999999-dev

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

06/06 2018

v3.0.3

3.0.3.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

05/06 2018

v3.0.2

3.0.2.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

05/06 2018

v3.0.1

3.0.1.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

05/06 2018

v3.0.0

3.0.0.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

04/06 2018

V2.0.2

2.0.2.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

02/06 2018

V2.0.1

2.0.1.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

01/06 2018

v2.0.0

2.0.0.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

31/05 2018

v1.0.5

1.0.5.0

PSR-11 PHP-DI autowire container configurator for ZF2, ZF3 and Zend Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

31/05 2018

v1.0.4

1.0.4.0

PSR-11 PHP-DI autowire container configurator for ZF and Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

31/05 2018

v1.0.3

1.0.3.0

PSR-11 PHP-DI autowire container configurator for ZF and Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

31/05 2018

v1.0.2

1.0.2.0

PSR-11 PHP-DI autowire container configurator for ZF and Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

31/05 2018

v1.0.1

1.0.1.0

PSR-11 PHP-DI autowire container configurator for ZF and Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di

31/05 2018

v1.0.0

1.0.0.0

PSR-11 PHP-DI autowire container configurator for ZF and Expressive applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Elie NEHME

container zendframework zf expressive di psr-11 php-di