2017 © Pedro Peláez
 

library container-reflection

Psr-11 container decorator providing autowiring feature

image

ellipse/container-reflection

Psr-11 container decorator providing autowiring feature

  • Friday, February 23, 2018
  • by pmall
  • Repository
  • 1 Watchers
  • 0 Stars
  • 131 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Reflection container

This package provides a Psr-11 container decorator enabling auto-wiring to any Psr-11 container implementation., (*1)

Require php >= 7.0, (*2)

Installation composer require ellipse/container-reflection, (*3)

Run tests ./vendor/bin/kahlan, (*4)

Getting started

This package provides an Ellipse\Container\ReflectionContainer class which can be used to decorate any Psr-11 container. By default it enables auto wiring for all the existing classes, modifying the behaviors of the original container ->has() and ->get() methods:, (*5)

The ->has() method now returns true for aliases contained in the original container but also when the given alias is an existing class name., (*6)

The ->get() method returns the value contained in the original container when the given alias is defined. Otherwise when the alias is an existing class name then auto wiring is used to return an new instance of this class. It means php reflection feature is used to extract the class constructor parameters and the reflection container ->get() method is called to retrieve a value for all parameters type hinted as a class name. For the other parameters their default values are used. Once a value is retrieved for all the class constructor parameters, a new instance of the class is built using those values. When called multiple times, the same instance of the class is returned like any Psr-11 container would do., (*7)

This auto wiring feature can be restricted to classes implementing a specified list of interfaces., (*8)

<?php

namespace App;

interface SomeInterface
{
    //
}
<?php

namespace App;

class SomeClass implements SomeInterface
{
    //
}
<?php

namespace App;

class SomeOtherClass
{
    public function __construct(SomeInterface $class1, YetSomeOtherClass $class2)
    {
        //
    }
}
<?php

namespace App;

class YetSomeOtherClass
{
    //
}
<?php

use Some\Psr11Container;

use Ellipse\Container\ReflectionContainer;

use App\SomeInterface;
use App\SomeClass;
use App\SomeOtherClass;
use App\YetSomeOtherClass;

// Get an instance of some Psr-11 container.
$container = new Psr11Container;

// Add some definitions into the original container.
$container->set('some.value', function () {

    return 'something';

});

$container->set(SomeInterface::class, function () {

    return new SomeClass;

});

// Decorate the container.
$container = new ReflectionContainer($container);

// Now ->has() returns true for all those aliases:
$container->has('some.value');
$container->has(SomeInterface::class);
$container->has(SomeClass::class);
$container->has(SomeOtherClass::class);
$container->has(YetSomeOtherClass::class);

// ->get() still returns the values contained in the original container:
$container->get('some.value'); // returns 'something'
$container->get(SomeInterface::class); // returns the defined instance of SomeClass

// now ->get() can also build instances of non contained classes.
// Here an instance of SomeOtherClass is build by injecting the contained implementation of SomeInterface and a new instance of YetSomeOtherClass.
$container->get(SomeOtherClass::class);

// On multiple call the same instance is returned.
$someotherclass1 = $container->get(SomeOtherClass::class);
$someotherclass2 = $container->get(SomeOtherClass::class);

$someotherclass1 === $someotherclass2; // true

Restricted auto wiring

The ReflectionContainer class takes an optional array of interface names as second parameter. When this array is not empty, auto wiring is enabled only for classes implementing at least one of those interfaces., (*9)

For exemple when an application has a lot of controllers it can be an exaustive task to register all of them into the container. To allow flexibility without loosing control on how the application services are created, the ReflectionContainer can be set up to allow auto wiring only for classes implementing a ControllerInterface., (*10)

<?php

namespace App\Controllers;

interface ControllerInterface
{
    //
}
<?php

namespace App\Controllers;

use App\SomeDependency;

class SomeController implements ControllerInterface
{
    private $dependency;

    public function __construct(SomeDependency $dependency)
    {
        $this->dependency = $dependency;
    }

    public function index()
    {
        //
    }
}
<?php

namespace App\Controllers;

use App\SomeOtherDependency;

class SomeOtherController implements ControllerInterface
{
    private $dependency;

    public function __construct(SomeOtherDependency $dependency)
    {
        $this->dependency = $dependency;
    }

    public function index()
    {
        //
    }
}
<?php

use Some\Psr11Container;

use Ellipse\Container\ReflectionContainer;

use App\Controllers\ControllerInterface;
use App\Controllers\SomeController;
use App\Controllers\SomeOtherController;
use App\SomeDependency;
use App\SomeOtherDependency;

// Get an instance of some Psr-11 container.
$container = new Psr11Container;

// Register 'App\SomeDependency' into the original container.
$container->set(SomeDependency::class, function () {

    return new SomeDependency;

});

// Allow auto wiring only for classes implementing ContainerInterface.
$container = new ReflectionContainer($container, [ControllerInterface::class]);

// This returns an new instance of SomeController with the defined instance of SomeDependency injected.
$controller = $container->get(SomeController::class);

// This fails because as SomeOtherDependency is not defined in the original container.
$controller = $container->get(SomeOtherController::class);

The Versions

23/02 2018

dev-master

9999999-dev https://github.com/ellipsephp/container-reflection

Psr-11 container decorator providing autowiring feature

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar pmall

container decorator dependency-injection psr-11 autowiring callable-dependency-injection runtime-dependency-injection

23/02 2018

1.0.4

1.0.4.0 https://github.com/ellipsephp/container-reflection

Psr-11 container decorator providing autowiring feature

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar pmall

container decorator dependency-injection psr-11 autowiring

28/01 2018

1.0.3

1.0.3.0 https://github.com/ellipsephp/container-reflection

Psr-11 container decorator providing autowiring feature

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar pmall

container decorator dependency-injection psr-11 autowiring

14/01 2018

1.0.2

1.0.2.0 https://github.com/ellipsephp/container-reflection

Psr-11 container decorator providing autowiring feature

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar pmall

container decorator dependency-injection psr-11 autowiring

19/12 2017

1.0.1

1.0.1.0 https://github.com/ellipsephp/container-reflection

Psr-11 container decorator providing autowiring feature

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar pmall

container decorator dependency-injection psr-11 autowiring

06/12 2017

1.0.0

1.0.0.0 https://github.com/ellipsephp/container-reflection

Psr-11 container decorator providing autowiring feature

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar pmall

container decorator dependency-injection psr-11 autowiring callable-dependency-injection runtime-dependency-injection