Download the PHP package yii1tech/di without Composer
On this page you can find all versions of the php package yii1tech/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package di
Yii1 Dependency Injection Extension
This extension provides Dependency Injection support for Yii1 application.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the "require" section of your composer.json.
Usage
This extension provides Dependency Injection support for Yii1 application using PSR-11 compatible container.
This extension introduces static facade class \yii1tech\di\DI
, which provides global access to PSR container and injector.
All dependency injection features provided in this extension relies on this facade.
It provides easy way for container entity retrieval and dependency injection.
For example:
Container Setup
The actual dependency injection container should be set via \yii1tech\di\DI::setContainer()
method.
It should be done at the Yii bootstrap stage before the application is created.
For example:
Instead of creating container instance right away, you may specify a PHP callback, which will instantiate it. For example:
Dependency Injection for Application Components
This extension allows configuration of the Application (Service Locator) components via DI container. In order to make it work, you need to use one of application classes provided within this extension - feature will not function automatically on the standard ones.
Following classes are provided:
\yii1tech\di\web\WebApplication
- DI aware Web Application.\yii1tech\di\console\ConsoleApplication
- DI aware Console Application.\yii1tech\di\base\Module
- DI aware Module.\yii1tech\di\web\WebModule
- DI aware Web Module.
If you use your own custom application class, you may apply DI component resolution to it using \yii1tech\di\base\ResolvesComponentViaDI
trait.
For example:
Being DI-aware, application will resolve configured components via DI Container according to their configured class. This allows you moving configuration into DI Container from Service Locator without breaking anything. For example:
Heads up! Be careful while moving Yii component definitions into DI container: remember to invoke method init()
manually on them.
Yii often places crucial initialization logic into component's init()
, if you omit its invocation you may receive broken component instance,
while fetching it directly from the container.
Heads up! If you move application component config to DI container, make sure to clean up its original configuration at Service Locator. Any remaining "property-value" definition will be applied to the object fetched from container. It may be useful, when you define container entity via factory, but in most cases it will cause you trouble.
Tip: component placed into Yii Application (Service Locator) via DI container is not required to implement
\IApplicationComponent
interface, since methodinit()
will never be automatically invoked on it.
Dependency Injection in Web Application
This extension allows injection of DI container entities into controller constructors and action methods based on arguments' type-hints.
However, this feature will not work on standard controllers extended directly from CController
- you'll need to extend \yii1tech\di\web\Controller
class or use \yii1tech\di\web\ResolvesActionViaDI
trait.
For example:
Heads up! While declaring your own controller's constructor, make sure it accepts parent's arguments $id
and $module
and passes them to the parent constructor. Otherwise, controller may not function properly.
Dependency Injection in Console Application
This extension allows injection of DI container entities into console commands constructors and action methods based on arguments' type-hints.
However, this feature will not work on standard console commands extended directly from CConsoleCommand
- you'll need to extend
\yii1tech\di\console\ConsoleCommand
class or use \yii1tech\di\console\ResolvesActionViaDI
trait.
For example:
Heads up! While declaring your own console command's constructor, make sure it accepts parent's arguments $name
and $runner
and passes them to the parent constructor. Otherwise, console command may not function properly.
External (3rd party) Container Usage
Container \yii1tech\di\Container
is very basic, and you may want to use something more sophisticated like PHP-DI instead of it.
Any PSR-11 compatible container can be used within this extension. All you need is pass your container to \yii1tech\di\DI::setContainer()
.
For example:
Many existing PSR-11 compatible solutions are coming with built-in injection mechanism, which you may want to utilize instead of the one,
provided by this extension. In order to do this, you should create your own injector, implementing \yii1tech\di\InjectorContract
interface,
and pass it to \yii1tech\di\DI::setInjector()
.
Many of the existing PSR-11 containers (including the 'PHP-DI') already implements dependency injection methods make()
and call()
.
You can use \yii1tech\di\external\ContainerBasedInjector
to utilize these methods. For example:
Heads up! Many of the existing PSR-11 containers, which provide built-in injection methods, implements method \Psr\Container\ContainerInterface::has()
in inconsistent way. While standard demands: it should return true
only, if there is explicit binding inside the container - many solutions return
true
even, if binding does not exist, but requested class can be resolved, e.g. its constructor arguments can be filled up using other bindings
from container. Such check is always comes with extra class and method reflection creation, which reduces the overall performance. This may cause a
significant impact, while resolving Yii application components via DI container.
It is recommended to resolve any inconsistency in \Psr\Container\ContainerInterface::has()
implementation before using container with this extension.
This can be easily achieved using \yii1tech\di\external\ContainerProxy
class. For example: