最后,我提出了自己的DI容器实现,引入了新的可配置属性:
public $inheritableDefinitions = [];
完整类别代码:
<?php
namespace app\di;
/**
* @inheritdoc
*/
class Container extends \yii\di\Container
{
/**
* @var array inheritable object configurations indexed by class name
*/
public $inheritableDefinitions = [];
/**
* @inheritdoc
*
* @param string $class
* @param array $params
* @param array $config
*
* @return object the newly created instance of the specified class
*/
protected function build($class, $params, $config) {
$config = $this->mergeInheritedConfiguration($class, $config);
return parent::build($class, $params, $config);
}
/**
* Merges configuration arrays of parent classes into configuration of newly created instance of the specified class.
* Properties defined in child class (via configuration or property declarations) will not get overridden.
*
* @param string $class
* @param array $config
*
* @return array
*/
protected function mergeInheritedConfiguration($class, $config) {
if (empty($this->inheritableDefinitions)) {
return $config;
}
$inheritedConfig = [];
/** @var \ReflectionClass $reflection */
list($reflection) = $this->getDependencies($class);
foreach ($this->inheritableDefinitions as $parentClass => $parentConfig) {
if ($class === $parentClass) {
$inheritedConfig = array_merge($inheritedConfig, $parentConfig);
} else if (is_subclass_of($class, $parentClass)) {
/** @var \ReflectionClass $parentReflection */
list($parentReflection) = $this->getDependencies($parentClass);
// The "@" is necessary because of possible (and wanted) array to string conversions
$notInheritableProperties = @array_diff_assoc($reflection->getDefaultProperties(),
$parentReflection->getDefaultProperties());
// We don't want to override properties defined specifically in child class
$parentConfig = array_diff_key($parentConfig, $notInheritableProperties);
$inheritedConfig = array_merge($inheritedConfig, $parentConfig);
}
}
return array_merge($inheritedConfig, $config);
}
}
这就是如何使用它来完成问题中描述的LinkPager的定制:
'container' => [
'inheritableDefinitions' => [
'yii\widgets\LinkPager' => ['maxButtonCount' => 5],
],
],
现在如果我创建一个类
FancyLinkPager
这延伸了
yii\widgets\LinkPager
DI容器将合并默认配置:
$pagination = \Yii::createObject(Pagination::class);
$linkPager = \Yii::createObject(['class' => LinkPager::class, 'pagination' => $pagination]);
$fancyLinkPager = \Yii::createObject(['class' => FancyLinkPager::class, 'pagination' => $pagination]);
$linkPager->maxButtonCount; // 5 as configured
$fancyLinkPager->maxButtonCount; // 5 as configured - hurrah!
我也考虑到了
qiangxue's comment
关于类定义中默认属性值的显式设置
FancyLinkPager
类本身:
class FancyLinkPager extends LinkPager
{
public $maxButtonCount = 18;
}
将遵守以下属性设置:
$linkPager->maxButtonCount; // 5 as configured
$fancyLinkPager->maxButtonCount; // 18 as declared
要在应用程序中交换默认DI容器,必须显式设置
Yii::$container
在输入脚本的某个地方:
Yii::$container = new \app\di\Container();