Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55fdf96

Browse files
committed
Allowed extensions to inline compiler passes
1 parent a630d87 commit 55fdf96

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Extension\CompilerExtensionInterface;
7+
8+
/**
9+
* A pass to execute inline compiler passes in extensions.
10+
*
11+
* @author Wouter J <[email protected]>
12+
*/
13+
class InlineCompilePass implements CompilerPassInterface
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function process(ContainerBuilder $container)
19+
{
20+
foreach ($container->getExtensions() as $extension) {
21+
if (!$extension instanceof CompilerExtensionInterface) {
22+
continue;
23+
}
24+
25+
$extension->compile($container);
26+
}
27+
}
28+
}

src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function __construct()
4545
$this->mergePass = new MergeExtensionConfigurationPass();
4646

4747
$this->optimizationPasses = array(
48+
new InlineCompilePass(),
4849
new ResolveDefinitionTemplatesPass(),
4950
new DecoratorServicePass(),
5051
new ResolveParameterPlaceHoldersPass(),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Extension;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
7+
/**
8+
* An interface which must be implemented by extensions providing compile functionality.
9+
*
10+
* Compilers can modify/add/delete definitions when the container is compiled.
11+
*
12+
* @author Wouter J <[email protected]>
13+
*/
14+
interface CompilerExtensionInterface
15+
{
16+
/**
17+
* This method is executed by a compiler pass.
18+
*
19+
* @param ContainerBuilder $container
20+
*/
21+
public function compile(ContainerBuilder $container);
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\InlineCompilePass;
6+
7+
/**
8+
* @author Wouter J <[email protected]>
9+
*/
10+
class InlineCompilePassTest extends \PHPUnit_Framework_TestCase
11+
{
12+
private $container;
13+
private $pass;
14+
15+
public function setUp()
16+
{
17+
$this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
18+
$this->pass = new InlineCompilePass();
19+
}
20+
21+
public function testProcess()
22+
{
23+
$extension1 = $this->createExtensionMock(true);
24+
$extension1->expects($this->once())->method('compile');
25+
$extension2 = $this->createExtensionMock(false);
26+
$extension3 = $this->createExtensionMock(false);
27+
$extension4 = $this->createExtensionMock(true);
28+
$extension4->expects($this->once())->method('compile');
29+
30+
$this->container->expects($this->any())
31+
->method('getExtensions')
32+
->will($this->returnValue(array($extension1, $extension2, $extension3, $extension4)))
33+
;
34+
35+
$this->pass->process($this->container);
36+
}
37+
38+
private function createExtensionMock($hasInlineCompile)
39+
{
40+
return $this->getMock('Symfony\Component\DependencyInjection\Extension\\'.($hasInlineCompile ? 'CompilerExtensionInterface' : 'ExtensionInterface'));
41+
}
42+
}

0 commit comments

Comments
 (0)