Код: Выделить всё
'components' => [
'storage' => [
'class' => 'app\components\Storage',
],
'component' => [
'class' => 'app\components\Component',
'storage' => 'storage',
],
],
Код: Выделить всё
'components' => [
'component' => [
'class' => 'app\components\Component',
'storage' => [
'class' => 'app\components\Storage',
],
],
],
Код: Выделить всё
class Component
{
public $storage;
}
Код: Выделить всё
class Component
{
private $_storage;
public function setStorage($value)
{
$this->_storage = Yii::$app->get($value);
}
}
Код: Выделить всё
class Component
{
private $_storage;
public function setStorage($value)
{
$this->_storage = Yii::createObject($value);
}
}
Было бы неполохо передавать компоненты неким маркером отложенного связывания по аналогии с DI для классов:
Код: Выделить всё
'components' => [
'storage' => [ ... ],
'component' => [
'class' => 'app\components\Component',
'storage' => Instance::of('storage'),
],
],
Чьё-то обсуждение закрылось весьма оперативно. Но, к костыльному счастью, можно определять компоненты анонимками. Так что на данный момент нашёл более-менее вменяемый вариант побороть это неудобство:
Код: Выделить всё
'components' => [
'storage' => [ ... ],
'component' => function () { return Yii::createObject([
'class' => 'app\components\Component',
'storage' => Yii::$app->get('storage'),
]); },
],