From 633d6b93b8ff2b026366b4b4d0590c5fda64277d Mon Sep 17 00:00:00 2001 From: Elvira Sheina Date: Sun, 9 Sep 2018 03:12:32 +0500 Subject: [PATCH] Fixes #15802: Fixed exception class in yii\di\Container --- framework/CHANGELOG.md | 1 + framework/di/Container.php | 8 +++++++- tests/framework/di/ContainerTest.php | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index be266f2268..48c96e8da6 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -17,6 +17,7 @@ Yii Framework 2 Change Log - Bug #16006: Handle case when `X-Forwarded-Host` header have multiple hosts separated with a comma (pgaultier) - Bug #16010: Fixed `yii\filters\ContentNegotiator` behavior when GET parameters contain an array (rugabarbo) - Bug #14660: Fixed `yii\caching\DbCache` concurrency issue when set values with the same key (rugabarbo) +- Bug #15802: Fixed exception class in yii\di\Container (vuchastyi, developeruz) - Bug #15988: Fixed bash completion (alekciy) - Bug #15798: Fixed render `yii\grid\RadioButtonColumn::$content` and `yii\grid\CheckboxColumn::$content` (lesha724) - Bug #15548: Fixed index names collision in RBAC (gonimar) diff --git a/framework/di/Container.php b/framework/di/Container.php index 34c804373c..83b381d990 100644 --- a/framework/di/Container.php +++ b/framework/di/Container.php @@ -256,6 +256,7 @@ class Container extends Component unset($this->_singletons[$class]); return $this; } + /** * Registers a class definition with this container and marks the class as a singleton class. * @@ -417,6 +418,7 @@ class Container extends Component * Returns the dependencies of the specified class. * @param string $class class name, interface name or alias name * @return array the dependencies of the specified class. + * @throws InvalidConfigException if a dependency cannot be resolved or if a dependency cannot be fulfilled. */ protected function getDependencies($class) { @@ -425,7 +427,11 @@ class Container extends Component } $dependencies = []; - $reflection = new ReflectionClass($class); + try { + $reflection = new ReflectionClass($class); + } catch (\ReflectionException $e) { + throw new InvalidConfigException('Failed to instantiate component or class "' . $class . '".', 0, $e); + } $constructor = $reflection->getConstructor(); if ($constructor !== null) { diff --git a/tests/framework/di/ContainerTest.php b/tests/framework/di/ContainerTest.php index 03d9717e74..291e86a3af 100644 --- a/tests/framework/di/ContainerTest.php +++ b/tests/framework/di/ContainerTest.php @@ -260,6 +260,7 @@ class ContainerTest extends TestCase 'qux.using.closure' => function () { return new Qux(); }, + 'rollbar', 'baibaratsky\yii\rollbar\Rollbar' ]); $container->setDefinitions([]); @@ -271,6 +272,14 @@ class ContainerTest extends TestCase $this->assertEquals('item1', $traversable->current()); $this->assertInstanceOf('yiiunit\framework\di\stubs\Qux', $container->get('qux.using.closure')); + + try { + $container->get('rollbar'); + $this->fail('InvalidConfigException was not thrown'); + } catch(\Exception $e) + { + $this->assertInstanceOf('yii\base\InvalidConfigException', $e); + } } public function testContainerSingletons()