drix47193 2015-10-30 22:49
浏览 30

使用一个包处理多个Symfony包配置

I have lots of bundles which have similar config files called rules.yml held in BundleName/Resources/config/rules.yml Each config file follows the same structure:

bundle_name:
    rules:
      name:
      items: []
      requirements: []

I have one bundle called RulerBundle. This bundle needs to automatically load, validate and combine all the rules.yml found within the other bundles. I would like RulerBundle to produce something like:

bundle_a:
    rules:
      name: Rule 1
      items: ['First Item']
      requirements: ['Second Item', 'Third Item']
bundle_b:
    rules:
      name: Rule 2
      items: ['Second Item']
      requirements: ['Third Item']

This should be automatically updated when a new bundle is added with a rules.yml

Questions

  • Should I validate and process the config within every bundle? This will lead to code duplication as the validation rules will just be same.

  • How would I go about finding and merging each of the bundle configs with the RulerBundle

  • 写回答

1条回答 默认 最新

  • dongrao9436 2016-04-20 13:22
    关注

    So to that you can write a DependencyInjection extension in your top level bundle that will do all of the validation. Not sure what should happen if something is invalid but you could something like this:

    // src/BundleName/DependencyInjection/BundleExtension.php
    namespace Acme\HelloBundle\DependencyInjection;
    
    use Symfony\Component\HttpKernel\DependencyInjection\Extension;
    use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    
    class BundleName extends Extension implements PrependExtensionInterface
    {
        public function prepend(ContainerBuilder $container)
        {
            $bundles = $container->getParameter('kernel.bundles');
            foreach ($bundles as $name => $extension) {
                $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.$name.'/../../Resources/config'));
                // load the configuration file, returned as an array
                $config = $loader->loadFile('rules.yml');
    
                // validate the contents of your file
                $this->validateConfig($config);
    
                // append the config with the specified bundle name "bundle_a", "bundle_a"
                $container->prependExtensionConfig($name, $config);
            }
        }
    }
    

    haven't tested any of that but generally that is the base idea. You'll need to implement your config validation method.

    评论

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置