8000 feature #20107 Added a build method to the kernel to replace Bundle::… · hhamon/symfony@99f60dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 99f60dc

Browse files
committed
feature symfony#20107 Added a build method to the kernel to replace Bundle::build() (iltar)
This PR was merged into the 3.3-dev branch. Discussion ---------- Added a build method to the kernel to replace Bundle::build() | Q | A | | --- | --- | | Branch? | master | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | symfony#20099 | | License | MIT | | Doc PR | ~ | Adds a DX method to make it easier to omit using an AppBundle in your application. **Old situation** ``` php // src/AppBundle.php class AppBundle extends Bundle { public function build(ContainerBuilder $container) { $container->addCompilerPass(new SomeCompilerPass()); $container->addCompilerPass(new AnotherCompilerPass()); $container->addCompilerPass(new YetAnotherCompilerPass()); } } // src/DependencyInjection/AppExtension.php class AppExtension extends Extension { public function load(array $config, ContainerBuilder $container) { $binary = ExecutableResolver::getPath($container->getParameter('kernel.root_dir').'/../'); $snappyConfig = ['pdf' => ['binary' => realpath($binary)]]; $container->prependExtensionConfig('knp_snappy', $snappyConfig); } } ``` **New situation** ``` php // rm src/AppBundle.php // rm src/DependencyInjection/AppExtension.php // app/AppKernel.php class AppKernel extends Kernel { protected function build(ContainerBuilder $container) { $binary = ExecutableResolver::getPath($container->getParameter('kernel.root_dir').'/../'); $snappyConfig = ['pdf' => ['binary' => realpath($binary)]]; $container->prependExtensionConfig('knp_snappy', $snappyConfig); $container->addCompilerPass(new SomeCompilerPass()); $container->addCompilerPass(new AnotherCompilerPass()); $container->addCompilerPass(new YetAnotherCompilerPass()); } } ``` Still missing tests, wondering if worth adding in this state first. Commits ------- 62e80fc Added build and class cache to kernel
2 parents 0e92e0a + 62e80fc commit 99f60dc

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,17 @@ protected function initializeBundles()
466466
}
467467
}
468468

469+
/**
470+
* The extension point similar to the Bundle::build() method.
471+
*
472+
* Use this method to register compiler passes and manipulate the container during the building process.
473+
*
474+
* @param ContainerBuilder $container
475+
*/
476+
protected function build(ContainerBuilder $container)
477+
{
478+
}
479+
469480
/**
470481
* Gets the container class.
471482
*
@@ -625,10 +636,13 @@ protected function prepareContainer(ContainerBuilder $container)
625636
$container->addObjectResource($bundle);
626637
}
627638
}
639+
628640
foreach ($this->bundles as $bundle) {
629641
$bundle->build($container);
630642
}
631643

644+
$this->build($container);
645+
632646
// ensure these extensions are implicitly loaded
633647
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
634648
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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;
13 10000 +
14+
use Symfony\Component\Config\Loader\LoaderInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\HttpKernel\Kernel;
17+
18+
class KernelWithoutBundles extends Kernel
19+
{
20+
public function registerBundles()
21+
{
22+
return array();
23+
}
24+
25+
public function registerContainerConfiguration(LoaderInterface $loader)
26+
{
27+
}
28+
29+
protected function build(ContainerBuilder $container)
30+
{
31+
$container->setParameter('test_executed', true);
32+
}
33+
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\HttpFoundation\Response;
2222
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
2323
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
24+
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelWithoutBundles;
2425

2526
class KernelTest extends TestCase
2627
{
@@ -725,6 +726,14 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
725726
$kernel->terminate(Request::create('/'), new Response());
726727
}
727728

729+
public function testKernelWithoutBundles()
730+
{
731+
$kernel = new KernelWithoutBundles('test', true);
732+
$kernel->boot();
733+
734+
$this->assertTrue($kernel->getContainer()->getParameter('test_executed'));
735+
}
736+
728737
/**
729738
* Returns a mock for the BundleInterface.
730739
*

0 commit comments

Comments
 (0)
0