Introducing Symfony Flex Loader

Symfony 4 and Flex is heading in direction of zero-setup configuration - no bundles, no extensions, no configuration. You already know how to get rid of Configuration. Flex now loads services instead of Extension class.

But it has an extra price, a lot of new boilerplate code in Kernel. Today you'll learn how to keep your Kernel Flex-ready and clean at the same time.

Can you spot, what extra directory is this Kernel loading...

<?php declare(strict_types=1);

// ...

final class AppKernel extends Kernel
{
    // ...

    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
    {
        $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
        $container->setParameter('container.dumper.inline_class_loader', true);
        $confDir = $this->getProjectDir() . '/config';
        $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
        $loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
        $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
        $loader->load($confDir . '/{services}_' . $this->environment.self::CONFIG_EXTS, 'glob');
        $loader->load(__DIR__ . '/optional/custom/path' . self::CONFIG_EXTS, 'glob');
    }

    // ...
}

...in 2 seconds?

If you found __DIR__ . '/optional/custom/path', good job!


What directory does this Kernel load?

<?php declare(strict_types=1);

// ...

final class AppKernel extends Kernel
{
    // ...

    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
    {
        $this->flexLoader->loadConfigs($container, $loader, [
            __DIR__ . '/another/custom/path'
        ]);

        $container->addCompilerPass(new AutowireArrayParameterCompilerPass());
    }

    // ...
}

Do you prefer to read about extra compiler passes that modify your application or all the directories and suffixes your configs might be loaded from?

Programmers are lazy and they don't want to read any letter more then they have to. Unless they're paid by for each read letter :).

Make Kernel Small Again

Symplify is introducing a small handy package - FlexLoader. It handles Flex service and route loading with 3 lines (methods) and makes your Kernel to be nice and readable - like they used to the old times.


How does FlexLoader look in practice?


That's all! Now you can focus on important things, like writing cool compiler passes.



Happy weight loss, so you're fit and slim for Christmas :)


Found a typo? Fix it to join team of 64 people that improve content here

❤️️ Do you like what I write about? Or do you hate it but enjoy discussion? 😠
You can support my writing by throwing a couple bucks at my Patreon