Symfony News

New in Symfony 3.4: Defining compiler passes in the kernel

Nicolas Grekas

Contributed by
Nicolas Grekas
in #24257.

In Symfony 3.4, the application kernel can subscribe to events just by implementing EventSubscriberInterface and adding the methods to handle the events. Given that Symfony 4 will push bundle-less applications, in Symfony 3.4 we improved the application kernel to also allow defining compiler passes in it.

To do so, the kernel must implement CompilerPassInterface and include a method called process() where the compiler pass logic is defined:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// src/Kernel.php
namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel implements CompilerPassInterface
{
    use MicroKernelTrait;

    // ...

    public function process(ContainerBuilder $container)
    {
        // define here the code to manipulate the service container...

        // For example, define a new container parameter
        $container->setParameter('app.my_parameter', '...');
    }
}

The compiler pass defined in the kernel is of type PassConfig::TYPE_BEFORE_OPTIMIZATION and has a priority of -10000.


Be trained by Symfony experts - 2017-10-9 Paris - 2017-10-9 Paris - 2017-10-9 Lyon


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook