Lynn van der Berg
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());
    }
}
Published in #Living on the edge