概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:46:43 | 217 次阅读 | 评论: 0 | 来源: 网络整理

You have seen how to use configuration parameters within Symfony service containers. There are special cases such as when you want, for instance, to use the %kernel.debug% parameter to make the services in your bundle enter debug mode. For this case there is more work to do in order to make the system understand the parameter value. By default your parameter %kernel.debug% will be treated as a simple string. Consider this example with the AcmeDemoBundle:

// Inside Configuration class
$rootNode
    ->children()
        ->booleanNode('logging')->defaultValue('%kernel.debug%')->end()
        // ...
    ->end()
;

// Inside the Extension class
$config = $this->processConfiguration($configuration, $configs);
var_dump($config['logging']);

Now, examine the results to see this closely:

  • YAML
    my_bundle:
        logging: true
        # true, as expected
    
    my_bundle:
        logging: "%kernel.debug%"
        # true/false (depends on 2nd parameter of AppKernel),
        # as expected, because %kernel.debug% inside configuration
        # gets evaluated before being passed to the extension
    
    my_bundle: ~
    # passes the string "%kernel.debug%".
    # Which is always considered as true.
    # The Configurator does not know anything about
    # "%kernel.debug%" being a parameter.
    
  • XML
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:my-bundle="http://example.org/schema/dic/my_bundle">
    
        <my-bundle:config logging="true" />
        <!-- true, as expected -->
    
         <my-bundle:config logging="%kernel.debug%" />
         <!-- true/false (depends on 2nd parameter of AppKernel),
              as expected, because %kernel.debug% inside configuration
              gets evaluated before being passed to the extension -->
    
        <my-bundle:config />
        <!-- passes the string "%kernel.debug%".
             Which is always considered as true.
             The Configurator does not know anything about
             "%kernel.debug%" being a parameter. -->
    </container>
    
  • PHP
    $container->loadFromExtension('my_bundle', array(
            'logging' => true,
            // true, as expected
        )
    );
    
    $container->loadFromExtension('my_bundle', array(
            'logging' => "%kernel.debug%",
            // true/false (depends on 2nd parameter of AppKernel),
            // as expected, because %kernel.debug% inside configuration
            // gets evaluated before being passed to the extension
        )
    );
    
    $container->loadFromExtension('my_bundle');
    // passes the string "%kernel.debug%".
    // Which is always considered as true.
    // The Configurator does not know anything about
    // "%kernel.debug%" being a parameter.
    

In order to support this use case, the Configuration class has to be injected with this parameter via the extension as follows:

namespace AcmeDemoBundleDependencyInjection;

use SymfonyComponentConfigDefinitionBuilderArrayNodeDefinition;
use SymfonyComponentConfigDefinitionBuilderTreeBuilder;
use SymfonyComponentConfigDefinitionConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    private $debug;

    public function  __construct($debug)
    {
        $this->debug = (Boolean) $debug;
    }

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('acme_demo');

        $rootNode
            ->children()
                // ...
                ->booleanNode('logging')->defaultValue($this->debug)->end()
                // ...
            ->end()
        ;

        return $treeBuilder;
    }
}

And set it in the constructor of Configuration via the Extension class:

namespace AcmeDemoBundleDependencyInjection;

use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentDependencyInjectionLoaderXmlFileLoader;
use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentConfigFileLocator;

class AcmeDemoExtension extends Extension
{
    // ...

    public function getConfiguration(array $config, ContainerBuilder $container)
    {
        return new Configuration($container->getParameter('kernel.debug'));
    }
}
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务