File tree Expand file tree Collapse file tree 7 files changed +47
-0
lines changed Expand file tree Collapse file tree 7 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -156,3 +156,7 @@ such as decorator.
156156
157157You can enforce registration in configuration phase
158158by setting `registerOnConfiguration` option to true.
159+
160+ When no service is registered for configuration entry, either because no class/interface
161+ matches the pattern or all matched services were already registered in container, exception
162+ is thrown. This check can be disabled by setting `errorOnNotMatchedDefinitions` option to false.
Original file line number Diff line number Diff line change 66
77use Fmasa \AutoDI \ClassList ;
88use Fmasa \AutoDI \Exceptions \IncompleteServiceDefinition ;
9+ use Fmasa \AutoDI \Exceptions \NoServiceRegistered ;
910use Nette ;
1011use Nette \DI \CompilerExtension ;
1112use Nette \Loaders \RobotLoader ;
@@ -17,6 +18,7 @@ public function getConfigSchema() : Nette\Schema\Schema
1718 {
1819 return Expect::structure ([
1920 'services ' => Expect::listOf (Expect::array ()),
21+ 'errorOnNotMatchedDefinitions ' => Expect::bool (true ),
2022 'registerOnConfiguration ' => Expect::bool (false ),
2123 'directories ' => Expect::listOf (Expect::string ())->default ([$ this ->getContainerBuilder ()->parameters ['appDir ' ]]),
2224 'defaults ' => Expect::array (),
@@ -82,6 +84,10 @@ private function registerServices(): void
8284 return $ service ;
8385 }, $ matchingClasses );
8486
87+ if ($ config ->errorOnNotMatchedDefinitions && count ($ services ) === 0 ) {
88+ throw NoServiceRegistered::byPattern ($ field );
89+ }
90+
8591 $ this ->compiler ->loadDefinitionsFromConfig ($ services );
8692 }
8793 }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Fmasa \AutoDI \Exceptions ;
6+
7+ use Exception ;
8+
9+ final class NoServiceRegistered extends Exception
10+ {
11+ /**
12+ * @internal This constructor is not part of public API and may change between versions
13+ */
14+ public static function byPattern (string $ pattern ) : self
15+ {
16+ return new self (
17+ "No services were matched by registered using $ pattern \n"
18+ . 'services with name matching the pattern were either not found or already registered by another extension '
19+ );
20+ }
21+ }
Original file line number Diff line number Diff line change 55namespace Fmasa \AutoDI \DI ;
66
77use Fmasa \AutoDI \Exceptions \IncompleteServiceDefinition ;
8+ use Fmasa \AutoDI \Exceptions \NoServiceRegistered ;
89use Nette \Configurator ;
910use Nette \DI \Container ;
1011use Fmasa \AutoDI \Tests ;
@@ -132,6 +133,13 @@ public function testServiceWithoutImplementAndClassKeyThrowsException() : void
132133 $ this ->getContainer (__DIR__ . '/missingKey.neon ' );
133134 }
134135
136+ public function testExceptionIsThrownIfThereAreNoServicesRegisteredForEntry () : void
137+ {
138+ $ this ->expectException (NoServiceRegistered::class);
139+
140+ $ this ->getContainer (__DIR__ . '/noRegisteredService.neon ' );
141+ }
142+
135143 private function getContainer (string $ configFile , string $ appDir = __DIR__ . '/../fixtures/app ' ): Container
136144 {
137145 $ configurator = new Configurator ();
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ services:
33 - Fmasa\AutoDI\Tests\Dir01\SimpleService2
44
55autoDI :
6+ errorOnNotMatchedDefinitions : false
67
78 services :
89 - implement : Fmasa\AutoDI\Tests\Dir01\ISimpleServiceFactory
Original file line number Diff line number Diff line change 1+ services :
2+ - Fmasa\AutoDI\Tests\Dir01\SimpleService2
3+
4+ autoDI :
5+ services :
6+ - class : Fmasa\AutoDI\Tests\Dir01\SimpleService2
Original file line number Diff line number Diff line change 33 - class : Fmasa\AutoDI\Tests\Dir01\SimpleService
44 tags : [ onCompilation ]
55 - class : Fmasa\AutoDI\Tests\Dir02\SimpleService
6+ errorOnNotMatchedDefinitions : false
67
78extensions :
89 autoDI : Fmasa\AutoDI\DI\AutoDIExtension
You can’t perform that action at this time.
0 commit comments