简体   繁体   English

Symfony捆绑包默认配置

[英]Symfony bundle default configurations

I was looking for an answer but i havent find anything helpful.. 我一直在寻找答案,但是我没有找到任何帮助。

I asked on symfony github but they told me to write here.. https://github.com/symfony/symfony/issues/28650 我在symfony github上问,但他们叫我在这里写。.https ://github.com/symfony/symfony/issues/28650

I am writing a simple symfony bundle, but I have a problem with changing default configurations. 我正在编写一个简单的symfony捆绑包,但是在更改默认配置时遇到了问题。 I mean that I want to use yaml translations (not xliff), yaml doctrine mappings (not annotations), yaml validation (not annotations), etc. (i know that yaml in doctrine is deprecated) 我的意思是我想使用yaml翻译(不是xliff),yaml学说映射(不是注释),yaml验证(不是注释)等(我不赞成使用yaml教义)

Is there any possibility to change this configuration inside a bundle? 有可能在捆绑包中更改此配置吗? I want my bundles to be self configured, I dont want to configure doctrine, translations, etc inside my main app. 我希望我的捆绑包是自配置的,我不想在主应用程序中配置教义,翻译等。

Thanks for help 感谢帮助

You can define default configuration in Symfony via Extension Classes also take a look this Symfony Bundle Configuration guide, it's explains a lot about bundle configuration. 您可以通过扩展类在Symfony中定义默认配置,也可以查看此Symfony捆绑包配置指南,其中详细介绍了捆绑包配置。 Default configurations can be for doctrine, translations or anything else you can configure in application level. 默认配置可以是教义,翻译或您可以在应用程序级别配置的任何其他配置。 Even modifying other bundles configurations are possible with Prepend Extension 使用Prepend Extension,甚至可以修改其他捆绑软件配置

Config component is responsible manage this configuration rules, you can learn more about from Config Component and Defining and Processing Configuration Values pages in documentation. 配置组件负责管理此配置规则,您可以从配置组件以及定义和处理配置值页面中了解更多信息。

FOSOAuthServerBundle is example for Doctrine default configuration. FOSOAuthServerBundle是Doctrine默认配置的示例。
They prefer XML but this is a format decision, configuration logic is same for XML, YAML or PHP. 他们更喜欢XML,但这是一种格式决定,XML,YAML或PHP的配置逻辑相同。

msgphp/user-bundle configuration is an other example for configuration via PHP files. msgphp / user-bundle配置是通过PHP文件配置的另一个示例。

Let's speak in code, this is an example for YAML configuration with doctrine entity and simple config array. 让我们用代码说话,这是带有理论实体和简单配置数组的YAML配置示例。 First, create Configuration class provides default configuration rules for our bundle. 首先,create Configuration类为我们的捆绑包提供了默认的配置规则。

// 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;
    }
}

Second, prepare our bundle's extension class for load this configurations. 其次,准备捆绑软件的扩展类以加载此配置。

//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');
    }
}

Last, create default YAML definitions and register our entry manager to container. 最后,创建默认的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%' ]

Also we can override default configuration in application level. 同样,我们可以在应用程序级别覆盖默认配置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM