Skip to content

Extends Symfony Dependency Injection Component and provides store configurations in each bundles separately by common yaml-file

License

Notifications You must be signed in to change notification settings

adrenalinkin/config-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Config Helper На Русском

Introduction

Component allows extend standard class Symfony\Component\DependencyInjection\Extension\Extension and open possibility for collect YAML configurations across all registered bundles.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this component:

    composer require adrenalinkin/config-helper

This command requires you to have Composer install globally.

Usage examples and compare with standard methods

Let's say we have two bundles in our project. Bundles contains business-logic of the two separate system parts:

  • AcmeBundle with entities AcmeBundle:User and AcmeBundle:Position
  • AcmePostBundle with entity AcmePostBundle:Post

Imagine you need to add configuration for each entity. Let's say we need configuration which should determine user's system role for get access to specific functionality. For the configuration creation has been created bundle AcmeConfigBundle. Configuration example:

acme_config:
    AcmeBundle:User:     ROLE_USER_ADMIN # key - name of the entity; value - role
    AcmeBundle:Position: ROLE_USER_ADMIN
    AcmePostBundle:Post: ROLE_POST_ADMIN

Standard methods

We can put configuration into global configuration file app/config/config.yml:

# app/config/config.yml

# other bundle's configurations

acme_config:
    AcmeBundle:User:     ROLE_USER_ADMIN
    AcmeBundle:Position: ROLE_USER_ADMIN
    AcmePostBundle:Post: ROLE_POST_ADMIN

# other bundle's configurations

Also, we can put configuration into AcmeConfigBundle bundle under specific configuration file and load that from AcmeConfigExtension:

#Acme/ConfigBundle/Resources/config/custom.yml
acme_config:
    AcmeBundle:User:     ROLE_USER_ADMIN
    AcmeBundle:Position: ROLE_USER_ADMIN
    AcmePostBundle:Post: ROLE_POST_ADMIN

However, both of method, for our realisation, got one flaw. All time when we will create new bundles - we will need modify global configuration file or configuration file in the AcmeConfigBundle. Both situation provoke a hard-linking between separate parts of the project

Component usage

To prevent hard-linkin between separate parts of the project you need:

  • Choose file name for configuration store, for example acme_config.yml.
  • Extends AcmeConfigExtension from AbstractExtension:
<?php

namespace Acme\Bundle\ConfigBundle\DependencyInjection;

use Linkin\Component\ConfigHelper\Extension\AbstractExtension;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;

class AcmeConfigExtension extends AbstractExtension
{
    /**
     * {@inheritdoc}
     */
    public function getAlias()
    {
        return 'acme_config';
    }

    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        // load all configurations from the all registered bundles
        $configs = $this->getConfigurationsFromFile('acme_config.yml', $container);
        // process received configuration
        $configs = $this->processConfiguration(new Configuration(), $configs);
        
        // some actions

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}
  • Create configuration files per bundles:
# Acme/AcmeBundle/Resources/config/acme_config.yml
acme_config:
    AcmeBundle:User:     ROLE_USER_ADMIN
    AcmeBundle:Position: ROLE_USER_ADMIN
# AcmePost/AcmePostBundle/Resources/config/acme_config.yml
acme_config:
    AcmePostBundle:Post: ROLE_POST_ADMIN

This method allows you create and remove configurations in the bundles without global changes in the project. You can remove some configuration in the needed bundle or even remove whole bundle ( for example AcmePostBundle).

Note: By default method getConfigurationsFromFile($fileName, ContainerBuilder $container, $merge = true) uses standard PHP function array_merge_recursive to merge all found configurations. If you want prepare configuration by yourself put false as third parameter and receive stack of the all registered configurations.

License

license

About

Extends Symfony Dependency Injection Component and provides store configurations in each bundles separately by common yaml-file

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages