Skip to content

Commit 7862a33

Browse files
feature #62800 [HttpKernel] Add support for bundles as compiler passes (yceruto)
This PR was merged into the 8.1 branch. Discussion ---------- [HttpKernel] Add support for bundles as compiler passes | Q | A | ------------- | --- | Branch? | 8.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #62470 | License | MIT Adds a bundle-level compiler pass to hook into the container compilation phase: ```php class MyBundle extends AbstractBundle implements CompilerPassInterface { public function process(ContainerBuilder $container): void { // ... } } ``` Commits ------- b029905 [HttpKernel] Add support for bundles as compiler passes
2 parents d15ed2e + b029905 commit 7862a33

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
8.1
5+
---
6+
7+
* Add support for bundles as compiler pass
8+
49
8.0
510
---
611

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ protected function prepareContainer(ContainerBuilder $container): void
616616
if ($this->debug) {
617617
$container->addObjectResource($bundle);
618618
}
619+
620+
if ($bundle instanceof CompilerPassInterface) {
621+
$container->addCompilerPass($bundle, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000);
622+
}
619623
}
620624

621625
foreach ($this->bundles as $bundle) {

src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
namespace Symfony\Component\HttpKernel\Tests\Bundle;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
1516
use Symfony\Component\HttpKernel\Bundle\Bundle;
17+
use Symfony\Component\HttpKernel\Kernel;
18+
use Symfony\Component\HttpKernel\Tests\Fixtures\BundleCompilerPass\BundleAsCompilerPassBundle;
1619
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;
1720

1821
class BundleTest extends TestCase
@@ -43,6 +46,29 @@ public function testBundleNameCanBeExplicitlyProvided()
4346
$this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
4447
$this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
4548
}
49+
50+
public function testBundleAsCompilerPass()
51+
{
52+
$kernel = new class('test', true) extends Kernel {
53+
public function registerBundles(): iterable
54+
{
55+
yield new BundleAsCompilerPassBundle();
56+
}
57+
58+
public function registerContainerConfiguration(LoaderInterface $loader): void
59+
{
60+
}
61+
62+
public function getProjectDir(): string
63+
{
64+
return sys_get_temp_dir().'/bundle_as_compiler_pass';
65+
}
66+
};
67+
68+
$kernel->boot();
69+
70+
$this->assertTrue($kernel->getContainer()->has('foo'));
71+
}
4672
}
4773

4874
class NamedBundle extends Bundle
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures\BundleCompilerPass;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
17+
18+
class BundleAsCompilerPassBundle extends AbstractBundle implements CompilerPassInterface
19+
{
20+
public function process(ContainerBuilder $container): void
21+
{
22+
$container->register('foo', \stdClass::class)
23+
->setPublic(true);
24+
}
25+
}

0 commit comments

Comments
 (0)