amylian/yii2-amylian-di

ATTENTION: EXPERIMENTAL! Dependency Injection Utilities for YII2

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2019-04-06 09:19 UTC

This package is auto-updated.

Last update: 2024-12-06 23:05:59 UTC


README

Latest Stable Version License [Build Status (master)]https://travis-ci.org/amylian/yii2-amylian-di.svg?branch=master

Extended Dependency Injection Container for Yii2

Features

Provides an extended implementation of an Dependency Injection Container for the Yii2 Framework

The following features are added to the standard implementation:

  • Named Parameters: Constructor parameters do not need to be specified by order or index-position, but constructor parameter names can be used optionally.
  • Setter-Calls: It's possible to specify setter-calls in the configuration array using the format methodName(). The value can be a reference (Instance or ReferenceInterface), but must be specified in an array.
  • ReferenceInterface: Additionally to the standard Instance object, a ReferenceInterface is defined which is resolved upon object creation. This component already contains various implementations of ReferenceInterface:
    • InstanceArray: Can be used to define an array of references (Instance-objects or objects implementing ReferenceInterface which are automatically resolved by the Container when needed. Note: The array may contain other values. If a item is not a reference (i.E. a object of another type, a string, etc.) the item is left unchanged.
    • Alias: Can be used to define a path containing an alias, e.g. "@runtime/data/path". This type of reference can be used in the config array if a path is used, but the defined object does not support Yii-Aliases (i.E. 3rd party, not yii-specific classes). using \Amylian\Yii\DI\Alias:of('@app/my/path') does the same as \Yii:getPath('@app/my/path'), but the latter cannot be used in configuration as the aliases are unknown when the configuration is loaded.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist amylian/yii2-amylian-di "*"

or add

"amylian/yii2-amylian-di": "*"

to the require section of your composer.json file.

Usage

Use the extended Container Class as standard Container in Yii2:

Yii2 creates the global Container by automatically. The easiest way to replace it with an instance of \Amylian\Yii\DI\Container is to add

\Yii::$container = new Amylian\Yii\DI\Container();

right after loading Yii.php:

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

\Yii::$container = new Amylian\Yii\DI\Container();  // <--- Create New Container!

(new yii\web\Application($config))->run();