2017 © Pedro Peláez
 

library ieu_container

ieUtilities - Dependency container

image

stg/ieu_container

ieUtilities - Dependency container

  • Wednesday, June 14, 2017
  • by inceddy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 27 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

ieu\Container

PHP Dependency Injection Container inspired by the AngularJS injector and Pimple\Container, (*1)

Usage

use ieu\Container\Container;

$container = (new Container())
    ->value('factor', 2)
    ->service('multiplier', ['factor', 'Vendor\\Project\\Multiplier'])
    ->factory('double', ['multiplier', function($theMultiplierService){
        return function($number) use ($theMultiplierService) {
            return $multiplierServcie->multiply($number);
        };
    }]);

echo $container['factor']; // 2
echo $container['double'](10); // 20

Injection

Dependencies can be injected into services and factories using a dependency array ['dependencyA', 'dependencyB', $callableOrClassname] where the dependecies will be given to the callable or the class constructor as arguments., (*2)


function some_function($A) { echo "function: $A"; } class Foo { public static function bar($A) { echo "static method: $A"; } public function baz($A) { echo "method: $A"; } } $object = new Foo; // Setup container $container = (new Container) // Add some content ->value('A', 'Some value') ->value('InnerCallbackObject', $object) ->value('InnerCallbackClosure', function($A){ echo "inner: $A"; }) // Case 1: Closure ->factory(['A', function($A) { echo "closure $A"; }]) // Case 2: Function ->factory('Function', ['A', 'some_function']) // Case 3: Static method ->factory('Static1', ['A', [Foo::CLASS, 'bar']]) // Case 4: Static method variant ->factory('Static2', ['A', Foo::CLASS . '::bar']) // Case 5: Public method ->factory('Public', ['A', [$object, 'baz']]) // Case 7: Container internal callback object ->factory('Inner', ['A', ['InnerCallbackObject', 'baz']]) // Case 7: Container internal callback closure ->factory('Inner', ['A', ['InnerCallbackClosure']])

A (slower) way is using the parameter names of the callable or constructor to specify the dependencies. E.g. function($dependencyA, $dependencyB) {...} has the same result as ['dependencyA', 'dependencyB', function($depA, $depB) { /*...*/ }]., (*3)

Note: This does not work with inner callbacks!, (*4)

Constant

Constants can be defined using the self ieu\Container\Container::constant(string $name, mixed $value)-method., (*5)

Note: Constants are available during the provider configation cycle!, (*6)

Values

Values can be defined using the self ieu\Container\Container::value(string $name, mixed $value)-method., (*7)

$container = (new Container)
    ->value('A', 'Value');

Factory

Factorys can be defined using the self ieu\Container\Container::factory(string $name, mixed $factory-method., (*8)

$container = (new Container)
    ->value('DependencyA', 'Value')

    // With dependency hint
    ->factory('Name', ['DependencyA', function($a) {
        echo $a; // Value
    }])

    // Auto resolve
    ->factory('Name', function($DependencyA) {
        echo $DependencyA; // Value
    }]);

Service

Services can be defined using the self ieu\Container\Container::service(string $name, mixed $service)-method. The service-method expects a class name or a dependency array with the class name as last element as argument. E.g. ['dependencyA', 'dependencyB', 'Vendor\\Project\\Service'] or just (slower) 'Vendor\\Project\\Service' where the parameter names of the constructor are used to inject the dependencies., (*9)


class Foo { public function __construct($DependencyA) { echo $DependencyA; // Value } } $container = (new Container) ->value('DependencyA', 'Value') // With dependency hint ->factory('Name', ['DependencyA', Foo::CLASS]) // Auto resolve ->factory('Name', Foo::CLASS);

Decorator

You can use the self ieu\Container\Container::decorator(string $name, mixed $decorator)-method to overload existing dependencies while receiving the original instance as local dependency. Decorators MUST be a factory or a provider., (*10)

$container = (new Container)
    ->factory('SomeName', [function(){
        return 'Hello';
    }])
    ->decorator('SomeName', ['DecoratedInstance', function($org) {
        return $org . 'World';
    }]);

echo $container['SomeName']; // HelloWorld

Provider

A provider can be any object having the public property factory describing the factory as dependency array. A provider can be set using the self ieu\Container\Container::provider(string $name, object $provider)-method., (*11)

Providers can be accessed during configuration process by using their name with Provider suffix as dependency., (*12)

class PrefixerProvider {
    private $prefix = 'Hello';

    public $factory;

    public function __construct()
    {
        $this->factory = ['Name', [$this, 'factory']];
    }

    public function setPrefix(string $prefix) : void
    {
        $this->prefix = $prefix;
    }

    public function factory(string $name) : string
    {
        return sprtinf('%s %s', $this->prefix, $name);
    }
}


$container = (new Container)
    ->factory('Name', [function(){
        return 'Justus';
    }])
    ->provider('PrefixedName', new PrefixerProvider))
    ->config(['PrefixedNameProvider', function($provider) {
        $provider->setPrefix('Goodbye');
    }]);

echo $container['PrefixedName']; // Goodbye Justus

The Versions

14/06 2017

dev-master

9999999-dev

ieUtilities - Dependency container

  Sources   Download

MIT

The Requires

 

by Philipp Steingrebe

14/06 2017

2.0.1

2.0.1.0

ieUtilities - Dependency container

  Sources   Download

MIT

The Requires

 

by Philipp Steingrebe

09/06 2017

2.0.0

2.0.0.0

ieUtilities - Dependency container

  Sources   Download

MIT

by Philipp Steingrebe

16/01 2017

1.0

1.0.0.0

ieUtilities - Dependency container

  Sources   Download

MIT

by Philipp Steingrebe