2017 © Pedro Peláez
 

library php-di-container

Simple Dependency Injection Container/Library for PHP

image

gr8abbasi/php-di-container

Simple Dependency Injection Container/Library for PHP

  • Saturday, March 11, 2017
  • by gr8abbasi
  • Repository
  • 2 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

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:, (*1)

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., (*2)

Future plan is to allow simple php container without any config file for small applications., (*3)

Note: Currently its only support PHP version 5.5 and above, (*4)

Build & Code Details: Build Status Scrutinizer Code Quality Code Coverage, (*5)

Package Details:, (*6)

Install using Composer

Add following dependency to your composer.json, (*7)

{
  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, (*8)

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., (*9)

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

The Versions

11/03 2017

dev-master

9999999-dev https://github.com/gr8abbasi/php-di-container

Simple Dependency Injection Container/Library for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Muhammad Kashif Abbasi

06/11 2016

dev-master-backup

dev-master-backup https://github.com/gr8abbasi/php-di-container

Simple Dependency Injection Container/Library for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Muhammad Kashif Abbasi

05/11 2016

1.0

1.0.0.0 https://github.com/gr8abbasi/php-di-container

Simple Dependency Injection Container/Library for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Muhammad Kashif Abbasi