From 3687eb4c95d53f4646bb0f29c525c6a7182e8a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Ma=C5=A1a?= Date: Sun, 23 Feb 2020 16:58:27 +0100 Subject: [PATCH] Throw error when entry didn't register any services [Closes #10] --- README.md | 4 ++++ src/AutoDI/DI/AutoDIExtension.php | 6 ++++++ src/AutoDI/Exceptions/NoServiceRegistered.php | 21 +++++++++++++++++++ tests/DI/AutoDIExtensionTest.php | 8 +++++++ tests/DI/alreadyRegistered.neon | 1 + tests/DI/noRegisteredService.neon | 6 ++++++ tests/DI/onConfiguration.neon | 1 + 7 files changed, 47 insertions(+) create mode 100644 src/AutoDI/Exceptions/NoServiceRegistered.php create mode 100644 tests/DI/noRegisteredService.neon diff --git a/README.md b/README.md index cd31369..9a9b61b 100644 --- a/README.md +++ b/README.md @@ -156,3 +156,7 @@ such as decorator. You can enforce registration in configuration phase by setting `registerOnConfiguration` option to true. + +When no service is registered for configuration entry, either because no class/interface +matches the pattern or all matched services were already registered in container, exception +is thrown. This check can be disabled by setting `errorOnNotMatchedDefinitions` option to false. diff --git a/src/AutoDI/DI/AutoDIExtension.php b/src/AutoDI/DI/AutoDIExtension.php index e6c68cc..215653e 100644 --- a/src/AutoDI/DI/AutoDIExtension.php +++ b/src/AutoDI/DI/AutoDIExtension.php @@ -6,6 +6,7 @@ use Fmasa\AutoDI\ClassList; use Fmasa\AutoDI\Exceptions\IncompleteServiceDefinition; +use Fmasa\AutoDI\Exceptions\NoServiceRegistered; use Nette; use Nette\DI\CompilerExtension; use Nette\Loaders\RobotLoader; @@ -17,6 +18,7 @@ public function getConfigSchema() : Nette\Schema\Schema { return Expect::structure([ 'services' => Expect::listOf(Expect::array()), + 'errorOnNotMatchedDefinitions' => Expect::bool(true), 'registerOnConfiguration' => Expect::bool(false), 'directories' => Expect::listOf(Expect::string())->default([$this->getContainerBuilder()->parameters['appDir']]), 'defaults' => Expect::array(), @@ -82,6 +84,10 @@ private function registerServices(): void return $service; }, $matchingClasses); + if ($config->errorOnNotMatchedDefinitions && count($services) === 0) { + throw NoServiceRegistered::byPattern($field); + } + $this->compiler->loadDefinitionsFromConfig($services); } } diff --git a/src/AutoDI/Exceptions/NoServiceRegistered.php b/src/AutoDI/Exceptions/NoServiceRegistered.php new file mode 100644 index 0000000..8cb5e13 --- /dev/null +++ b/src/AutoDI/Exceptions/NoServiceRegistered.php @@ -0,0 +1,21 @@ +getContainer(__DIR__ . '/missingKey.neon'); } + public function testExceptionIsThrownIfThereAreNoServicesRegisteredForEntry() : void + { + $this->expectException(NoServiceRegistered::class); + + $this->getContainer(__DIR__ . '/noRegisteredService.neon'); + } + private function getContainer(string $configFile, string $appDir = __DIR__ . '/../fixtures/app'): Container { $configurator = new Configurator(); diff --git a/tests/DI/alreadyRegistered.neon b/tests/DI/alreadyRegistered.neon index 87ed916..9f66125 100644 --- a/tests/DI/alreadyRegistered.neon +++ b/tests/DI/alreadyRegistered.neon @@ -3,6 +3,7 @@ services: - Fmasa\AutoDI\Tests\Dir01\SimpleService2 autoDI: + errorOnNotMatchedDefinitions: false services: - implement: Fmasa\AutoDI\Tests\Dir01\ISimpleServiceFactory diff --git a/tests/DI/noRegisteredService.neon b/tests/DI/noRegisteredService.neon new file mode 100644 index 0000000..dfb2c39 --- /dev/null +++ b/tests/DI/noRegisteredService.neon @@ -0,0 +1,6 @@ +services: + - Fmasa\AutoDI\Tests\Dir01\SimpleService2 + +autoDI: + services: + - class: Fmasa\AutoDI\Tests\Dir01\SimpleService2 diff --git a/tests/DI/onConfiguration.neon b/tests/DI/onConfiguration.neon index 9990365..89b42b1 100644 --- a/tests/DI/onConfiguration.neon +++ b/tests/DI/onConfiguration.neon @@ -3,6 +3,7 @@ autoDI: - class: Fmasa\AutoDI\Tests\Dir01\SimpleService tags: [ onCompilation ] - class: Fmasa\AutoDI\Tests\Dir02\SimpleService + errorOnNotMatchedDefinitions: false extensions: autoDI: Fmasa\AutoDI\DI\AutoDIExtension