Skip to content

Commit

Permalink
Throw error when entry didn't register any services [Closes #10]
Browse files Browse the repository at this point in the history
  • Loading branch information
fmasa committed Feb 23, 2020
1 parent 03fb6a4 commit 3687eb4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions src/AutoDI/DI/AutoDIExtension.php
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/AutoDI/Exceptions/NoServiceRegistered.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Fmasa\AutoDI\Exceptions;

use Exception;

final class NoServiceRegistered extends Exception
{
/**
* @internal This constructor is not part of public API and may change between versions
*/
public static function byPattern(string $pattern) : self
{
return new self(
"No services were matched by registered using $pattern \n"
. 'services with name matching the pattern were either not found or already registered by another extension'
);
}
}
8 changes: 8 additions & 0 deletions tests/DI/AutoDIExtensionTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Fmasa\AutoDI\DI;

use Fmasa\AutoDI\Exceptions\IncompleteServiceDefinition;
use Fmasa\AutoDI\Exceptions\NoServiceRegistered;
use Nette\Configurator;
use Nette\DI\Container;
use Fmasa\AutoDI\Tests;
Expand Down Expand Up @@ -132,6 +133,13 @@ public function testServiceWithoutImplementAndClassKeyThrowsException() : void
$this->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();
Expand Down
1 change: 1 addition & 0 deletions tests/DI/alreadyRegistered.neon
Expand Up @@ -3,6 +3,7 @@ services:
- Fmasa\AutoDI\Tests\Dir01\SimpleService2

autoDI:
errorOnNotMatchedDefinitions: false

services:
- implement: Fmasa\AutoDI\Tests\Dir01\ISimpleServiceFactory
Expand Down
6 changes: 6 additions & 0 deletions tests/DI/noRegisteredService.neon
@@ -0,0 +1,6 @@
services:
- Fmasa\AutoDI\Tests\Dir01\SimpleService2

autoDI:
services:
- class: Fmasa\AutoDI\Tests\Dir01\SimpleService2
1 change: 1 addition & 0 deletions tests/DI/onConfiguration.neon
Expand Up @@ -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
Expand Down

0 comments on commit 3687eb4

Please sign in to comment.