New in Symfony 3.3: Kernel Build Method
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Lynn van der Berg
in #20107.
The bundle system has been an integral part of the Symfony framework since day one. However, some developers prefer to not use bundles in their applications except Symfony's built-in bundles and third-party bundles.
In Symfony 3.3 we've decided to simplify this by adding a new build()
method
to the Kernel
class. This method allows to register compiler passes and
manipulate the container during the building process. That's why this method
makes it easy to avoid using any bundle, even the default AppBundle.
Consider the following initial situation where you enable some compiler passes in your AppBundle and then define a bundle extension to load some configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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/AppBundle/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);
}
}
In Symfony 3.3, you can remove the AppBundle entirely and define instead this
build()
method in your AppKernel
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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());
}
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
This feature is primarily useful when you want to modify certain 3rd party bundle services can be only done _after_ their extension is loaded.
https://stovepipe.systems/post/what-are-bundles-in-symfony