File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 44
55use Symfony \Bundle \FrameworkBundle \FrameworkBundle ;
66use Symfony \Component \Config \Loader \LoaderInterface ;
7+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
78use Symfony \Component \DependencyInjection \ContainerBuilder ;
89use Symfony \Component \HttpKernel \Kernel ;
910use 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}
Original file line number Diff line number Diff line change 33namespace Nyholm \BundleTest ;
44
55use PHPUnit \Framework \TestCase ;
6+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
67use Symfony \Component \DependencyInjection \ResettableContainerInterface ;
78use 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments