我们说我有这样的配置(来自official guide的代码段):
$config = [
// ...
'container' => [
'definitions' => [
'yii\widgets\LinkPager' => ['maxButtonCount' => 5],
],
],
// ...
];
我创建了一个名为FancyLinkPager的类:
class FancyLinkPager extends \yii\widgets\LinkPager
{
// ...
}
当我像这样创建一个类FancyLinkPager的对象时(请忽略$ pagination对象,为了正确起见,它在这里):
$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; // 10 as LinkPager's default
我的问题是我希望$ fancyLinkPager-> maxButtonCount配置为5。我知道我可以在配置中添加另一行或调整它以指定我的自定义类,但它不适合我,因为:
我的问题是:是否有任何框架支持的方法来实现这一目标?我想出的解决方案是:
也许唯一真正的解决方案是创建我自己的yii \ di \ Container实现,允许从父类继承配置,但在我深入研究之前,我想知道我是否还没有忽略某些东西。
答案 0 :(得分:0)
最后,我提出了自己的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();