首页 > 解决方案 > Symfony 捆绑包默认配置

问题描述

我一直在寻找答案,但我没有找到任何有用的东西..

我在 symfony github 上问过,但他们告诉我写在这里.. https://github.com/symfony/symfony/issues/28650

我正在编写一个简单的 symfony 包,但我在更改默认配置时遇到了问题。我的意思是我想使用 yaml 翻译(不是 xliff)、yaml 学说映射(不是注释)、yaml 验证(不是注释)等(我知道学说中的 yaml 已被弃用)

是否有可能在捆绑包中更改此配置?我希望我的包是自我配置的,我不想在我的主应用程序中配置学说、翻译等。

感谢帮助

标签: phpsymfonycomposer-phpbundleconfig

解决方案


你可以通过扩展类在 Symfony 中定义默认配置,也可以看看这个Symfony Bundle Configuration guide,它解释了很多关于 bundle 配置的内容。默认配置可以用于教义、翻译或您可以在应用程序级别配置的任何其他内容。甚至可以使用Prepend Extension修改其他捆绑包配置

Config 组件负责管理此配置规则,您可以从文档中的Config ComponentDefining and Processing Configuration Values页面了解更多信息。

FOSOAuthServerBundle是 Doctrine 默认配置的示例。
他们更喜欢 XML,但这是一种格式决定,XML、YAML 或 PHP 的配置逻辑相同。

msgphp/user-bundle配置是通过 PHP 文件进行配置的另一个示例。

让我们用代码说话,这是一个带有教义实体和简单配置数组的 YAML 配置示例。首先,创建配置类为我们的包提供默认配置规则。

// src/Acme/HelloBundle/DependencyInjection/Configuration.php
namespace Acme\HelloBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('acme_hello');

        $rootNode
            ->children()
                ->arrayNode('simple_array')
                    ->children()
                        ->scalarNode('foo')->end()
                        ->scalarNode('bar')->end()
                    ->end()
                ->end()
                ->arrayNode('doctrine')
                    ->children()
                        ->arrayNode('entity')
                            ->children()
                                ->scalarNode('class')->isRequired()->cannotBeEmpty()->end()
                            ->end()
                        ->end()
                        ->arrayNode('manager')
                            ->children()
                                ->scalarNode('class')->defaultValue('Acme\\HelloBundle\\Entity\\Manager\\EntryManager')->end()
                            ->end()
                        ->end()
                     ->end()
                 ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

其次,准备我们包的扩展类以加载此配置。

//src/Acme/HelloBundle/DependencyInjection/AcmeHelloExtension.php
namespace Acme\HelloBundle\DependencyInjection;

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

class AcmeHelloExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

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

        // you now have these 2 config keys
        // $config['simple_array'] and $config['doctrine']
        // $container->getDefinition('acme_hello.simple_array.foo');
    }
}

最后,创建默认 YAML 定义并将我们的条目管理器注册到容器。

//src/Acme/HelloBundle/Resources/config/acme_hello.yaml
acme_hello:
    simple_array:
        foo: 'hello'
        bar: 'world'
    doctrine:
        entity:
            class: 'Acme\HelloBundle\Entity\Entry'
        manager:
            class: 'Acme\HelloBundle\Entity\Manager\EntryManager'

//src/Acme/HelloBundle/Resources/config/services.yaml
services:
    acme_hello.entry_manager:
        class:     '%acme_hello.doctrine.manager.class%'
        arguments: [ '@doctrine.orm.entity_manager', '%acme_hello.doctrine.entity.class%' ]

我们也可以在应用程序级别覆盖默认配置。

//config/packages/acme_hello.yaml
acme_hello:
    simple_array:
        foo: 'world'
        bar: 'hello'

推荐阅读