【问题标题】:Symfony set up Export class and Configuration for your bundleSymfony 为你的包设置导出类和配置
【发布时间】:2017-02-23 15:51:08
【问题描述】:

我在 symfony 项目中构建捆绑包时遇到了意外行为。
我已经在 DependencyInjection 命名空间中构建了一个导出类,我只是这样做:

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

        $container->setParameter( 'auction.path', $config[ 'path' ] );
        $container->setParameter( 'auction.miao', $config[ 'miao' ] );
        $container->setParameter( 'auction.stock.pain', $config[ 'stock' ][ 'pain' ] );

        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../Resources/config/')
        );
    }
}

理论上我应该简单地定义 3 个变量。如果我转储配置变量,我会得到以下输出:

Array
(
    [path] => C:\progetti_symfony\repository-cms\src\AuctionBundle\DependencyInjection../web/images/tmp/
    [miao] => C:\progetti_symfony\repository-cms\src\AuctionBundle\DependencyInjection../web/images/pmt/
)

其中嵌套的 stock.pain 部分(详见下一个 sn-p)被忽略。

然后我在同一个文件夹中有一个配置类,我在其中定义了这个包的配置(这里是我丢失指南针的地方):

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

        $rootNode
                        ->children()
                            ->scalarNode('path')->defaultValue(__DIR__ . '../web/images/tmp/')->end()
                            ->scalarNode('miao')->defaultValue(__DIR__ . '../web/images/pmt/')->end()
                            ->arrayNode('stock')
                                ->children()
                                    ->scalarNode('pain')->defaultValue(__DIR__ . '../web/images/mpt/')->end()
                                ->end()
                            ->end()
                        ->end();
        return $treeBuilder;
    }
}

关于如何构建和访问此配置的任何建议?

【问题讨论】:

  • 有什么问题?

标签: symfony


【解决方案1】:

如果未设置stock,您可以将其定义为空数组。然后pain 将被设置为默认值。

$rootNode
    ->beforeNormalization()
        ->ifTrue(function($v) {
            return !isset($v['stock']);
        })
        ->then(function($v) {
            $v['stock'] = array();
            return $v;
        })
        ->end()
    ->children()
        ->scalarNode('path')->defaultValue(__DIR__ . '../web/images/tmp/')->end()
        ->scalarNode('miao')->defaultValue(__DIR__ . '../web/images/pmt/')->end()
        ->arrayNode('stock')
            ->children()
                ->scalarNode('pain')->defaultValue(__DIR__ . '../web/images/mpt/')->end()
                ->end()
            ->end()
        ->end();

更新:

更好的决策:使用addDefaultsIfNotSet()

$rootNode
    ->children()
        ->scalarNode('path')->defaultValue(__DIR__ . '../web/images/tmp/')->end()
        ->scalarNode('miao')->defaultValue(__DIR__ . '../web/images/pmt/')->end()
        ->arrayNode('stock')
            ->addDefaultsIfNotSet()
            ->children()
                ->scalarNode('pain')->defaultValue(__DIR__ . '../web/images/mpt/')->end()
                ->end()
            ->end()
        ->end();

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 2021-08-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2013-05-01
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多