如何从包的本地配置文件中添加全局配置? [英] How to add global configuration from the bundle's local configuration file?

查看:27
本文介绍了如何从包的本地配置文件中添加全局配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中业务逻辑被拆分为多个包,以便于管理和扩展.每个包都提供其实体、服务、模板(如果是 UI 包)等.

I have an application where business logic is split into bundles for easy management and extensions. Each bundle provides its entities, services, templates (if that is UI bundle) etc.

实体应注册到 ORM 以参与诸如 ./bin/console 指令:schema:update 之类的命令.

Entities should be registered to ORM to participate in commands like ./bin/console doctrine:schema:update.

我正在为我的包使用自定义文件夹结构,所以我在每个包中都没有任何根实体文件夹.因此,我必须使用 doctrine.orm.mappings 配置键注册实体.

I'm using custom folder structure for my bundles so I don't have any root Entity folder in each bundle. So, I have to register entities using doctrine.orm.mappings configuration key.

我希望每个包都可以自行注册其实体,而不会将对它们的引用放入全局配置中.

I want each bundle to register its entities itself without putting references to them into global config.

所以,我在每个包中都使用了 PrependExtensionInterface.我的 prepend() 方法如下所示:

So, I'm using PrependExtensionInterface for this in each bundle. My prepend() method looks like this:

/**
 * Allow an extension to prepend the extension configurations.
 *
 * @param ContainerBuilder $container
 */
public function prepend(ContainerBuilder $container)
{
    $config = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/config.yml'));

    foreach ($config as $key => $configuration) {
        $container->prependExtensionConfig($key, $configuration);
    }
}

我的 .yml 看起来像这样:

My .yml looks like this:

doctrine:
  orm:
      mappings:
          thiris_cart_logic_auth_user:
              type: annotation
              dir: %kernel.root_dir%/../src/ThirisCart/Logic/AuthBundle/User/Model
              alias: Category
              prefix: ThirisCart\Logic\AuthBundle\User\Model
              is_bundle: false

除了一件事之外,它的效果非常好.看起来,我从 .yml 中读取的配置没有经过正确性验证.它只是合并到全局配置中,没有进一步的问题.

It works very good except for one thing. It looks, like the configuration I've read from .yml is not verified for correctness. It is just merged into global configuration without further questions.

那么,问题是如何验证?

So, the question is how to verify it?

推荐答案

我通过查看 Symfony 源代码找到了答案.

I've found an answer by looking through Symfony source.

如果你查看 Symfony\Component\DependencyInjection\Compiler\PassConfig::__construct 你会看到,第一遍是 MergeExtensionConfigurationPass,其中prepend()被调用.因此,附加的配置将得到验证.

If you look into Symfony\Component\DependencyInjection\Compiler\PassConfig::__construct you will see, that first pass is MergeExtensionConfigurationPass, where prepend() is called. So, appended configuration will get verified.

但请注意,Symfony 缓存配置.如果您在 prepend() 方法中更改代码,您将必须touch() 一些配置文件,或者只是使配置缓存无效才能使您的更改生效.

But note, that Symfony caches configuration. And if you change your code in your prepend() method, you will have to touch() some of your config files as well or just invalidate config cache for your changes to have effect.

这篇关于如何从包的本地配置文件中添加全局配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆