Skip to content

Commit f738392

Browse files
authored
Merge pull request #6 from fmasa/register-on-configuration
Add option to register services on configuration
2 parents 052ee2b + 69ba0b2 commit f738392

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ autoDI:
118118
tags: [ my.auto.service ]
119119
120120
services:
121-
# theese services will have tag my.auto.service
121+
# these services will have tag my.auto.service
122122
- class: App\Model\Repositories\**
123123
124-
# theese services will have only tag eventBus.subscriber
124+
# these services will have only tag eventBus.subscriber
125125
- class: app\Model\Subscribers\**
126126
tags: [ eventBus.subscriber ]
127127
```
@@ -136,3 +136,14 @@ autoDI:
136136
- %appDir%
137137
- %appDir%/../vendor
138138
```
139+
140+
## Register services on configuration
141+
142+
Compiler extensions such as AutoDIExtension manipulates the DI container
143+
in two phases (configuration loading and before compilation).
144+
By default this extension registers all services before compilation.
145+
This may not be optimal if you wan't to use this extension with other extensions
146+
such as decorator.
147+
148+
You can enforce registration in configuration phase
149+
by setting `registerOnConfiguration` option to true.

src/AutoDI/DI/AutoDIExtension.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AutoDIExtension extends CompilerExtension
1111
{
1212

1313
private $defaults = [
14+
'registerOnConfiguration' => FALSE,
1415
'directories' => [
1516
'%appDir%',
1617
],
@@ -20,11 +21,33 @@ class AutoDIExtension extends CompilerExtension
2021

2122
public function beforeCompile()
2223
{
24+
if ( ! $this->shouldRegisterOnConfiguration()) {
25+
$this->registerServices();
26+
}
27+
}
28+
29+
public function loadConfiguration()
30+
{
31+
if ($this->shouldRegisterOnConfiguration()) {
32+
$this->registerServices();
33+
}
34+
}
35+
36+
/**
37+
* @return bool
38+
*/
39+
private function shouldRegisterOnConfiguration()
40+
{
41+
return (bool) $this->getConfig($this->defaults)['registerOnConfiguration'];
42+
}
43+
44+
private function registerServices()
45+
{
2346
$config = $this->getConfig($this->defaults);
2447

2548
$robotLoader = new RobotLoader();
2649

27-
foreach($config['directories'] as $directory) {
50+
foreach ($config['directories'] as $directory) {
2851
$robotLoader->addDirectory($directory);
2952
}
3053

@@ -37,11 +60,11 @@ public function beforeCompile()
3760

3861
$builder = $this->getContainerBuilder();
3962

40-
foreach($config['services'] as $service) {
63+
foreach ($config['services'] as $service) {
4164

4265
list($field, $matchingClasses) = $this->getClasses($service, $classes);
4366

44-
if(isset($service['exclude'])) {
67+
if (isset($service['exclude'])) {
4568
$excluded = $service['exclude'];
4669
$matchingClasses = $this->removeExcludedClasses($matchingClasses, is_string($excluded) ? [$excluded] : $excluded);
4770
unset($service['exclude']);
@@ -65,7 +88,7 @@ public function beforeCompile()
6588
}
6689
}
6790

68-
/**
91+
/**
6992
* @param array $service
7093
* @param ClassList $classes
7194
* @return array [definition field, Class list]

tests/DI/AutoDIExtensionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ public function testExcludePatternList()
9191
$this->assertNull($container->getByType(Tests\Dir02\SimpleService::class, false));
9292
}
9393

94+
/**
95+
* There are 2 instances of AutoDIExtension registered, first with registration before compilation
96+
* and second with registration on configuration. When registering same service by both,
97+
* only second extension should register it
98+
*/
99+
public function testRegisterOnConfiguration()
100+
{
101+
$container = $this->getContainer(__DIR__ . '/onConfiguration.neon');
102+
103+
$this->assertCount(1, $container->findByTag('onConfiguration'));
104+
105+
// service registered before compilation
106+
$this->assertCount(1, $container->findByType(Tests\Dir02\SimpleService::class));
107+
}
108+
109+
public function testWorksWithNetteDIDecorator()
110+
{
111+
$container = $this->getContainer(__DIR__ . '/decorator.neon');
112+
113+
$this->assertCount(1, $container->findByTag('decorated'));
114+
}
115+
94116
/**
95117
* @param string $configFile
96118
* @param string $appDir

tests/DI/decorator.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
autoDI:
2+
registerOnConfiguration: true
3+
services:
4+
- class: Fmasa\AutoDI\Tests\Dir01\SimpleService
5+
6+
decorator:
7+
Fmasa\AutoDI\Tests\Dir01\SimpleService:
8+
tags: [ decorated ]
9+
10+
di:
11+
debugger: false

tests/DI/onConfiguration.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
autoDI:
2+
services:
3+
- class: Fmasa\AutoDI\Tests\Dir01\SimpleService
4+
tags: [ onCompilation ]
5+
- class: Fmasa\AutoDI\Tests\Dir02\SimpleService
6+
7+
extensions:
8+
autoDI: Fmasa\AutoDI\DI\AutoDIExtension
9+
autoDI2: Fmasa\AutoDI\DI\AutoDIExtension
10+
11+
autoDI2:
12+
registerOnConfiguration: true
13+
services:
14+
- class: Fmasa\AutoDI\Tests\Dir01\SimpleService
15+
tags: [ onConfiguration ]
16+
17+
di:
18+
debugger: false

0 commit comments

Comments
 (0)