PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Kashif Abbasi   PHP DI Container   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP DI Container
Resolve dependencies from configuration files
Author: By
Last change:
Date: 7 years ago
Size: 4,517 bytes
 

Contents

Class file image Download

php-di-container

PHP DI Container resolves dependencies for php application using Dependency Injection. And it provides a simple container for all depnedencies as well. It can take dependencies as argument to other classes and resolve them efficiently. Good thing about container is you can specify dependencies in many formats:

PHP

JSON

YAML

INI

PHP DI Container is capable of reading configuration from above four formats and load them as services to the container using lazy loading using PHP Simple Config While using PHP Simple Config can use cache for our configuration as well to boost the performance. Please go through details about PHP Simple Config.

Future plan is to allow simple php container without any config file for small applications.

Note: Currently its only support PHP version 5.5 and above

Build & Code Details: Build Status Scrutinizer Code Quality Code Coverage

Package Details:

Install using Composer

Add following dependency to your composer.json

{
  require: {
     // Add this line to your `composer require` as shown below
     "gr8abbasi/php-di-container":"dev-master"
  }
}

Create a configuration file in the desired formate and use desired reader by default service loader will use php config file reader

use Mcustiel\Config\Drivers\Reader\php\Reader as PhpReader;
use Mcustiel\Config\Drivers\Reader\ini\Reader as IniReader;
use Mcustiel\Config\Drivers\Reader\json\Reader as JsonReader;
use Mcustiel\Config\Drivers\Reader\yaml\Reader as YamlReader;

PHP configuration file looks like something below and you can see it contains others classes as dependencies as well.

PHP Example:

<?php

return [
    'class-a'         => [
        'class'       => 'Tests\\DummyServices\\ClassA',
    ],
    'class-b'         => [
        'class'       => 'Tests\\DummyServices\\ClassB',
        'arguments'   => [
            'class-a'
        ]
    ],
    'class-c'         => [
        'class'       => 'Tests\\DummyServices\\ClassC',
        'arguments'   => [
            'class-b'
        ]
    ],
];

$loader = new ServiceLoader();

$services = $loader->loadServices(
    __DIR__ . "/Fixtures/phpServicesConfig.php",
    new PhpReader()
    );

$container = new Container($services);

// To get services from container
$service = $container->get('foo');

JSON Example:


{
    "services": [
        {
            "id": "class-a",
            "class": "Tests\\DummyServices\\ClassA"
        },
        {
            "id": "class-b",
            "class": "Tests\\DummyServices\\ClassB",
            "arguments": [
                {
                    "id": "class-a"
                }
            ]
        },
        {
            "id": "class-c",
            "class": "Tests\\DummyServices\\ClassC",
            "arguments": [
                {
                    "id": "class-a",
                    "id": "class-b"
                }
            ]
        }
    ]
}


$services = $loader->loadServices(
    __DIR__ . "/Fixtures/jsonServicesConfig.json",
    new JsonReader()
    );

YAML Example:


SERVICES:
  class-a:
    class: Tests\\DummyServices\\ClassA
  class-b:
    class: Tests\\DummyServices\\ClassB
    arguments:
      class: class-a
  class-c:
    class: Tests\\DummyServices\\ClassC
    arguments:
      class: class-a
      class: class-b


$services = $loader->loadServices(
    __DIR__ . "/Fixtures/yamlServicesConfig.yml",
    new YamlReader()
    );

INI Example:


[SERVICES]
class-a.class = Tests\\DummyServices\\ClassA
class-b.class = Tests\\DummyServices\\ClassB
class-b.arguments = class-a
class-c.class = Tests\\DummyServices\\ClassC
class-c.arguments = class-a
class-c.arguments = class-b


$services = $loader->loadServices(
    __DIR__ . "/Fixtures/iniServicesConfig.ini",
    new IniReader()
    );