簡體   English   中英

Symfony捆綁包默認配置

[英]Symfony bundle default configurations

我一直在尋找答案,但是我沒有找到任何幫助。

我在symfony github上問,但他們叫我在這里寫。.https ://github.com/symfony/symfony/issues/28650

我正在編寫一個簡單的symfony捆綁包,但是在更改默認配置時遇到了問題。 我的意思是我想使用yaml翻譯(不是xliff),yaml學說映射(不是注釋),yaml驗證(不是注釋)等(我不贊成使用yaml教義)

有可能在捆綁包中更改此配置嗎? 我希望我的捆綁包是自配置的,我不想在主應用程序中配置教義,翻譯等。

感謝幫助

您可以通過擴展類在Symfony中定義默認配置,也可以查看此Symfony捆綁包配置指南,其中詳細介紹了捆綁包配置。 默認配置可以是教義,翻譯或您可以在應用程序級別配置的任何其他配置。 使用Prepend Extension,甚至可以修改其他捆綁軟件配置

配置組件負責管理此配置規則,您可以從配置組件以及定義和處理配置值頁面中了解更多信息。

FOSOAuthServerBundle是Doctrine默認配置的示例。
他們更喜歡XML,但這是一種格式決定,XML,YAML或PHP的配置邏輯相同。

msgphp / user-bundle配置是通過PHP文件配置的另一個示例。

讓我們用代碼說話,這是帶有理論實體和簡單配置數組的YAML配置示例。 首先,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;
    }
}

其次,准備捆綁軟件的擴展類以加載此配置。

//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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM