If you want to maintain it, you can make a fork.
This extension can configure di container for yii2 with simple php-array and use lazy load.
For license information check the LICENSE-file.
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist mougrim/yii2-container-configurator
or add
"mougrim/yii2-container-configurator": "*"
to the require
section of your composer.json.
Edit file config/main.php:
<?php
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
use yii\di\Container;
...
Yii::$container->set(
'containerConfigurator',
function (Container $container) {
$containerConfigurator = new ContainerConfigurator($container);
$containerConfigurator->configure(require __DIR__ . '/container.php'); // di container config path
return $containerConfigurator;
}
);
...
return [
...
'bootstrap' => [
...
'containerConfigurator'
],
'components' => [
'containerConfigurator' => 'containerConfigurator',
...
],
];
And create config/container.php:
<?php
return [
// your di container config
]
You can use callbacks in container config:
<?php
return [
'app' => function () {
return Yii::$app;
},
];
This callback will call in all get:
Yii::$container->get('app');
By default components created as service, i.e. created once in first get:
<?php
use yii\web\Response;
return [
'front.response' => [
'class' => Response::class, // class name
],
];
If you want for every get created new instance, you can use prototype:
<?php
use yii\web\Response;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
return [
'front.response' => [
'class' => Response::class, // class name
'type' => ContainerConfigurator::COMPONENT_TYPE_PROTOTYPE,
],
];
If you want add alias of service or prototype:
<?php
use yii\web\Response;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
return [
'front.response' => [
'class' => Response::class, // class name
],
'front.response-alias' => 'front.response',
];
Now this:
<?php
Yii::$container->get('front.response-alias');
equivalent this:
<?php
Yii::$container->get('front.response');
If you want inject from di, you can use parameter type reference:
[
'id' => 'front.request',
'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
]
If you want inject some value, you can use parameter type value:
[
'type' => ContainerConfigurator::ARGUMENT_TYPE_VALUE,
'value' => 'some value',
],
If you want pass arguments to constructor, you can use 'arguments':
<?php
use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
return [
'controllers.site' => [
'class' => SiteController::class,
'arguments' => [
2 => [ // argument number
'id' => 'front.request',
'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
],
],
],
];
SiteController:
<?php
namespace frontend\controllers;
use yii\base\Module;
use yii\web\Controller
use yii\web\Request;
class SiteController extends Controller
{
private $request;
public function __construct(
$id,
Module $module,
Request $request,
array $config = []
)
{
parent::__construct($id, $module, $config);
$this->request = $request;
}
}
And add to controller map in config/main.php:
...
'controllerMap' => [
...
'site' => 'controllers.site',
],
...
If you want set properties, you can use 'properties':
<?php
use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
return [
'controllers.site' => [
'class' => SiteController::class,
'properties' => [
'request' => [
'id' => 'front.request',
'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
],
],
],
];
If you want call callbacks, you can use 'call', but in yii objects (extends from \yii\base\Object) callback will call after method init:
<?php
use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
return [
'controllers.site' => [
'class' => SiteController::class,
'call' => [
'setRequest' => [
[
'id' => 'front.request',
'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
],
],
],
],
];
If you want extend config, you can use 'extends':
<?php
use frontend\controllers\SiteController;
use mougrim\yii2ContainerConfigurator\ContainerConfigurator;
return [
'components.controller' => [
'type' => ContainerConfigurator::COMPONENT_TYPE_PROTOTYPE,
'properties' => [
'app' => [
'id' => 'app',
'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
],
],
],
'controllers.site' => [
'class' => SiteController::class,
'extends' => 'components.controller',
'arguments' => [
2 => [
'id' => 'front.request',
'type' => ContainerConfigurator::ARGUMENT_TYPE_REFERENCE,
],
],
],
];