Skip to content

Commit 798d62a

Browse files
committed
Adding compiler pass support
1 parent 3955a8b commit 798d62a

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/AppKernel.php

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

55
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
66
use Symfony\Component\Config\Loader\LoaderInterface;
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\HttpKernel\Kernel;
910
use Symfony\Component\Routing\RouteCollectionBuilder;
@@ -33,6 +34,11 @@ class AppKernel extends Kernel
3334
*/
3435
private $fakedProjectDir;
3536

37+
/**
38+
* @var CompilerPassInterface[]
39+
*/
40+
private $compilerPasses = [];
41+
3642
/**
3743
* @param string $cachePrefix
3844
*/
@@ -141,4 +147,27 @@ public function loadRoutes(LoaderInterface $loader)
141147

142148
return $routes->build();
143149
}
150+
151+
/**
152+
* {@inheritdoc}
153+
*/
154+
protected function buildContainer()
155+
{
156+
$container = parent::buildContainer();
157+
158+
foreach ($this->compilerPasses as $pass) {
159+
$container->addCompilerPass($pass);
160+
}
161+
162+
return $container;
163+
}
164+
165+
166+
/**
167+
* @param CompilerPassInterface[] $compilerPasses
168+
*/
169+
public function addCompilerPasses(array $compilerPasses)
170+
{
171+
$this->compilerPasses = $compilerPasses;
172+
}
144173
}

src/BaseBundleTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Nyholm\BundleTest;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
67
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
78
use Symfony\Component\HttpKernel\Kernel;
89

@@ -16,6 +17,11 @@ abstract class BaseBundleTestCase extends TestCase
1617
*/
1718
private $kernel;
1819

20+
/**
21+
* @var CompilerPassInterface[]
22+
*/
23+
private $compilerPasses = [];
24+
1925
/**
2026
* @return string
2127
*/
@@ -61,6 +67,7 @@ protected function createKernel()
6167

6268
$this->kernel = new $class(uniqid('cache'));
6369
$this->kernel->addBundle($this->getBundleClass());
70+
$this->kernel->addCompilerPasses($this->compilerPasses);
6471

6572
return $this->kernel;
6673
}
@@ -86,4 +93,12 @@ protected function tearDown()
8693
{
8794
$this->ensureKernelShutdown();
8895
}
96+
97+
/**
98+
* @param CompilerPassInterface $compilerPass
99+
*/
100+
protected function addCompilerPass(CompilerPassInterface $compilerPass)
101+
{
102+
$this->compilerPasses[] = $compilerPass;
103+
}
89104
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\CompilerPass;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
/**
9+
* @author Tobias Nyholm <[email protected]>
10+
*/
11+
class PublicServicePass implements CompilerPassInterface
12+
{
13+
/**
14+
* A regex to match the services that should be public
15+
* @var string
16+
*/
17+
private $regex;
18+
19+
/**
20+
* @param string $regex
21+
*/
22+
public function __construct($regex = '|.*|')
23+
{
24+
$this->regex = $regex;
25+
}
26+
27+
public function process(ContainerBuilder $container) {
28+
foreach ($container->getDefinitions() as $id => $definition) {
29+
if (preg_match($this->regex, $id)) {
30+
$definition->setPublic(true);
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)