Download the PHP package phossa2/di without Composer
On this page you can find all versions of the php package phossa2/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package di
Short Description A powerful Container-Interop/PSR-11 implementation of dependency injection library for PHP.
License MIT
Homepage https://github.com/phossa2/di
Informations about the package di
phossa2/di [ABANDONED]
PLEASE USE phoole/di library instead
phossa2/di is a fast and powerful Container-Interop or PSR-11 implementation of dependency injection library for PHP. It builds upon the versatile phossa2/config library and supports container delegation, object scope and more.
It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1, PSR-2, PSR-4, and proposed PSR-5, PSR-11.
Installation
Install via the composer utility.
or add the following lines to your composer.json
Usage
-
With autowiring of classes.
A couple of simple classes as follows,
Instead of creating
MyCacheDriverandMyCacheinstances manually, you may get the both instances automatically using the DI container,With autowiring is turned on by default, the container will look for the
MyCacheclass if no service defined as 'MyCache', and resolves its dependency injection (here, is theMyCacheDriverinstance) automatically when creating theMyCacheinstance. -
With manual service addition using
set()Services can be added to the container manually by using
set()method.A service reference
'${#driver}'used in the constructor arguments here indicating it is thedriverservice instance from the container. -
With configuration from files or array
Container uses a
Phossa2\Config\Interfaces\ConfigInterfaceinstance as its definition resolver for parameter and service definitions. The config instance may either read configs from files or get configs from an array as follows,By default, container related configurations are under the node
diand service definitions are under thedi.servicenode.
Features
-
References in the form of '${reference}' can be used to refer to predefined parameters from the config or services in the container.
Characters of
'$', '{', '}', '.'are not allowed in reference name. Characters of'#', '@'have special meanings, such that should not be part of normal service names.See phossa2/config reference for detail. Parameter references are read from configuration files or can be defined by container method
param()as follows,Service reference in the form of '${#service_id}' can be used to referring a service instance in the container (or in the delegator).
Two reserved service references are '${#container}' and '${#config}'. These two are refering the container instance itself and the config instance it is using. These two can be used just like other service references.
- Using references
References can be used anywhere in the configs or as the arguments for all container methods (except for the paramter
$idof the methods). -
Autowiring is the ability of container instantiating objects and resolving its dependencies automatically by their classname. The base for autowiring is the PHP function parameter type-hinting.
By reflecting on class, constructor and methods, phossa2/di is able to find the right class for the instance (user need to use the classname as the service id) and right classes for its dependencies (type-hinted with the classnames).
If interface name is used for dependency type-hint, users may set up the mapping of interfaces to the right classnames as follows,
Or define mappings in the config node
di.serviceas follows,Autowiring can be turned on/off. Turning off autowiring will enable user to check any defintion errors without automatic loading.
-
Object decorating is to apply decorating changes (executing methods etc.) right after the instantiation of a service instance.
- Decorating methods for individual instance only
By adding
methodssection into thecacheservice definition in the form of[ callableOrMethodName, OptionalArgumentArray ], these methods will be executed right aftercacheinstantiation.callableOrMethodNamehere can be,-
method name of current instance
-
a valid callable
- a psedudo callable with references (after resolving the references, it is a valid callable).
OptionalArgumentArrayhere can be,-
empty
-
array of values or references
- Common decorating methods for all instances
Common methods can be configured in the 'di.common' node to apply to all the instances right after their instantiation. The definition consists of two parts, the first is a tester callable takes current instance and the container as parameters and returns a boolean value. The second part is in the same method format as in the service definition 'methods'.
To skip execution of common methods for one service, define it with
skipas follows, -
According to Interop Container Delegate Lookup, container may register a delegate container (the delegator), and
-
Calls to the
get()method should only return an entry if the entry is part of the container. If the entry is not part of the container, an exception should be thrown (as requested by theContainerInterface). -
Calls to the
has()method should only return true if the entry is part of the container. If the entry is not part of the container, false should be returned. -
If the fetched entry has dependencies, instead of performing the dependency lookup in the container, the lookup is performed on the delegate container (delegator).
- Important By default, the lookup SHOULD be performed on the delegate container only, not on the container itself.
phossa2/di fully supports the delegate feature.
-
-
- Shared or single scope
By default, service instances in the container are shared inside the container. Actually they have the scope of
Container::SCOPE_SHARED. If users want different instance each time, they may either use the methodone()or define the service withContainer::SCOPE_SINGLEscope.Or define the service as
Container::SCOPE_SINGLESet the container's default scope to
Container::SCOPE_SINGLEwill cause eachget()returns a new instance (unless 'scope' is explicitly defined as for this service).- Use your own scope
You may get an instance in your own scope as follows, no matter whatever the default scope or the defined scope for this instance,
Service references can also have scope defined as follows,
- Share instance only in certain object
Sometimes, user may want to share one instance only inside certain object.
In previous code,
Cis not only shared under theA, but also shared among different instances ofA. What if user want to shareConly under theAbut not betweenA?By setting scope of
Cto '#A' as follows, -
Both
Phossa2\Di\ContainerandPhossa2\Di\Delegatorimplements\ArrayAccessinterface.By default
Phossa2\Di\Containeris writable which means user can add new service definitions to the container manually by usingset().Note If container is set readonly, auto wiring is turned off automatically.
To get a readonly container,
APIs
-
get(string $id): objectfrom ContainerInterface
If not found,
Phossa2\Di\Exception\NotFoundExceptionis thrown.$idmay have '@scope' appended. An array can be used as the second parameter for object constructor's arguments.has(string $id): boolfrom ContainerInterface
$idmay have '@scope' appended.one(string $id, array $args = []): objectfrom ExtendedContainerInterface
Get a new instance for this
$id. May provide new arguments for the object constructor.run(mixed $callable, array $args = []): mixedfrom ExtendedContainerInterface
Execute a callable or pseduo callable (with references) with provided arguments (may contain references also)
param(string $name, mixed $value): boolfrom ExtendedContainerInterface
Set a parameter in the container's config tree for later to be used as a reference. Returns a bool value to indicate status.
alias(string $id, object|string $object): boolfrom ExtendedContainerInterface
Aliasing an object as
$idin the container. Difference withset()method is that getting this object will skip the execution of common methods.Returns
trueon success andfalseon failure.auto(bool $flag): $thisfrom AutoWiringInterface
Set container autowiring
ONorOFF.isAuto(): boolfrom AutoWiringInterface
Test container is auto wiring
ONorOFF.resolve(mixed &$data): $thisfrom ReferenceResolveInterface
Recursively resolving references in the
$data.$datamight bestringorarray. Other data type will be untouched.share(bool $flag = true): $thisfrom ScopeInterface
Make container default scope to
Container::SCOPE_SHAREDorContainer::SCOPE_SINGLE.set(string $id, mixed $value): boolfrom WritableInterface
Set a service with
$idinto the container.$valuecan be an array in the form of array as follows,or
$valuecan be classname, object, callback etc. Returnstrueon success orfalseon failure. Container has to be writable, otherwise aLogicExceptionwill be thrown.isWritable(): boolfrom WritableInterface
Is this container writable ?
setWritable(bool $writable): boolfrom WritableInterface
Set this container writable or readonly.
-
get(string $id): objectfrom ContainerInterface
Get service from the delegator. If not found,
Phossa2\Di\Exception\NotFoundExceptionis thrown.$idmay have '@scope' appended as long as underlying container supports this feature.has(string $id): boolfrom ContainerInterface
$idmay have '@scope' appended as long as underlying container supports this feature.set(string $id, mixed $value): boolfrom WritableInterface
Set a service with
$idinto the delegator as long as the delegator is writable.isWritable(): boolfrom WritableInterface
Is this delegator writable ?
setWritable(bool $writable): boolfrom WritableInterface
Set this delegator writable or readonly.
addContainer(ContainerInterface $container): $thisfrom DelegatorInterface
Add a container to the delegator.
Change log
Please see CHANGELOG from more information.
Testing
Contributing
Please see CONTRIBUTE for more information.
Dependencies
-
PHP >= 5.4.0
-
phossa2/config >= 2.0.12
- phossa2/shared >= 2.0.21
License
All versions of di with dependencies
phossa2/shared Version ^2.0.19
phossa2/config Version ^2.0.12
container-interop/container-interop Version ~1.0
