PHP Classes

PHP DI Container: Resolve dependencies from configuration files

Recommend this page to a friend!
  Info   View files Documentation   View files View files (29)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 56 This week: 1All time: 10,529 This week: 571Up
Version License PHP version Categories
php-di-container 1.0MIT/X Consortium ...5.5PHP 5, Language, Design Patterns
Description 

Author

This package can resolve dependencies from configuration files.

It can take a configuration file in the PHP, JSON, INI and YAML format with the definition of service classes.

The package can loads classes according to the requested services.

Picture of Kashif Abbasi
  Performance   Level  
Name: Kashif Abbasi <contact>
Classes: 1 package by
Country: Pakistan Pakistan
Age: ???
All time rank: 447760 in Pakistan Pakistan
Week rank: 420 Up8 in Pakistan Pakistan Up

Documentation

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


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 3 directories)
Files folder imagetests (3 files, 4 directories)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageException (3 files)
Files folder imageFactory (3 files)
Files folder imageRepository (2 files)
  Plain text file Container.php Class Class source
  Plain text file ServiceLoader.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file CircularDependencyException.php Class Class source
  Plain text file ContainerException.php Class Class source
  Plain text file NotFoundException.php Class Class source

  Files folder image Files  /  src  /  Factory  
File Role Description
  Plain text file ConfigFileServiceFactory.php Class Class source
  Plain text file JsonServiceFactory.php Class Class source
  Plain text file ServiceFactoryInterface.php Class Class source

  Files folder image Files  /  src  /  Repository  
File Role Description
  Plain text file InMemoryServiceRepository.php Class Class source
  Plain text file ServiceRepositoryInterface.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageDummyServices (3 files)
Files folder imageFactory (2 files)
Files folder imageFixtures (4 files)
Files folder imageRepository (1 file)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script
  Plain text file ContainerTest.php Class Class source
  Plain text file ServiceLoaderTest.php Class Class source

  Files folder image Files  /  tests  /  DummyServices  
File Role Description
  Plain text file ClassA.php Class Class source
  Plain text file ClassB.php Class Class source
  Plain text file ClassC.php Class Class source

  Files folder image Files  /  tests  /  Factory  
File Role Description
  Plain text file ConfigFileServiceFactoryTest.php Class Class source
  Accessible without login Plain text file services.json Data Auxiliary data

  Files folder image Files  /  tests  /  Fixtures  
File Role Description
  Accessible without login Plain text file iniServicesConfig.ini Data Auxiliary data
  Accessible without login Plain text file jsonServicesConfig.json Data Auxiliary data
  Accessible without login Plain text file phpServicesConfig.php Aux. Auxiliary script
  Accessible without login Plain text file yamlServicesConfig.yml Data Auxiliary data

  Files folder image Files  /  tests  /  Repository  
File Role Description
  Plain text file InMemoryServiceRepositoryTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:56
This week:1
All time:10,529
This week:571Up