2017 © Pedro Peláez
 

library container

Lazy and naive container for the dependency injection for PHP 7

image

mpstyle/container

Lazy and naive container for the dependency injection for PHP 7

  • Wednesday, August 16, 2017
  • by MpStyle
  • Repository
  • 0 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Container

Lazy and naive container for the dependency injection. Require PHP >=7.0., (*1)

Use the flyweight design pattern to store a single instance of injectable classes., (*2)

Build Status, (*3)

Installation

Simply add a dependency on mpstyle/container to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on MpStyle Container:, (*4)

{
    "require-dev": {
        "mpstyle/container": "1.*.*"
    }
}

or using console:, (*5)

composer require "mpstyle/container=1.*.*"

Usages

Simple usage of the container:, (*6)


interface Foo extends Injectable {} class Dummy implements Injectable {} class Bar implements Foo { public $dummy; public function __construct(Dummy $d){ $this->dummy = $d; } } $container = new Container(); // add an instance: $container->addInstance(Foo::class, new Bar()); // or add a definition: $container->addInstance(Foo::class, Bar::class); // retrieve an object: $foo = $container->getInstance(Foo::class); // $foo is an instance of Bar, and $dummy property of Bar is initialized as an instance of Dummy.

Closure

It's possible to pass a Closure to the container which contains the logic to instantiate an object:, (*7)

UniqueContainer::get()->addClosure( Foo::class, function ( Dummy $d ): Foo
{
    return new Bar( $d );
} );

/* @var $serviceB ServiceB */
$foo = UniqueContainer::get()->getInstance( Foo::class );

INI File

It's possible to create a container using a PHP file collecting definitions:, (*8)

definitions.ini:, (*9)

mpstyle\container\dummy\Foo = mpstyle\container\dummy\Bar

In your PHP code:, (*10)

$path = 'definitions.ini';
$container = Container::fromIni($path);
$foo = $container->getInstance(Foo::class);

// $foo is an instance of Bar.

Closure or object are not supported using INI file., (*11)

PHP File

It's possible to create a container using a PHP file collecting configurations:, (*12)

definitions.php:, (*13)

<?php

return [
    Foo::class => Bar::class
];

In your PHP code:, (*14)

$path = 'definitions.php';
$container = Container::fromPHP($path);

$this->assertTrue($container->existsKey(Foo::class));

$foo = $container->getInstance(Foo::class);

Singleton instance

Using the wrapper of singleton instance:, (*15)


interface Foo extends Injectable {} class Dummy implements Injectable {} class Bar implements Foo { public $dummy; public function __construct(Dummy $d){ $this->dummy = $d; } } // add an instance: UniqueContainer::get()->addInstance( Foo::class, new Bar(new Dummy()) ); // or add a definition: UniqueContainer::get()->addDefinition( Foo::class, Bar::class ); // retrieve an object: $foo = UniqueContainer::get()->getInstance(Foo::class); // $foo is an instance of Bar, and $dummy property of Bar is initialized as an instance of Dummy.

Version

  • 1.5.1: Removed unused classes.
  • 1.5.0: Improved performance and tests.
  • 1.4.0: Added support to load definitions from INI file and to load a container from PHP configuration file.
  • 1.3.1: Little fixes.
  • 1.3.0: Improved performance and stability, deprecated Container#get(string $key) method, use Container#getInstance(string $key) instead.
  • 1.2.0: Added Closure support to the container.
  • 1.1.0
  • 1.0.0

The Versions

16/08 2017

dev-master

9999999-dev

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

29/12 2016

v1.5.1

1.5.1.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

28/12 2016

v1.5.0

1.5.0.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

27/12 2016

v1.4.0

1.4.0.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

27/12 2016

v1.3.1

1.3.1.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

27/12 2016

v1.3.0

1.3.0.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

26/12 2016

v1.2.1

1.2.1.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

26/12 2016

v1.2.0

1.2.0.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

10/12 2016

v1.1.1

1.1.1.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

10/12 2016

v1.1.0

1.1.0.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires

10/12 2016

v1.0.0

1.0.0.0

Lazy and naive container for the dependency injection for PHP 7

  Sources   Download

LGPL v3

The Development Requires