简体   繁体   English

Symfony 6.1 获取另一个包配置数据

[英]Symfony 6.1 get another bundle configuration data

I'm trying to get the configuration of one bundle from within my bundle, configured using the new, simplified bundle configuratation at https://symfony.com/blog/new-in-symfony-6-1-simpler-bundle-extension-and-configuration我正在尝试从我的捆绑包中获取一个捆绑包的配置,使用https://symfony.com/blog/new-in-symfony-6-1-simpler-bundle-extension上的新的简化捆绑包配置进行配置-和-配置

My bundle has symfony/maker-bundle as a dependency, and I'd like to know maker-bundle's configuration我的包有 symfony/maker-bundle 作为依赖项,我想知道 maker-bundle 的配置

maker: 
  root_namespace: MyApp

I'm trying to figure out where I have access to that information, especially since I no longer have an explicit MyBundleExtension class.我试图弄清楚我在哪里可以访问这些信息,特别是因为我不再有一个明确的 MyBundleExtension 类。 When my bundle is loading, the parameter 'maker.root_namespace' is not in the container parameterBag.当我的包正在加载时,参数“maker.root_namespace”不在容器 parameterBag 中。 So I added a CompilerPass, and it's not visible there either.所以我添加了一个 CompilerPass,它在那里也不可见。

I've tried using the prepend() and prependExtension() calls, but am not sure how to wire those in to the new bundle configuration, or even if that's the right place.我已经尝试使用 prepend() 和 prependExtension() 调用,但不确定如何将它们连接到新的捆绑配置中,或者即使那是正确的地方。

While I'm using the symfony/maker-bundle in the particular case, I often want to know the values of other bundle configurations, and it seems like a pretty common thing.当我在特定情况下使用 symfony/maker-bundle 时,我经常想知道其他包配置的值,这似乎是一件很常见的事情。 I suspect it's possible without creating an Extension class, but not sure how.我怀疑不创建扩展类是可能的,但不确定如何。

Accessing other bundle configurations is currently possible in the prependExtension() method and it's the recommended place to do it:目前可以在prependExtension()方法中访问其他捆绑配置,这是推荐的地方:

class MyMakerBundle extends AbstractBundle
{
    public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        $configs = $builder->getExtensionConfig('maker');
        
        // ...
    }
}

However, this method is meant to prepend more configs not to get/process these configs.但是,此方法旨在添加更多配置而不是获取/处理这些配置。 So you have to take into account that it's a list of unprocessed and unmerged configs which can change if another extension is prepending a new config to the MakerBundle after your own extension.因此,您必须考虑到它是一个未处理和未合并的配置列表,如果另一个扩展在您自己的扩展之后将新配置添加到 MakerBundle,则该列表可能会发生变化。

Further, the value of this root_namespace can be (less probable in this case) a parameter or env var (makes no sense but is still possible) which is not resolved at this point (raw value).此外,这个root_namespace的值可以是(在这种情况下不太可能)参数或 env var(没有意义,但仍然可能),此时尚未解决(原始值)。

Option 1 (Safest)选项 1(最安全)

Create a similar configuration in your MyMakerBundle::configure() method and prepend the one obtained from the MakerBundle extension:在您的MyMakerBundle::configure()方法中创建一个类似的配置,并预先添加从 MakerBundle 扩展中获得的配置:

public function configure(DefinitionConfigurator $definition): void
{
    $definition->rootNode()
        ->children()
            ->scalarNode('root_namespace')->end()
        ->end();
}

public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
    $configs = $builder->getExtensionConfig('maker');
        
    // iterate in reverse to preserve the original order after prepending the config
    foreach (array_reverse($configs) as $config) {
        $container->prependExtensionConfig('my_maker', [
            'root_namespace' => $config['root_namespace'],
        ]);
    }
}

public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
    // do something with 
    $config['root_namespace'];
}

https://symfony.com/doc/current/bundles/prepend_extension.html https://symfony.com/doc/current/bundles/prepend_extension.html

Option 2 (Risky)选项 2(风险)

Because it's a simple configuration used in dev mode only, you can do a simple $config = array_merge(...$configs) directly to get the $config['root_namespace'] value:因为它是一个仅在开发模式下使用的简单配置,所以您可以直接执行简单的$config = array_merge(...$configs)来获取$config['root_namespace']值:

public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
    $configs = $builder->getExtensionConfig('maker');

    $config = array_merge(...$configs);
    $container->parameters()
        ->set('my_maker.root_namespace', $config['root_namespace']);
}

public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
    // do something with 
    $builder->getParameter('my_maker.root_namespace'));

    $builder->getParameterBag()->remove('my_maker.root_namespace');
}

This second option can be done in a CompilerPass too with the benefit that you wouldn't need to create a temporal parameter.第二个选项也可以在 CompilerPass 中完成,其好处是您不需要创建时间参数。

Option 3 (Risky)选项 3(风险)

In a CompilerPass class, get the root namespace value from one of the following services belonging to the MakerBundle:在 CompilerPass 类中,从属于 MakerBundle 的以下服务之一获取根命名空间值:

  • maker.generator argument 1 (eg $container->getDefinition('maker.generator')->getArgument(1) ) maker.generator参数 1(例如$container->getDefinition('maker.generator')->getArgument(1)
  • maker.autoloader_finder argument 0. maker.autoloader_finder参数 0。

I know, so far it's not as simple and intuitive as you might expect.我知道,到目前为止,它并不像你想象的那么简单和直观。

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

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