Download the PHP package ellipse/container-reflection without Composer
On this page you can find all versions of the php package ellipse/container-reflection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ellipse/container-reflection
More information about ellipse/container-reflection
Files in ellipse/container-reflection
Package container-reflection
Short Description Psr-11 container decorator providing autowiring feature
License MIT
Homepage https://github.com/ellipsephp/container-reflection
Informations about the package container-reflection
Reflection container
This package provides a Psr-11 container decorator enabling auto-wiring to any Psr-11 container implementation.
Require php >= 7.0
Installation composer require ellipse/container-reflection
Run tests ./vendor/bin/kahlan
- Getting started
- Restricted auto wiring
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:
The ->has()
method now returns true for aliases contained in the original container but also when the given alias is an existing class name.
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.
This auto wiring feature can be restricted to classes implementing a specified list of interfaces.
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.
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
.