Skip to content

Commit

Permalink
Fix yiisoft#18373: Fix not taking default value when unable to resolv…
Browse files Browse the repository at this point in the history
…e non-exing class via DI container
  • Loading branch information
vjik authored Nov 10, 2020
1 parent ee0fe97 commit bbd9a26
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Yii Framework 2 Change Log
==========================

2.0.40 under development
------------------------
2.0.39.1 under development
--------------------------

- Bug #18373: Fix not taking default value when unable to resolve non-exing class via DI container (vjik)
- Enh #18370: Add option to provide a string replacement for `null` value in `yii\data\DataFilter` (bizley)


Expand Down
7 changes: 6 additions & 1 deletion framework/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\di;

use ReflectionClass;
use ReflectionException;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -518,7 +519,11 @@ protected function getDependencies($class)
$c = $param->getType();
$isClass = $c !== null && !$param->getType()->isBuiltin();
} else {
$c = $param->getClass();
try {
$c = $param->getClass();
} catch (ReflectionException $e) {
$c = null;
}
$isClass = $c !== null;
}
$className = $isClass ? $c->getName() : null;
Expand Down
1 change: 1 addition & 0 deletions tests/framework/di/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public function testNulledConstructorParameters()
$alpha = $container->get(Alpha::className());
$this->assertInstanceOf(Beta::className(), $alpha->beta);
$this->assertInstanceOf($QuxInterface, $alpha->omega);
$this->assertNull($alpha->unknown);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/framework/di/stubs/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class Alpha extends BaseObject
{
public $beta;
public $omega;
public $unknown = true;

public function __construct(Beta $beta = null, QuxInterface $omega = null)
public function __construct(Beta $beta = null, QuxInterface $omega = null, Unknown $unknown = null)
{
$this->beta = $beta;
$this->omega = $omega;
$this->unknown = $unknown;
}
}

0 comments on commit bbd9a26

Please sign in to comment.