-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to use container as a factory
- Loading branch information
Showing
3 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Di\Hook; | ||
|
||
use Closure; | ||
use Yiisoft\Di\Container; | ||
|
||
final class AfterBuiltHook | ||
{ | ||
public static function unsetInstance(): Closure | ||
{ | ||
return function (Container $container, string $id) { | ||
/** | ||
* @var $this Container | ||
*/ | ||
unset($this->instances[$id]); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unit\Hook; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Di\Container; | ||
use Yiisoft\Di\ContainerConfig; | ||
use Yiisoft\Di\Hook\AfterBuiltHook; | ||
use Yiisoft\Di\Tests\Support\EngineInterface; | ||
use Yiisoft\Di\Tests\Support\EngineMarkOne; | ||
|
||
final class AfterBuiltHookTest extends TestCase | ||
{ | ||
public function testDifferentObjects() | ||
{ | ||
$config = ContainerConfig::create() | ||
->withDefinitions([ | ||
EngineInterface::class => [ | ||
'class' => EngineMarkOne::class, | ||
'afterBuilt' => AfterBuiltHook::unsetInstance() | ||
], | ||
]); | ||
$container = new Container($config); | ||
|
||
$this->assertNotSame( | ||
$container->get(EngineInterface::class), | ||
$container->get(EngineInterface::class), | ||
); | ||
} | ||
} |