From d6eadfedea16775bef74ec576e7bd7e83989f783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Ma=C5=A1a?= Date: Sun, 24 Dec 2017 01:44:36 +0100 Subject: [PATCH 1/4] Add to register services earlier --- src/AutoDI/DI/AutoDIExtension.php | 31 +++++++++++++++++++++++++++---- tests/DI/AutoDIExtensionTest.php | 15 +++++++++++++++ tests/DI/onConfiguration.neon | 18 ++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 tests/DI/onConfiguration.neon diff --git a/src/AutoDI/DI/AutoDIExtension.php b/src/AutoDI/DI/AutoDIExtension.php index 6b6fda9..d181c73 100644 --- a/src/AutoDI/DI/AutoDIExtension.php +++ b/src/AutoDI/DI/AutoDIExtension.php @@ -11,6 +11,7 @@ class AutoDIExtension extends CompilerExtension { private $defaults = [ + 'registerOnConfiguration' => FALSE, 'directories' => [ '%appDir%', ], @@ -20,11 +21,33 @@ class AutoDIExtension extends CompilerExtension public function beforeCompile() { + if ( ! $this->shouldRegisterOnConfiguration()) { + $this->registerServices(); + } + } + + public function loadConfiguration() + { + if ($this->shouldRegisterOnConfiguration()) { + $this->registerServices(); + } + } + + /** + * @return bool + */ + private function shouldRegisterOnConfiguration() + { + return (bool) $this->getConfig($this->defaults)['registerOnConfiguration']; + } + + private function registerServices() + { $config = $this->getConfig($this->defaults); $robotLoader = new RobotLoader(); - foreach($config['directories'] as $directory) { + foreach ($config['directories'] as $directory) { $robotLoader->addDirectory($directory); } @@ -37,11 +60,11 @@ public function beforeCompile() $builder = $this->getContainerBuilder(); - foreach($config['services'] as $service) { + foreach ($config['services'] as $service) { list($field, $matchingClasses) = $this->getClasses($service, $classes); - if(isset($service['exclude'])) { + if (isset($service['exclude'])) { $excluded = $service['exclude']; $matchingClasses = $this->removeExcludedClasses($matchingClasses, is_string($excluded) ? [$excluded] : $excluded); unset($service['exclude']); @@ -65,7 +88,7 @@ public function beforeCompile() } } - /** + /** * @param array $service * @param ClassList $classes * @return array [definition field, Class list] diff --git a/tests/DI/AutoDIExtensionTest.php b/tests/DI/AutoDIExtensionTest.php index bc03756..9b709fb 100644 --- a/tests/DI/AutoDIExtensionTest.php +++ b/tests/DI/AutoDIExtensionTest.php @@ -91,6 +91,21 @@ public function testExcludePatternList() $this->assertNull($container->getByType(Tests\Dir02\SimpleService::class, false)); } + /** + * There are 2 instances of AutoDIExtension registered, first with registration before compilation + * and second with registration on configuration. When registering same service by both, + * only second extension should register it + */ + public function testRegisterOnConfiguration() + { + $container = $this->getContainer(__DIR__ . '/onConfiguration.neon'); + + $this->assertCount(1, $container->findByTag('onConfiguration')); + + // service registered before compilation + $this->assertCount(1, $container->findByType(Tests\Dir02\SimpleService::class)); + } + /** * @param string $configFile * @param string $appDir diff --git a/tests/DI/onConfiguration.neon b/tests/DI/onConfiguration.neon new file mode 100644 index 0000000..9990365 --- /dev/null +++ b/tests/DI/onConfiguration.neon @@ -0,0 +1,18 @@ +autoDI: + services: + - class: Fmasa\AutoDI\Tests\Dir01\SimpleService + tags: [ onCompilation ] + - class: Fmasa\AutoDI\Tests\Dir02\SimpleService + +extensions: + autoDI: Fmasa\AutoDI\DI\AutoDIExtension + autoDI2: Fmasa\AutoDI\DI\AutoDIExtension + +autoDI2: + registerOnConfiguration: true + services: + - class: Fmasa\AutoDI\Tests\Dir01\SimpleService + tags: [ onConfiguration ] + +di: + debugger: false From ba75d279927d7a5cbcaf15aaf3f364bec013b402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Ma=C5=A1a?= Date: Sun, 24 Dec 2017 01:44:55 +0100 Subject: [PATCH 2/4] Add test for decorator support --- tests/DI/AutoDIExtensionTest.php | 7 +++++++ tests/DI/decorator.neon | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/DI/decorator.neon diff --git a/tests/DI/AutoDIExtensionTest.php b/tests/DI/AutoDIExtensionTest.php index 9b709fb..786130b 100644 --- a/tests/DI/AutoDIExtensionTest.php +++ b/tests/DI/AutoDIExtensionTest.php @@ -106,6 +106,13 @@ public function testRegisterOnConfiguration() $this->assertCount(1, $container->findByType(Tests\Dir02\SimpleService::class)); } + public function testWorksWithNetteDIDecorator() + { + $container = $this->getContainer(__DIR__ . '/decorator.neon'); + + $this->assertCount(1, $container->findByTag('decorated')); + } + /** * @param string $configFile * @param string $appDir diff --git a/tests/DI/decorator.neon b/tests/DI/decorator.neon new file mode 100644 index 0000000..790af00 --- /dev/null +++ b/tests/DI/decorator.neon @@ -0,0 +1,11 @@ +autoDI: + registerOnConfiguration: true + services: + - class: Fmasa\AutoDI\Tests\Dir01\SimpleService + +decorator: + Fmasa\AutoDI\Tests\Dir01\SimpleService: + tags: [ decorated ] + +di: + debugger: false From 71bc1dfb90d00309f4847d842b8ad2da3ac19f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Ma=C5=A1a?= Date: Sun, 24 Dec 2017 01:50:26 +0100 Subject: [PATCH 3/4] Mention option in docs --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index ebbafef..d0e30f5 100644 --- a/README.md +++ b/README.md @@ -136,3 +136,14 @@ autoDI: - %appDir% - %appDir%/../vendor ``` + +## Register services on configuration + +Compiler extensions such as AutoDIExtension manipulates the DI container +in two phases (configuration loading and before compilation). +By default this extension registers all services before compilation. +This may not be optimal if you wan't to use this extension with other extensions +such as decorator. + +You can enforce registration in configuration phase +by setting `registerOnConfiguration` option to true. From 69ba0b226c73fb3beb22e44701cb652658897ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Ma=C5=A1a?= Date: Sun, 24 Dec 2017 01:51:33 +0100 Subject: [PATCH 4/4] Fix typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0e30f5..4706dc4 100644 --- a/README.md +++ b/README.md @@ -118,10 +118,10 @@ autoDI: tags: [ my.auto.service ] services: - # theese services will have tag my.auto.service + # these services will have tag my.auto.service - class: App\Model\Repositories\** - # theese services will have only tag eventBus.subscriber + # these services will have only tag eventBus.subscriber - class: app\Model\Subscribers\** tags: [ eventBus.subscriber ] ```