Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

8.1
---

* Add support for bundles as compiler pass

8.0
---

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ protected function prepareContainer(ContainerBuilder $container): void
if ($this->debug) {
$container->addObjectResource($bundle);
}

if ($bundle instanceof CompilerPassInterface) {
$container->addCompilerPass($bundle, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000);
}
}

foreach ($this->bundles as $bundle) {
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Symfony\Component\HttpKernel\Tests\Bundle;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Tests\Fixtures\BundleCompilerPass\BundleAsCompilerPassBundle;
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;

class BundleTest extends TestCase
Expand Down Expand Up @@ -43,6 +46,29 @@ public function testBundleNameCanBeExplicitlyProvided()
$this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace());
$this->assertSame('ExplicitlyNamedBundle', $bundle->getName());
}

public function testBundleAsCompilerPass()
{
$kernel = new class('test', true) extends Kernel {
public function registerBundles(): iterable
{
yield new BundleAsCompilerPassBundle();
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
}

public function getProjectDir(): string
{
return sys_get_temp_dir().'/bundle_as_compiler_pass';
}
};

$kernel->boot();

$this->assertTrue($kernel->getContainer()->has('foo'));
}
}

class NamedBundle extends Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\Fixtures\BundleCompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class BundleAsCompilerPassBundle extends AbstractBundle implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$container->register('foo', \stdClass::class)
->setPublic(true);
}
}
Loading