From 19f233952e192507f9c621ba94c9fbad9c5da509 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Fri, 17 Mar 2023 18:24:56 +0900 Subject: [PATCH 01/14] Bump ray/compiler to ^1.10 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 12bbe14..2098a1a 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "require": { "php": ">=8.0", "ray/di": "^2.15.1", - "ray/compiler": "^1.9.2", + "ray/compiler": "^1.10", "doctrine/cache": "^1.10 || ^2.2" }, "require-dev": { From bcbf445817189f8390670f3a593d7647d56b447d Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Fri, 17 Mar 2023 18:55:49 +0900 Subject: [PATCH 02/14] Add module override --- src/Application.php | 22 +++++++++++++++++++- tests/ApplicationTest.php | 33 ++++++++++++++++++++++++++++++ tests/Classes/OverrideGreeting.php | 13 ++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/Classes/OverrideGreeting.php diff --git a/src/Application.php b/src/Application.php index a7b00d1..88ab869 100644 --- a/src/Application.php +++ b/src/Application.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Container\BindingResolutionException; use Ray\Compiler\AbstractInjectorContext; use Ray\Compiler\ContextInjector; +use Ray\Di\AbstractModule; use Ray\Di\Exception\Unbound; use Ray\Di\InjectorInterface; use Ray\RayDiForLaravel\Attribute\Injectable; @@ -22,10 +23,15 @@ class Application extends \Illuminate\Foundation\Application /** @var string[] */ private array $abstractsResolvedByRay = []; + private AbstractInjectorContext $context; + + private AbstractModule|null $overrideModule = null; + public function __construct(string $basePath, AbstractInjectorContext $injectorContext) { parent::__construct($basePath); $this->injector = ContextInjector::getInstance($injectorContext); + $this->context = $injectorContext; } protected function resolve($abstract, $parameters = [], $raiseEvents = true) @@ -34,8 +40,10 @@ protected function resolve($abstract, $parameters = [], $raiseEvents = true) return parent::resolve($abstract, $parameters, $raiseEvents); } + $injector = $this->overrideModule ? ContextInjector::getOverrideInstance($this->context, $this->overrideModule) : $this->injector; + try { - return $this->injector->getInstance($abstract); + return $injector->getInstance($abstract); } catch (Unbound $e) { throw new BindingResolutionException("Failed to resolve {$abstract} by Ray's injector.", 0, $e); } @@ -61,4 +69,16 @@ private function shouldBeResolvedByRay(string $abstract): bool $this->abstractsResolvedByRay[] = $abstract; return true; } + + public function flush() + { + parent::flush(); + + $this->overrideModule = null; + } + + public function overrideModule(AbstractModule $module): void + { + $this->overrideModule = $module; + } } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 84ddf6e..97ea761 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -7,13 +7,16 @@ use Illuminate\Contracts\Container\BindingResolutionException; use PHPUnit\Framework\TestCase; use Ray\Compiler\AbstractInjectorContext; +use Ray\Di\AbstractModule; use Ray\RayDiForLaravel\Classes\FakeCacheableContext; use Ray\RayDiForLaravel\Classes\FakeContext; use Ray\RayDiForLaravel\Classes\FakeInvalidContext; +use Ray\RayDiForLaravel\Classes\GreetingInterface; use Ray\RayDiForLaravel\Classes\GreetingServiceProvider; use Ray\RayDiForLaravel\Classes\IlluminateGreeting; use Ray\RayDiForLaravel\Classes\InjectableService; use Ray\RayDiForLaravel\Classes\NonInjectableService; +use Ray\RayDiForLaravel\Classes\OverrideGreeting; class ApplicationTest extends TestCase { @@ -91,6 +94,36 @@ public function testFailedToResolveByRayDi(): void $app->make(InjectableService::class); } + /** @depends testResolvedByRayWhenMarkedClassGiven */ + public function testOverrideModule(Application $application): Application + { + $overrideModule = new class extends AbstractModule { + protected function configure() + { + $this->bind(GreetingInterface::class)->to(OverrideGreeting::class); + } + }; + $application->overrideModule($overrideModule); + + $instance = $application->make(InjectableService::class); + + $this->assertInstanceOf(InjectableService::class, $instance); + $this->assertSame('Hello, override!', $instance->run()); + + return $application; + } + + /** @depends testOverrideModule */ + public function testFlush(Application $application): void + { + $application->flush(); + + $instance = $application->make(InjectableService::class); + + $this->assertInstanceOf(InjectableService::class, $instance); + $this->assertSame('Hello, Ray! Intercepted.', $instance->run()); + } + /** * @param class-string $contextClass */ diff --git a/tests/Classes/OverrideGreeting.php b/tests/Classes/OverrideGreeting.php new file mode 100644 index 0000000..f88dd2c --- /dev/null +++ b/tests/Classes/OverrideGreeting.php @@ -0,0 +1,13 @@ + Date: Fri, 17 Mar 2023 18:56:41 +0900 Subject: [PATCH 03/14] Flush abstractsResolvedByRay --- src/Application.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Application.php b/src/Application.php index 88ab869..80f0b6f 100644 --- a/src/Application.php +++ b/src/Application.php @@ -75,6 +75,7 @@ public function flush() parent::flush(); $this->overrideModule = null; + $this->abstractsResolvedByRay = []; } public function overrideModule(AbstractModule $module): void From 02d9ed47a510a70931c980954c4bbbedf9c92e6a Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Mon, 20 Mar 2023 11:40:13 +0900 Subject: [PATCH 04/14] Add OverrideModule trait --- src/Testing/OverrideModule.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/Testing/OverrideModule.php diff --git a/src/Testing/OverrideModule.php b/src/Testing/OverrideModule.php new file mode 100644 index 0000000..1af47be --- /dev/null +++ b/src/Testing/OverrideModule.php @@ -0,0 +1,24 @@ +app instanceof Application) { + return; + } + + $this->app->overrideModule($module); + } +} From c7a40bcfaa4566a33c19ff98b8d7e916e3c75da3 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Mon, 20 Mar 2023 11:47:48 +0900 Subject: [PATCH 05/14] Update README --- README.ja.md | 25 +++++++++++++++++++++++++ README.md | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README.ja.md b/README.ja.md index bce6606..08d6e93 100644 --- a/README.ja.md +++ b/README.ja.md @@ -88,6 +88,31 @@ apcu拡張が有効な場合インジェクターをキャッシュします。 独自のコンテキストが必要になる場合もあります。 組み込みコンテキストを参考にカスタムコンテキストを実装し、`RayDi/Context/ContextProvider`で利用するようにします。 +### モジュールのオーバーライド + +テスト実行時にはテストケースによって束縛を変更したい場合があります。 + +以下のように、テストクラスで `Ray\RayDiForLaravel\Testing\OverrideModule` を利用し、`$this->overrideModule` を呼び出してください。 + +```php +use Tests\TestCase; + +final class HelloTest extends TestCase +{ + use Ray\RayDiForLaravel\Testing\OverrideModule; + + public function testStatusOk(): void + { + $this->overrideModule(new OverrideModule()); + + $res = $this->get('/hello'); + + $res->assertOk(); + $res->assertSeeText('Hello 1 * 2 = 2.'); + } +} +``` + ## パフォーマンス [DiCompileModule](https://github.com/ray-di/Ray.Compiler/blob/1.x/src/DiCompileModule.php)をインストールすることで、 diff --git a/README.md b/README.md index 885a775..5a839fa 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,31 @@ In the `RayDi/Context/ProductionContext`, the injector is cached if the apcu ext You may need your own context. Implement a custom context with reference to the built-in context and use it in `RayDi/Context/ContextProvider`. +### Overriding Modules + +When running tests, you may want to change the binding depending on the test case. + +Use `Ray\RayDiForLaravel\Testing\OverrideModule` in your test class and call `$this->overrideModule` as shown below. + +```php +use Tests\TestCase; + +final class HelloTest extends TestCase +{ + use Ray\RayDiForLaravel\Testing\OverrideModule; + + public function testStatusOk(): void + { + $this->overrideModule(new OverrideModule()); + + $res = $this->get('/hello'); + + $res->assertOk(); + $res->assertSeeText('Hello 1 * 2 = 2.'); + } +} +``` + ## Performance By installin the [DiCompileModule](https://github.com/ray-di/Ray.Compiler/blob/1.x/src/DiCompileModule.php), An optimized injector is used and dependency errors are reported at compile time, not at runtime. From 7f3203f5520790aea39733e0bcec0b5e03352322 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Mon, 20 Mar 2023 12:28:27 +0900 Subject: [PATCH 06/14] Add OverrideModuleTest --- tests/ApplicationTest.php | 11 ++----- tests/Classes/OverrideGreetingModule.php | 16 ++++++++++ tests/OverrideModuleTest.php | 39 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 tests/Classes/OverrideGreetingModule.php create mode 100644 tests/OverrideModuleTest.php diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 97ea761..f278143 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -7,16 +7,14 @@ use Illuminate\Contracts\Container\BindingResolutionException; use PHPUnit\Framework\TestCase; use Ray\Compiler\AbstractInjectorContext; -use Ray\Di\AbstractModule; use Ray\RayDiForLaravel\Classes\FakeCacheableContext; use Ray\RayDiForLaravel\Classes\FakeContext; use Ray\RayDiForLaravel\Classes\FakeInvalidContext; -use Ray\RayDiForLaravel\Classes\GreetingInterface; use Ray\RayDiForLaravel\Classes\GreetingServiceProvider; use Ray\RayDiForLaravel\Classes\IlluminateGreeting; use Ray\RayDiForLaravel\Classes\InjectableService; use Ray\RayDiForLaravel\Classes\NonInjectableService; -use Ray\RayDiForLaravel\Classes\OverrideGreeting; +use Ray\RayDiForLaravel\Classes\OverrideGreetingModule; class ApplicationTest extends TestCase { @@ -97,12 +95,7 @@ public function testFailedToResolveByRayDi(): void /** @depends testResolvedByRayWhenMarkedClassGiven */ public function testOverrideModule(Application $application): Application { - $overrideModule = new class extends AbstractModule { - protected function configure() - { - $this->bind(GreetingInterface::class)->to(OverrideGreeting::class); - } - }; + $overrideModule = new OverrideGreetingModule(); $application->overrideModule($overrideModule); $instance = $application->make(InjectableService::class); diff --git a/tests/Classes/OverrideGreetingModule.php b/tests/Classes/OverrideGreetingModule.php new file mode 100644 index 0000000..2bfe643 --- /dev/null +++ b/tests/Classes/OverrideGreetingModule.php @@ -0,0 +1,16 @@ +bind(GreetingInterface::class)->to(OverrideGreeting::class); + } +} diff --git a/tests/OverrideModuleTest.php b/tests/OverrideModuleTest.php new file mode 100644 index 0000000..d16e3b7 --- /dev/null +++ b/tests/OverrideModuleTest.php @@ -0,0 +1,39 @@ +app = new Application( + dirname(__DIR__), + new FakeContext(dirname(__DIR__) . '/tests') + ); + + $this->app->register(GreetingServiceProvider::class); + } + + public function testAppIsRayDiApplication(): void + { + $this->overrideModule(new OverrideGreetingModule()); + + $instance = $this->app->make(InjectableService::class); + $actual = $instance->run(); + + $this->assertSame('Hello, override!', $actual); + } +} From a1cc04aac4164a0afdfd262fcbfb40c7abfc8517 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Mon, 20 Mar 2023 12:28:45 +0900 Subject: [PATCH 07/14] Add codeCoverageIgnore --- src/Testing/OverrideModule.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Testing/OverrideModule.php b/src/Testing/OverrideModule.php index 1af47be..58735f7 100644 --- a/src/Testing/OverrideModule.php +++ b/src/Testing/OverrideModule.php @@ -12,11 +12,11 @@ trait OverrideModule protected function overrideModule(AbstractModule $module): void { if (! property_exists($this, 'app')) { - return; + return; // @codeCoverageIgnore } if (! $this->app instanceof Application) { - return; + return; // @codeCoverageIgnore } $this->app->overrideModule($module); From f6afda2a112636082fc1f592c22714fc8a2448f0 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Mon, 20 Mar 2023 12:39:55 +0900 Subject: [PATCH 08/14] Enable in memory injector cache each overrideModule --- src/Application.php | 24 +++++++++++++++++++++++- tests/ApplicationTest.php | 31 +++++++++++++++++++++++++++++++ tests/Classes/FakeGreeting.php | 13 +++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/Classes/FakeGreeting.php diff --git a/src/Application.php b/src/Application.php index 80f0b6f..4568854 100644 --- a/src/Application.php +++ b/src/Application.php @@ -27,6 +27,9 @@ class Application extends \Illuminate\Foundation\Application private AbstractModule|null $overrideModule = null; + /** @var array, InjectorInterface> */ + private array $overrideInjectorInstance = []; + public function __construct(string $basePath, AbstractInjectorContext $injectorContext) { parent::__construct($basePath); @@ -40,7 +43,7 @@ protected function resolve($abstract, $parameters = [], $raiseEvents = true) return parent::resolve($abstract, $parameters, $raiseEvents); } - $injector = $this->overrideModule ? ContextInjector::getOverrideInstance($this->context, $this->overrideModule) : $this->injector; + $injector = $this->getInjector(); try { return $injector->getInstance($abstract); @@ -76,10 +79,29 @@ public function flush() $this->overrideModule = null; $this->abstractsResolvedByRay = []; + $this->overrideInjectorInstance = []; } public function overrideModule(AbstractModule $module): void { $this->overrideModule = $module; } + + private function getInjector(): InjectorInterface + { + if ($this->overrideModule === null) { + return $this->injector; + } + + $overrideModuleClass = $this->overrideModule::class; + + if (isset($this->overrideInjectorInstance[$overrideModuleClass])) { + return $this->overrideInjectorInstance[$overrideModuleClass]; + } + + $injector = ContextInjector::getOverrideInstance($this->context, $this->overrideModule); + $this->overrideInjectorInstance[$overrideModuleClass] = $injector; + + return $injector; + } } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index f278143..e8285be 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -7,9 +7,12 @@ use Illuminate\Contracts\Container\BindingResolutionException; use PHPUnit\Framework\TestCase; use Ray\Compiler\AbstractInjectorContext; +use Ray\Di\AbstractModule; use Ray\RayDiForLaravel\Classes\FakeCacheableContext; use Ray\RayDiForLaravel\Classes\FakeContext; +use Ray\RayDiForLaravel\Classes\FakeGreeting; use Ray\RayDiForLaravel\Classes\FakeInvalidContext; +use Ray\RayDiForLaravel\Classes\GreetingInterface; use Ray\RayDiForLaravel\Classes\GreetingServiceProvider; use Ray\RayDiForLaravel\Classes\IlluminateGreeting; use Ray\RayDiForLaravel\Classes\InjectableService; @@ -106,6 +109,34 @@ public function testOverrideModule(Application $application): Application return $application; } + /** @depends testOverrideModule */ + public function testAlreadyResolvedOverrideModule(Application $application): void + { + $instance = $application->make(InjectableService::class); + + $this->assertInstanceOf(InjectableService::class, $instance); + $this->assertSame('Hello, override!', $instance->run()); + } + + /** @depends testOverrideModule */ + public function testSetOtherOverrideModule(Application $application): void + { + $overrideModule = new class extends AbstractModule { + + protected function configure() + { + $this->bind(GreetingInterface::class)->to(FakeGreeting::class); + } + }; + + $application->overrideModule($overrideModule); + + $instance = $application->make(InjectableService::class); + + $this->assertInstanceOf(InjectableService::class, $instance); + $this->assertSame('Hello, fake!', $instance->run()); + } + /** @depends testOverrideModule */ public function testFlush(Application $application): void { diff --git a/tests/Classes/FakeGreeting.php b/tests/Classes/FakeGreeting.php new file mode 100644 index 0000000..12b10fd --- /dev/null +++ b/tests/Classes/FakeGreeting.php @@ -0,0 +1,13 @@ + Date: Mon, 20 Mar 2023 16:25:38 +0900 Subject: [PATCH 09/14] Update README --- README.ja.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.ja.md b/README.ja.md index 08d6e93..4b7f632 100644 --- a/README.ja.md +++ b/README.ja.md @@ -103,7 +103,7 @@ final class HelloTest extends TestCase public function testStatusOk(): void { - $this->overrideModule(new OverrideModule()); + $this->overrideModule(new MyModule()); $res = $this->get('/hello'); diff --git a/README.md b/README.md index 5a839fa..835b813 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ final class HelloTest extends TestCase public function testStatusOk(): void { - $this->overrideModule(new OverrideModule()); + $this->overrideModule(new MyModule()); $res = $this->get('/hello'); From 1bb3ffd2b49ab5bfd98373d2cd1b97b0c7f09339 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Wed, 22 Mar 2023 18:35:25 +0900 Subject: [PATCH 10/14] Consider nested modules for injector cache --- src/Application.php | 10 +++++----- tests/ApplicationTest.php | 12 ++++++++++++ tests/Classes/FakeFoo.php | 13 +++++++++++++ tests/Classes/Foo.php | 13 +++++++++++++ tests/Classes/FooInterface.php | 8 ++++++++ tests/Classes/InjectableService.php | 5 +++-- tests/Classes/Module.php | 1 + tests/Classes/OtherModule.php | 15 +++++++++++++++ 8 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 tests/Classes/FakeFoo.php create mode 100644 tests/Classes/Foo.php create mode 100644 tests/Classes/FooInterface.php create mode 100644 tests/Classes/OtherModule.php diff --git a/src/Application.php b/src/Application.php index 4568854..41f37b0 100644 --- a/src/Application.php +++ b/src/Application.php @@ -27,7 +27,7 @@ class Application extends \Illuminate\Foundation\Application private AbstractModule|null $overrideModule = null; - /** @var array, InjectorInterface> */ + /** @var array */ private array $overrideInjectorInstance = []; public function __construct(string $basePath, AbstractInjectorContext $injectorContext) @@ -93,14 +93,14 @@ private function getInjector(): InjectorInterface return $this->injector; } - $overrideModuleClass = $this->overrideModule::class; + $moduleString = md5((string) $this->overrideModule); - if (isset($this->overrideInjectorInstance[$overrideModuleClass])) { - return $this->overrideInjectorInstance[$overrideModuleClass]; + if (isset($this->overrideInjectorInstance[$moduleString])) { + return $this->overrideInjectorInstance[$moduleString]; } $injector = ContextInjector::getOverrideInstance($this->context, $this->overrideModule); - $this->overrideInjectorInstance[$overrideModuleClass] = $injector; + $this->overrideInjectorInstance[$moduleString] = $injector; return $injector; } diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index e8285be..109cab0 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -17,6 +17,7 @@ use Ray\RayDiForLaravel\Classes\IlluminateGreeting; use Ray\RayDiForLaravel\Classes\InjectableService; use Ray\RayDiForLaravel\Classes\NonInjectableService; +use Ray\RayDiForLaravel\Classes\OtherModule; use Ray\RayDiForLaravel\Classes\OverrideGreetingModule; class ApplicationTest extends TestCase @@ -118,6 +119,17 @@ public function testAlreadyResolvedOverrideModule(Application $application): voi $this->assertSame('Hello, override!', $instance->run()); } + /** @depends testOverrideModule */ + public function testSameModuleAndHasParentModule(Application $application): void + { + $application->overrideModule(new OverrideGreetingModule(new OtherModule())); + + $instance = $application->make(InjectableService::class); + + $this->assertInstanceOf(InjectableService::class, $instance); + $this->assertSame('Hello, override!Fake', $instance->run()); + } + /** @depends testOverrideModule */ public function testSetOtherOverrideModule(Application $application): void { diff --git a/tests/Classes/FakeFoo.php b/tests/Classes/FakeFoo.php new file mode 100644 index 0000000..d539397 --- /dev/null +++ b/tests/Classes/FakeFoo.php @@ -0,0 +1,13 @@ +fake)(); + $value = ($this->foo)(); - return $this->greeting->greet(); + return $this->greeting->greet() . $value; } } diff --git a/tests/Classes/Module.php b/tests/Classes/Module.php index 2bbe023..4158c2e 100644 --- a/tests/Classes/Module.php +++ b/tests/Classes/Module.php @@ -14,6 +14,7 @@ protected function configure(): void { $this->bind(GreetingInterface::class)->to(RayGreeting::class)->in(Scope::SINGLETON); $this->bind(FakeInterface::class)->to(Fake::class); + $this->bind(FooInterface::class)->to(Foo::class); $this->bindInterceptor( $this->matcher->any(), $this->matcher->annotatedWith(StringDecorator::class), diff --git a/tests/Classes/OtherModule.php b/tests/Classes/OtherModule.php new file mode 100644 index 0000000..3e28953 --- /dev/null +++ b/tests/Classes/OtherModule.php @@ -0,0 +1,15 @@ +bind(FooInterface::class)->to(FakeFoo::class); + } +} From 1d0356675f030c55b5c95266d5d89b67d65369a8 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Wed, 22 Mar 2023 21:37:07 +0900 Subject: [PATCH 11/14] Coverage 100% --- src/Application.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Application.php b/src/Application.php index 41f37b0..c9bba84 100644 --- a/src/Application.php +++ b/src/Application.php @@ -93,14 +93,15 @@ private function getInjector(): InjectorInterface return $this->injector; } - $moduleString = md5((string) $this->overrideModule); + $currentModuleString = md5((string) $this->overrideModule); - if (isset($this->overrideInjectorInstance[$moduleString])) { - return $this->overrideInjectorInstance[$moduleString]; + if (isset($this->overrideInjectorInstance[$currentModuleString])) { + return $this->overrideInjectorInstance[$currentModuleString]; } $injector = ContextInjector::getOverrideInstance($this->context, $this->overrideModule); - $this->overrideInjectorInstance[$moduleString] = $injector; + $newModuleString = md5((string) $this->overrideModule); + $this->overrideInjectorInstance[$newModuleString] = $injector; return $injector; } From 59ced62372452c45efaef4c1c08ccd3c7dd5783b Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Tue, 28 Mar 2023 17:51:33 +0900 Subject: [PATCH 12/14] Use object hash --- src/Application.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Application.php b/src/Application.php index c9bba84..000bab0 100644 --- a/src/Application.php +++ b/src/Application.php @@ -93,14 +93,14 @@ private function getInjector(): InjectorInterface return $this->injector; } - $currentModuleString = md5((string) $this->overrideModule); + $currentModuleString = spl_object_hash($this->overrideModule); if (isset($this->overrideInjectorInstance[$currentModuleString])) { return $this->overrideInjectorInstance[$currentModuleString]; } $injector = ContextInjector::getOverrideInstance($this->context, $this->overrideModule); - $newModuleString = md5((string) $this->overrideModule); + $newModuleString = spl_object_hash($this->overrideModule); $this->overrideInjectorInstance[$newModuleString] = $injector; return $injector; From c058571c4c1faa98bcfe27bd2dd0d5dbefed5ef3 Mon Sep 17 00:00:00 2001 From: tsuchiya Date: Tue, 28 Mar 2023 19:09:06 +0900 Subject: [PATCH 13/14] Caching override injector for current override module --- src/Application.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Application.php b/src/Application.php index 000bab0..6679fd9 100644 --- a/src/Application.php +++ b/src/Application.php @@ -27,8 +27,7 @@ class Application extends \Illuminate\Foundation\Application private AbstractModule|null $overrideModule = null; - /** @var array */ - private array $overrideInjectorInstance = []; + private InjectorInterface|null $overrideInjectorInstance = null; public function __construct(string $basePath, AbstractInjectorContext $injectorContext) { @@ -79,12 +78,13 @@ public function flush() $this->overrideModule = null; $this->abstractsResolvedByRay = []; - $this->overrideInjectorInstance = []; + $this->overrideInjectorInstance = null; } public function overrideModule(AbstractModule $module): void { $this->overrideModule = $module; + $this->overrideInjectorInstance = null; } private function getInjector(): InjectorInterface @@ -93,15 +93,12 @@ private function getInjector(): InjectorInterface return $this->injector; } - $currentModuleString = spl_object_hash($this->overrideModule); - - if (isset($this->overrideInjectorInstance[$currentModuleString])) { - return $this->overrideInjectorInstance[$currentModuleString]; + if ($this->overrideInjectorInstance !== null) { + return $this->overrideInjectorInstance; } $injector = ContextInjector::getOverrideInstance($this->context, $this->overrideModule); - $newModuleString = spl_object_hash($this->overrideModule); - $this->overrideInjectorInstance[$newModuleString] = $injector; + $this->overrideInjectorInstance = $injector; return $injector; } From cb2fb4aa8158917b96b1bbc87b4edab11c9b946a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 28 Mar 2023 21:16:26 +0900 Subject: [PATCH 14/14] Update README.ja.md --- README.ja.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.ja.md b/README.ja.md index 4b7f632..0196164 100644 --- a/README.ja.md +++ b/README.ja.md @@ -90,9 +90,8 @@ apcu拡張が有効な場合インジェクターをキャッシュします。 ### モジュールのオーバーライド -テスト実行時にはテストケースによって束縛を変更したい場合があります。 - -以下のように、テストクラスで `Ray\RayDiForLaravel\Testing\OverrideModule` を利用し、`$this->overrideModule` を呼び出してください。 +テストケースによって束縛を変更したい場合があります。 +その場合は、`Ray\RayDiForLaravel\Testing\OverrideModule`を利用し、テストクラスで`$this->overrideModule` を呼び出してください。 ```php use Tests\TestCase;