All Projects → elie29 → zend-di-config

elie29 / zend-di-config

Licence: MIT license
PSR-11 PHP-DI container configurator for Laminas, Mezzio, ZF, Expressive applications or any framework that needs a PSR-11 container

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to zend-di-config

ErrorHeroModule
💎 A Hero for your Zend Framework/Laminas, and Expressive/Mezzio application to log ( DB and Mail ) and handle php errors & exceptions during Mvc process/between request and response
Stars: ✭ 47 (+147.37%)
Mutual labels:  zend-expressive, laminas, mezzio
laminas-dependency-plugin
Replace zendframework and zfcampus packages with their Laminas Project equivalents.
Stars: ✭ 32 (+68.42%)
Mutual labels:  laminas, mezzio
SlmMail
Send mail from Laminas or Mezzio using external mail services.
Stars: ✭ 107 (+463.16%)
Mutual labels:  laminas, mezzio
ApigilityConsumer
🍃 Zend Framework/Laminas and Expressive/Mezzio Apigility/Laminas API Tools Client Module to consume API Services
Stars: ✭ 15 (-21.05%)
Mutual labels:  laminas, mezzio
zf-dependency-injection
Advanced dependency injection for laminas framework
Stars: ✭ 17 (-10.53%)
Mutual labels:  zend-framework, laminas
twbs-helper-module
Laminas (formerly Zend Framework) module for easy integration of Twitter Bootstrap
Stars: ✭ 18 (-5.26%)
Mutual labels:  zend-framework, laminas
SlmQueue
Laminas / Mezzio module that integrates with various queue management systems.
Stars: ✭ 135 (+610.53%)
Mutual labels:  laminas, mezzio
stefano-tree
Framework agnostic Nested Set (MPTT) implementation for PHP
Stars: ✭ 24 (+26.32%)
Mutual labels:  zend-framework, laminas
petstore
A simple skeleton to build api's based on the chubbyphp-framework, mezzio (former zend-expressive) or slim.
Stars: ✭ 34 (+78.95%)
Mutual labels:  zend-expressive, mezzio
zend-problem-details
Provides Problem Details for HTTP APIs (RFC 7807) support for PSR-7 applications.
Stars: ✭ 50 (+163.16%)
Mutual labels:  zend-expressive, zend-framework
OpenRegistry
OpenRegistry - A decentralised container registry fully compliant with OCI Distribution Specification
Stars: ✭ 33 (+73.68%)
Mutual labels:  container
hostnic-cni
hostnic-cni is a Container Network Interface plugin.
Stars: ✭ 25 (+31.58%)
Mutual labels:  container
lede-dockerbuilder
A (container based) LEDE/OpenWrt image builder.
Stars: ✭ 53 (+178.95%)
Mutual labels:  container
heroku-stack-container-repo
After deploying this repo your Heroku app will have stack as container. After that you just have to deploy your own original app.
Stars: ✭ 43 (+126.32%)
Mutual labels:  container
manager
The API endpoint that manages nebula orchestrator clusters
Stars: ✭ 28 (+47.37%)
Mutual labels:  container
learnzf2
Learn ZF2: Learning Zend Framework 2 by Example
Stars: ✭ 35 (+84.21%)
Mutual labels:  zend-framework
publishing-api
API to publish content on GOV.UK
Stars: ✭ 29 (+52.63%)
Mutual labels:  container
emrah-buster-templates
The templates of the emrah-buster installer.
Stars: ✭ 57 (+200%)
Mutual labels:  container
hyper.sh-connector-k8s
Hyper.sh Container Connector for Kubernetes
Stars: ✭ 22 (+15.79%)
Mutual labels:  container
ubuntu-vnc-xfce-firefox
Retired. Headless Ubuntu/Xfce containers with VNC/noVNC and Firefox (Generation 1)
Stars: ✭ 20 (+5.26%)
Mutual labels:  container

zend-phpdi-config

Build Status Coverage Status

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

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

Configuration

Service Manager Configuration

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

<?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:

  • 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:

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

CLI command to add a new autowire entry

Configuration image

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

Example of adding ConsoleHelper to a config.php:

./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:

<?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:

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:

  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

Migration guides

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].