Skip to content

Commit

Permalink
Merge pull request #6 from fmasa/register-on-configuration
Browse files Browse the repository at this point in the history
Add option to register services on configuration
  • Loading branch information
fmasa authored Dec 24, 2017
2 parents 052ee2b + 69ba0b2 commit f738392
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
```
Expand All @@ -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.
31 changes: 27 additions & 4 deletions src/AutoDI/DI/AutoDIExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AutoDIExtension extends CompilerExtension
{

private $defaults = [
'registerOnConfiguration' => FALSE,
'directories' => [
'%appDir%',
],
Expand All @@ -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);
}

Expand All @@ -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']);
Expand All @@ -65,7 +88,7 @@ public function beforeCompile()
}
}

/**
/**
* @param array $service
* @param ClassList $classes
* @return array [definition field, Class list]
Expand Down
22 changes: 22 additions & 0 deletions tests/DI/AutoDIExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ 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));
}

public function testWorksWithNetteDIDecorator()
{
$container = $this->getContainer(__DIR__ . '/decorator.neon');

$this->assertCount(1, $container->findByTag('decorated'));
}

/**
* @param string $configFile
* @param string $appDir
Expand Down
11 changes: 11 additions & 0 deletions tests/DI/decorator.neon
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions tests/DI/onConfiguration.neon
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f738392

Please sign in to comment.