Fabien Potencier
Contributed by Fabien Potencier in #21635

A few weeks ago, we published an article about importing files with glob patterns in Symfony 3.3. The response from the community was so enthusiastic that we decided to improve and generalize the support of glob patterns in the framework.

The previous feature allowed to use glob patterns only when importing files and it was restricted to the files used in the container, so you couldn't use it in routing files.

This new feature moves all the glob pattern logic to the Config component and adds a new GlobFileLoader class that actually loads files using glob patterns. Moreover, this new loader is registered for both the routing and container file loaders.

The following example, extracted from the Kernel class used in the soon-to-be-unveiled "Symfony Flex" project, shows this new glob file loader used inside a Symfony micro kernel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace Symfony\Flex;

use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    const CONFIG_EXTS = '.{php,xml,yaml,yml}';

    // ...

    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
    {
        $confDir = dirname($this->getRootDir()).'/etc';
        $loader->import($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
        $loader->import($confDir.'/packages/'.$this->getEnvironment().'/**/*'.self::CONFIG_EXTS, 'glob');
        $loader->import($confDir.'/container'.self::CONFIG_EXTS, 'glob');
    }

    protected function configureRoutes(RouteCollectionBuilder $routes)
    {
        $confDir = dirname($this->getRootDir()).'/etc';
        $routes->import($confDir.'/routing/*'.self::CONFIG_EXTS, '/', 'glob');
        $routes->import($confDir.'/routing/'.$this->getEnvironment().'/**/*'.self::CONFIG_EXTS, '/', 'glob');
        $routes->import($confDir.'/routing'.self::CONFIG_EXTS, '/', 'glob');
    }
}
Published in #Living on the edge