Download the PHP package thamtech/yii2-di without Composer
On this page you can find all versions of the php package thamtech/yii2-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thamtech/yii2-di
More information about thamtech/yii2-di
Files in thamtech/yii2-di
Package yii2-di
Short Description Yii2 dependency-injection enhancements
License BSD-3-Clause
Informations about the package yii2-di
Yii2 Dependency Injection Enhancements
Yii2-di provides some enhancements to Yii2's dependency injection framework.
Installation
The preferred way to install this extension is through composer.
or add
to the require
section of your composer.json
file.
Usage
Instance
The Instance::ensureAny()
method can be used in place of
Instance::ensure()
if you would like to ensure that an object is one of any number of
specified types.
For example,
Instance Provider
The InstanceProvider
class adds support for using providers in your dependency
injection implementation. This can be used for several purposes, such as
lazy or optional retrieval of an instance or as a marker for certain
instance types.
Lazy/Optional Retrieval
You may want to inject an object into a class, but the class may not need
the object instantiated yet (or ever). In this case, you can inject a provider
instead and let your class decide if and when to get()
the object instance.
For example,
Provider as Marker Class
Java developers might be used to using annotations on abstractly typed parameters to specify particular versions of the instance.
For example,
To accomplish something similar with Yii's dependency injection, you can
create marker class implementations of InstanceProvider
and define your
marked components in the dependency injection container.
For example,
In the example above, the FooCacheProvider
and BarCacheProvider
subclasses of InstanceProvider
are used as marker classes that indicate
which type of cache component to provide via dependency injection.
The default implementation of InstanceProvider
will look for
instances defined in the container with a name corresponding to the
subclass provider's class name. The Provider
suffix is dropped, and
a :::provided
suffix is appended.
So the example\package\FooCacheProvider
's get()
method is going to use the
container to look for an instance defined as
example\package\FooCache:::provided
.
You can override this behavior by overriding one or more of $class
,
$params
, and $config
properties when you implement your InstanceProvider
subclass.
For example,
Type Safety
As seen above, you can configure the provider to return objects of any type via overriding class properties or via container definitions. You may want to enforce a certain level of type safety.
For example, if FooCacheProvider
should only return certain cache types, you
could specify it as follows:
This will ensure that your application will throw an exception if someone accidentally configures it to return something else instead: