Browse Source

Allow DI references in array in dependencies (#18127)

* Allow DI references in array in dependencies

Extremely needed for handler maps and like.
Example container config:

```
return [
    ContentTypeMiddleware::class => [
        '__construct()' => [
            Instance::of(StreamFactory::class),
            [
                'json' => Intance::of(JsonFormatter::class),
                'yaml' => Intance::of(YamlFormatter::class),
            ],
        ],
    ],
];
```

* Add tests for DI references in array in dependencies

* Add changelog line

* Update CHANGELOG.md

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
tags/2.0.36
Alexander Makarov 4 years ago committed by GitHub
parent
commit
b6f09b5b2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 2
      framework/di/Container.php
  3. 38
      tests/framework/di/ContainerTest.php
  4. 21
      tests/framework/di/stubs/Corge.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.36 under development
------------------------
- Bug #18127: Add resolving DI references inside of arrays in dependencies (hiqsol)
- Bug #18047: Fix colorization markers output in Table.php (cheeseq)
- Bug #18028: Fix division by zero exception in Table.php::calculateRowHeight (fourhundredfour)
- Enh #18019: Allow jQuery 3.5.0 to be installed (wouter90)

2
framework/di/Container.php

@ -487,6 +487,8 @@ class Container extends Component
$class = $reflection->getName();
throw new InvalidConfigException("Missing required parameter \"$name\" when instantiating \"$class\".");
}
} elseif (is_array($dependency)) {
$dependencies[$index] = $this->resolveDependencies($dependency, $reflection);
}
}

38
tests/framework/di/ContainerTest.php

@ -16,6 +16,7 @@ use yiiunit\data\ar\Order;
use yiiunit\data\ar\Type;
use yiiunit\framework\di\stubs\Bar;
use yiiunit\framework\di\stubs\BarSetter;
use yiiunit\framework\di\stubs\Corge;
use yiiunit\framework\di\stubs\Foo;
use yiiunit\framework\di\stubs\FooProperty;
use yiiunit\framework\di\stubs\Qux;
@ -352,6 +353,43 @@ class ContainerTest extends TestCase
$this->assertSame(42, $qux->a);
}
public function testReferencesInArrayInDependencies()
{
$quxInterface = 'yiiunit\framework\di\stubs\QuxInterface';
$container = new Container();
$container->setSingletons([
$quxInterface => [
'class' => Qux::className(),
'a' => 42,
],
'qux' => Instance::of($quxInterface),
'bar' => [
'class' => Bar::className(),
],
'corge' => [
'__class' => Corge::className(),
'__construct()' => [
[
'qux' => Instance::of('qux'),
'bar' => Instance::of('bar'),
'q33' => new Qux(33),
],
],
],
]);
$corge = $container->get('corge');
$this->assertInstanceOf(Corge::className(), $corge);
$qux = $corge->map['qux'];
$this->assertInstanceOf(Qux::className(), $qux);
$this->assertSame(42, $qux->a);
$bar = $corge->map['bar'];
$this->assertInstanceOf(Bar::className(), $bar);
$this->assertSame($qux, $bar->qux);
$q33 = $corge->map['q33'];
$this->assertInstanceOf(Qux::className(), $q33);
$this->assertSame(33, $q33->a);
}
public function testGetByInstance()
{
$container = new Container();

21
tests/framework/di/stubs/Corge.php

@ -0,0 +1,21 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\di\stubs;
use yii\base\BaseObject;
class Corge extends BaseObject
{
public $map;
public function __construct(array $map, $config = [])
{
$this->map = $map;
parent::__construct($config);
}
}
Loading…
Cancel
Save