【问题标题】:Symfony 2 with custom config yaml file带有自定义配置 yaml 文件的 Symfony 2
【发布时间】:2014-05-04 16:27:10
【问题描述】:

我在我的项目中创建了一个新包,并按照how to expose the configuration 上的说明进行操作。

这部分工作正常,但是当我尝试在 bundle 配置中添加一个新的 yaml 文件(以我的 bundle 命名,以便我可能在其他 bundle config 文件夹中创建类似的命名文件并将它们合并)时,使用相同的标签并将其添加到扩展类中以进行加载我收到错误:

InvalidArgumentException: There is no extension able to load the configuration for "foo_bar_notify" (in /path/to/symfony/src/Foo/Bar/NotifyBundle/DependencyInjection/../Resources/config/notify.yml). Looked for namespace "foo_bar_notify", found none

谁能指出我错过了什么(或做错了什么)。

代码 sn-ps;
配置类

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

    $rootNode
        ->children()
            ->arrayNode('roles')
                ->requiresAtLeastOneElement()
                ->prototype('array')
                    ->children()
                        ->scalarNode('name')
                            ->isRequired(true)
                        ->end()
                        ->scalarNode('description')
                        ->end()
                        ->arrayNode('placeholders')
                            ->prototype('scalar')->end()
                        ->end()
                        ->booleanNode('html')
                            ->isRequired()
                            ->defaultValue(true)
                        ->end()
                        ->scalarNode('template')
                            ->isRequired(true)
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();

    return $treeBuilder;
}

扩展类

public function load(array $configs, ContainerBuilder $container)
{
    $processor = new Processor();
    $configuration = new Configuration();
    $config = $processor->processConfiguration($configuration, $configs);

    $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('services.yml');
    $loader->load('foo_bar_notify.yml');

    $container->setParameter('notify', $config);
}

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

主配置文件

foo_bar_notify:
    roles:
        software_developer:
            name: Alice
            description: Foo
            placeholders:
                - $name
                - $email
            html: true
            template: 'some/path'
        super_developer:
            name: Malfoy
            description: bar
            html: false
            template: 'some/path/over/here'

自定义 yaml (notify.yml)

foo_bar_notify:
    roles:
        core_developer:
            name: core
            html: true
            template: 'some/path'

【问题讨论】:

    标签: php symfony configuration bundle


    【解决方案1】:

    我已经使用this question 的答案找到了解决方案。

    我的扩展类现在看起来像这样;

    private $configFileName = 'notify.yml';
    
    public function load(array $configs, ContainerBuilder $container)
    {  
        // see if there are any other notify.yml files defined in other bundles
        $finder = new \Symfony\Component\Finder\Finder();
        $finder
            ->directories()
            ->in($container->getParameter('kernel.root_dir') . '/../src/Foo/Bar/*Bundle/Resources')
            ->path('config');
    
        foreach ($finder as $c_dir) {
            $absolute_path = $c_dir.'/'.$this->configFileName;
            if (file_exists($absolute_path)) {
                $to_add = array_values(Yaml::parse(file_get_contents($absolute_path)));
                $configs[0] = $this->merge($configs[0], $to_add[0]);
            }
        }
    
        $processor = new Processor();
        $configuration = new Configuration();
        $config = $processor->processConfiguration($configuration, $configs);
    
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    
        // set the processed values as a parameter
        $container->setParameter('notify', $config);
    }
    

    【讨论】:

    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多