DIC和参数

时间:2014-10-10 15:14:57

标签: symfony

我缺少一些东西。

假设我有一个数据库来自我开始一个新项目。该数据库将在恢复.dump文件后创建,因此它还将包含一些“静态”(从头开始的静态插入,以在其中填充“不可更改的”数据)信息。

由于这些信息永远不会改变,因为我需要它们中的一些(即:一个用于检索特定实体的id),我认为将它们放入一个用DIC&处理的配置文件中。共

第一个问题:这种方法是正确的还是更好的创建具有一些配置值的特定 .php文件?

第二个问题:为什么我通过DIC定义的参数不可用?

我在我的包中创建了一个名为DependencyInjection的文件夹,我创建了MyCompanyMyBundleNameExtension文件并创建了一个Configuration文件。我确信文件放在正确的位置,如果我“删除”它们所有的东西都会搞砸(只为那些会评论的人“你确定......”是的,我确定)我我的包“main”文件中还包含以下内容

public function build(ContainerBuilder $container) 
{
    parent::build($container);
}

但是如果我转储(我使用这种方法作为内部控制器异常“参数”xxxxx“必须定义。被引发”)扩展I内的配置文件可以注意到它是空的。我错过了什么吗?

<小时/>

更新

这是Configuration.php

namespace Foo\FooBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

class Configuration implements ConfigurationInterface
{
    /**
     * Generates the configuration tree.
     * 
     * @return TreeBuilder
     */
    public function getConfigTreeBuilder() 
    {
        $tree_builder = new TreeBuilder();
        $root_node = $tree_builder->root('foo_bundle');

        $root_node
            ->children()
                ->arrayNode('text_type')
                    ->children()
                        ->integerNode('title')->defaultValue(1)->end()
                        ->integerNode('description')->defaultValue(2)->end()
                    ->end()
                ->end()
            ->end()
        ;

        return $tree_builder;
    }
}

这是扩展

<?php

namespace Foo\BookingEngineBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Processor;

class FooFooBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container) 
    {
        $processor = new Processor();
        $configuration = new Configuration();

        $config = $processor->processConfiguration($configuration, $configs);
        error_log(print_r($config, true)); //empty

        $this->remapParametersNamespaces($config, $container, array(
                      '' => array(
                          'text_type' => 'foo_bundle.text_type.%s',
                      ),
        ));
    }

    protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces) 
    {
        foreach ($namespaces as $ns => $map) {
            if ($ns) {
                if (!array_key_exists($ns, $config)) {
                    continue;
                }
                $namespaceConfig = $config[$ns];
            } else {
                $namespaceConfig = $config;
            }
            if (is_array($map)) {
                $this->remapParameters($namespaceConfig, $container, $map);
            } else {
                foreach ($namespaceConfig as $name => $value) {
                    $container->setParameter(sprintf($map, $name), $value);
                }
            }
        }
    }

    protected function remapParameters(array $config, ContainerBuilder $container, array $map) 
    {
        foreach ($map as $name => $paramName) {
            if (array_key_exists($name, $config)) {
                $container->setParameter($paramName, $config[$name]);
            }
        }
    }

更新2

如果我从命令行运行以下php app/console config:dump-reference foo_bundle我可以看到打印出的配置。我很困惑。


更新3

我终于明白了:

您似乎需要在config.yml主文件中指定至少一个参数,否则执行到YourBundleNameExtension的“merge”选项将失败并返回一个空数组。

Subquestion:我没有一种方法可以让我从write parateter中解放出来参数.yml?

1 个答案:

答案 0 :(得分:0)

我终于明白了:

您似乎需要在config.yml主文件中指定至少一个参数(与您的Config DI文件相关),否则执行到YourBundleNameExtension的“合并”选项将失败并返回空数组。

查看我的Configuration.php文件,为了使代码无法直接在parameters.yml文件中指定参数,请以这种方式修改Configuration.php

<?php

namespace Koobi\BookingEngineBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class Configuration implements ConfigurationInterface
{
    /**
     * Generates the configuration tree.
     * 
     * @return TreeBuilder
     */
    public function getConfigTreeBuilder() 
    {
        $tree_builder = new TreeBuilder();
        $root_node = $tree_builder->root('koobi_booking_engine');

        $root_node
            ->children()
                ->arrayNode('text_type')
                    ->cannotBeOverwritten()
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->integerNode('title')->defaultValue(1)->end()
                        ->integerNode('description')->defaultValue(2)->end()
                    ->end()
                ->end()
            ->end()
        ;

        return $tree_builder;
    }
}

关键是->addDefaultsIfNotSet()构建器的方法

我如何解决问题?

查看扩展文件:你应该注意到

$config = $processor->processConfiguration($configuration, $configs);

代码片段。

如果您打开use Symfony\Component\Config\Definition\Processor;并分析processConfiguration()方法,您会看到代表$configs内容的config.yml - 将合并到$configuration(代表Configuration.php,因此扩展将管理的DIC文件。) 因此我想到了一个共同密钥应该出现在某处的想法:在成功检查之后我开始寻找一些方法来帮助我避免将参数写入config.yml的明确过程(这不是我想要的东西,现在一切都像魅力

SIDE NOTE

我仍然不知道这是否是一种有效的方法,所以我不会将其视为正确答案。