2017 © Pedro PelΓ‘ez
 

symfony-bundle directory-loader-bundle

Symfony DirectoryLoaderBundle to recursively import directories of configuration

image

wemakecustom/directory-loader-bundle

Symfony DirectoryLoaderBundle to recursively import directories of configuration

  • Thursday, February 18, 2016
  • by lavoiesl
  • Repository
  • 4 Watchers
  • 1 Stars
  • 860 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Symfony Directory Loader

Recursively import directories of configuration., (*1)

This works for your main configuration files and the router., (*2)

See https://github.com/symfony/symfony-standard/issues/599, (*3)

Since https://github.com/symfony/symfony/pull/14700 has been merged (Symfony 2.8+), the directory loader is included by default in Symfony. The documentation is kept here however for reference purposes., (*4)

Please refer to http://symfony.com/doc/current/cookbook/configuration/configuration_organization.html for further use-cases of the DirectoryLoader., (*5)

Installation

Add the following in your root composer.json file:, (*6)

{
    "require": {
        "wemakecustom/directory-loader-bundle": "^1.0"
    },
}

And modify your app/AppKernel.php:, (*7)

getResolver();
        $resolver->addLoader(new DirectoryFileLoader($container, $locator));
     
        return $loader;
    }
?>

Strictly speaking, registering the Bundle is only necessary if you want the fonctionnality in the routing files and overloading the ContainerLoader is only necessary for the main configuration files, but you do what you want., (*8)

Usage

The main goal of this bundle is to drop configuration files in folder without modifying the main config.yml. This way, each file can group all (and only) the configuration related to a specific bundle., (*9)

Using a directory based on the environment

Instead of using config_ENV.yml, one may use a directory for each environment., (*10)

For example:, (*11)

└── app
   β”œβ”€β”€ config
   β”‚  β”œβ”€β”€ common
   β”‚  β”‚  └── assetic.yml
   β”‚  β”‚  └── framework.yml
   β”‚  β”‚  └── security.yml
   β”‚  β”œβ”€β”€ dev
   β”‚  β”‚  └── framework.yml
   β”‚  β”‚  └── assetic.yml
   β”‚  β”œβ”€β”€ prod
   β”‚  β”‚  └── assetic.yml
   β”‚  β”‚  └── google_analytics.yml
   β”‚  β”œβ”€β”€ config.yml
   β”‚  └── parameters.yml
   └── AppKernel.php
<?php
    // app/AppKernel.php
    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config.yml');
        $loader->load(__DIR__.'/config/' . $this->getEnvironment() . '/');
    }
?>
# app/config/config.yml
imports:
    - { resource: 'parameters.yml' }
    - { resource: 'common/' }

Using directories for parameters

Parameters can be loaded as directories as well in the same fashion., (*12)

Moreover, an interesting concept is to have a folder for core parameters, global to your application and local parameters, specific to each installation., (*13)

For example:, (*14)

└── app
   β”œβ”€β”€ config
   β”‚  └── …
   β”œβ”€β”€ parameters
   β”‚  └── common
   β”‚  β”‚  └── core.yml
   β”‚  β”‚  └── i18n.yml
   β”‚  β”œβ”€β”€ dist
   β”‚  β”‚  └── mailer.yml
   β”‚  β”œβ”€β”€ local
   β”‚  β”‚  └── .gitignore
   β”‚  └── config.yml
   └── AppKernel.php

app/parameters/local/*.yml will be ignored and its content will be the files from app/parameters/dist, copied and modified accordingly., (*15)

You can then modify your config.yml:, (*16)

# app/config/config.yml
imports:
    - { resource: 'parameters/common/' }
    - { resource: 'parameters/local/' }
    - { resource: 'common/' }

Using a composer script to generate configuration files

To fill the missing files on each composer run, you can replace the native parameters builder by wemakecustom/composer-script-utils and configure your composer.json, replacing Incenteev\ParameterHandler\ScriptHandler, (*17)

{
    "require": {
        # ...
        "wemakecustom/directory-loader-bundle": "^1.0",
        "wemakecustom/composer-script-utils": "^0.3"
    },
    "scripts": {
        "post-install-cmd": [
            "WMC\\Composer\\Utils\\ConfigFile\\ConfigDir::updateDirs",
            # remove this: "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
            # ...
        ],
        "post-update-cmd": [
            "WMC\\Composer\\Utils\\ConfigFile\\ConfigDir::updateDirs",
            # remove this: "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
            # ...
        ]
    },
    "extra": {
        "update-config-dirs": {
            "app/config/parameters/dist": "app/config/parameters/local"
        },
        # # remove this: 
        # "incenteev-parameters": {
        #     "file": "app/config/parameters.yml"
        # },
    }
}

Full example

You can see the final configuration tree here: https://github.com/wemakecustom/symfony-kickstart/tree/master/dist, (*18)

Usage without Symfony Framework

It is also possible to use this library without registering with Symfony., (*19)

To do so, simply add the proper loader to your LoaderResolver., (*20)

Example with routing:, (*21)

addLoader(new DirectoryFileLoader($locator));
?>

Example with DI can be found above., (*22)

Word of caution

AsseticBundle features a PHP directory loader to be able to parse Controllers. This directory loader superseeds it: if it tries to do the job of Assetic’s loader, it works correctly, but not the other way around., (*23)

Therefore, this bundle inserts itself before Assetic to ensure it is run before. A way to prevent this is to specify type: directory in the router import, but there is currently a bug around this method. See symfony/AsseticBundle#289 and symfony/symfony#11018., (*24)

The Versions

18/02 2016

dev-master

9999999-dev http://github.com/wemakecustom/DirectoryLoaderBundle

Symfony DirectoryLoaderBundle to recursively import directories of configuration

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

18/02 2016

v1.0

1.0.0.0 http://github.com/wemakecustom/DirectoryLoaderBundle

Symfony DirectoryLoaderBundle to recursively import directories of configuration

  Sources   Download

MIT

The Requires

  • php >=5.3.2