Skip to content

Commit 3687eb4

Browse files
committed
Throw error when entry didn't register any services [Closes #10]
1 parent 03fb6a4 commit 3687eb4

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,7 @@ such as decorator.
156156

157157
You can enforce registration in configuration phase
158158
by 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.

src/AutoDI/DI/AutoDIExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Fmasa\AutoDI\ClassList;
88
use Fmasa\AutoDI\Exceptions\IncompleteServiceDefinition;
9+
use Fmasa\AutoDI\Exceptions\NoServiceRegistered;
910
use Nette;
1011
use Nette\DI\CompilerExtension;
1112
use 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
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

tests/DI/AutoDIExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Fmasa\AutoDI\DI;
66

77
use Fmasa\AutoDI\Exceptions\IncompleteServiceDefinition;
8+
use Fmasa\AutoDI\Exceptions\NoServiceRegistered;
89
use Nette\Configurator;
910
use Nette\DI\Container;
1011
use 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();

tests/DI/alreadyRegistered.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ services:
33
- Fmasa\AutoDI\Tests\Dir01\SimpleService2
44

55
autoDI:
6+
errorOnNotMatchedDefinitions: false
67

78
services:
89
- implement: Fmasa\AutoDI\Tests\Dir01\ISimpleServiceFactory

tests/DI/noRegisteredService.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
- Fmasa\AutoDI\Tests\Dir01\SimpleService2
3+
4+
autoDI:
5+
services:
6+
- class: Fmasa\AutoDI\Tests\Dir01\SimpleService2

tests/DI/onConfiguration.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ autoDI:
33
- class: Fmasa\AutoDI\Tests\Dir01\SimpleService
44
tags: [ onCompilation ]
55
- class: Fmasa\AutoDI\Tests\Dir02\SimpleService
6+
errorOnNotMatchedDefinitions: false
67

78
extensions:
89
autoDI: Fmasa\AutoDI\DI\AutoDIExtension

0 commit comments

Comments
 (0)