Skip to content

Commit

Permalink
Fixes yiisoft#6632: yii\di\Container::get() did not handle config p…
Browse files Browse the repository at this point in the history
…arameter correctly when it is passed as a constructor parameter
  • Loading branch information
qiangxue committed Dec 24, 2014
1 parent 840fe73 commit 166cb99
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions framework/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,16 @@ protected function build($class, $params, $config)
$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;
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/framework/di/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,15 @@ public function testDefault()
$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);
}
}

0 comments on commit 166cb99

Please sign in to comment.