Giter Club home page Giter Club logo

symfony2-light-configuration-container's Introduction

light symfony2 bundle configuration container

how to use it:

/my_bundle_dir/Resources/config/services.yml:

services:
  n3b_config:
    class:                          ConfigBuilderNamespace\ConfigContainerBuilder
    arguments:
      container:                    '@service_container'
  another_service:
    ...

MyBundleNamespace\DependencyInjection\MyBundleExtension:

<?php

namespace MyBundleNamespace\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;

class MyBundleExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
        $loader->load('services.yml');
        new \n3b\Bundle\Util\Service\Config\ConfigParser($configs, $container, $this->getAlias());
    }

    public function getAlias()
    {
        return 'my_bundle';
    }
}

and now, if you inject this container in your service:

/my_bundle_dir/Resources/config/services.yml:

services:
  n3b_config:
    class:                          ConfigBuilderNamespace\ConfigContainerBuilder
    arguments:
      container:                    '@service_container'
  blog:
    class:                          n3b\Bundle\Blog\Service\BlogService
    arguments:
      services:
        em:                         '@doctrine.orm.entity_manager'
        templating:                 '@templating'
        security:                   '@security.context'
        router:                     '@router'
        http_kernel:                '@http_kernel'
      config:                       '@n3b_config'

and you have something in your app config file:

/app/config/config.yml:

...
n3b_blog:
    posts_per_page: 10
    max_notices: 5
    twitter:
      articles_translate: false
      app_name: N3b.ru exchange app
      app_key: abcde
...

you can get it this way:

n3b\Bundle\Blog\Service\BlogService:

public function __construct($services, $config)
{
    var_dump($config->get('n3b_blog'));
}

result:

object(n3b\Bundle\Util\Service\Config\ConfigContainer)#4022 (1) {
  ["container":protected]=>
  array(5) {
    ["posts_per_page"]=>
    int(10)
    ["max_notices"]=>
    int(5)
    ["twitter"]=>
    object(n3b\Bundle\Util\Service\Config\ConfigContainer)#4023 (1) {
      ["container":protected]=>
      array(6) {
        ["articles_translate"]=>
        bool(false)
        ["app_name"]=>
        string(19) "N3b.ru exchange app"
        ["app_key"]=>
        string(5) "abcde"
      }
    }
  }

var_dump($config->get('n3b_blog')->get('twitter'));:

result:

object(n3b\Bundle\Util\Service\Config\ConfigContainer)#68 (1) {
  ["container":protected]=>
  array(6) {
    ["articles_translate"]=>
    bool(false)
    ["app_name"]=>
    string(19) "N3b.ru exchange app"
    ["app_key"]=>
    string(5) "abcde"
  }
}

var_dump($config->get('n3b_blog.twitter.app_name'));:

result:

string(19) "N3b.ru exchange app"

var_dump($config->get('n3b_blog.twitter')->getArray());:

result:

array(6) {
  ["articles_translate"]=>
  bool(false)
  ["app_name"]=>
  string(19) "N3b.ru exchange app"
  ["app_key"]=>
  string(5) "abcde"
}

symfony2-light-configuration-container's People

Contributors

n3b avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.