Commit 166cb99b by Qiang Xue

Fixes #6632: `yii\di\Container::get()` did not handle config parameter correctly…

Fixes #6632: `yii\di\Container::get()` did not handle config parameter correctly when it is passed as a constructor parameter
parent 840fe732
......@@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #6080: Oracle DB schema did not load column types correctly (wenbin1989)
- Bug #6404: advanced application template `Alert` widget was generating duplicate IDs in case of multiple flashes (SDKiller)
- Bug #6557: Link URLs generated by `yii\widgets\Menu` are not encoded (qiangxue)
- Bug #6632: `yii\di\Container::get()` did not handle config parameter correctly when it is passed as a constructor parameter (qiangxue)
- Enh #6247: Logger and error handler are now using slightly less memory (stepanselyuk, samdark)
- Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv)
- Enh #6467: `ActiveForm` will scroll to the nearest visible element when the first error input is hidden (newartix)
......
......@@ -357,13 +357,16 @@ class Container extends Component
$dependencies[$index] = $param;
}
$dependencies = $this->resolveDependencies($dependencies, $reflection);
if (empty($config)) {
return $reflection->newInstanceArgs($dependencies);
}
if (!empty($dependencies) && is_a($class, 'yii\base\Object', true)) {
// set $config as the last parameter (existing one will be overwritten)
$dependencies[count($dependencies) - 1] = $config;
$dependencies = $this->resolveDependencies($dependencies, $reflection);
return $reflection->newInstanceArgs($dependencies);
} else {
$dependencies = $this->resolveDependencies($dependencies, $reflection);
$object = $reflection->newInstanceArgs($dependencies);
foreach ($config as $name => $value) {
$object->$name = $value;
......
......@@ -89,5 +89,15 @@ class ContainerTest extends TestCase
$qux1 = $container->get('qux');
$qux2 = $container->get('qux');
$this->assertTrue($qux1 === $qux2);
// config
$container = new Container;
$container->set('qux', $Qux);
$qux = $container->get('qux', [], ['a' => 2]);
$this->assertEquals(2, $qux->a);
$qux = $container->get('qux', [3]);
$this->assertEquals(3, $qux->a);
$qux = $container->get('qux', [3, ['a' => 4]]);
$this->assertEquals(4, $qux->a);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment