如何修复参数循环引用异常


How to fix ParameterCircularReferenceException?

我有一个symfony2应用程序。我们之前已经为开发设置了一个docker-compose堆栈,这就是为什么我们希望它的所有配置都通过环境变量而不是parameters.yml进行设置。

因此,我将参数.yml的内容替换为:

parameters:
    locale: 'en'
    secret: 'SOME_SECURITY_TOKEN'
    ...

自:

parameters:
    locale: '%locale%'
    secret: '%secret%'
    ...

我的docker-compose.yml文件包含:

my_app:
    hostname: my-app
    build: .
    dockerfile: Dockerfile.dev
    ports:
        - "9080:80"
        - "9043:433"
    environment:
        LOCALE: en
        SECRET: SOME_SECURITY_TOKEN
        ...

然而,在重建我的容器后,我得到了异常:

ParameterCircularReferenceException in ParameterBag.php line 209: Circular reference detected for parameter "secret" ("secret" > "secret").
    1. in ParameterBag.php line 209
    2. at ParameterBag->resolveString('%secret%', array('secret' => true)) in ParameterBag.php line 185
    3. at ParameterBag->resolveValue('%secret%', array('secret' => true)) in ParameterBag.php line 214
    4. at ParameterBag->resolveString('%secret%', array('secret' => true)) in ParameterBag.php line 185
    5. at ParameterBag->resolveValue('%secret%', array()) in ParameterBag.php line 175
    6. at ParameterBag->resolveValue(array('secret' => '%secret%', 'router' => array('resource' => '%kernel.root_dir%/config/routing.yml', 'strict_requirements' => null), 'form' => null, 'csrf_protection' =>
       null, 'validation' => array('enable_annotations' => true), 'templating' => array('engines' => array('twig')), 'default_locale' => '%locale%', 'trusted_hosts' => null, 'trusted_proxies' => null,
       'session' => array('handler_id' => 'api.session.handler.memcached'), 'fragments' => null, 'http_method_override' => true), array()) in ParameterBag.php line 175
    7. at ParameterBag->resolveValue(array(array('secret' => '%secret%', 'router' => array('resource' => '%kernel.root_dir%/config/routing.yml', 'strict_requirements' => null), 'form' => null,
       'csrf_protection' => null, 'validation' => array('enable_annotations' => true), 'templating' => array('engines' => array('twig')), 'default_locale' => '%locale%', 'trusted_hosts' => null,
       'trusted_proxies' => null, 'session' => array('handler_id' => 'api.session.handler.memcached'), 'fragments' => null, 'http_method_override' => true), array('router' =>
       array('resource' => '%kernel.root_dir%/config/routing_dev.yml', 'strict_requirements' => true), 'profiler' => array('only_exceptions' => false)))) in MergeExtensionConfigurationPass.php line 45
    8. at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 39
    9. at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in Compiler.php line 107
   10. at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 589
   11. at ContainerBuilder->compile() in bootstrap.php.cache line 2687
   12. at Kernel->initializeContainer() in bootstrap.php.cache line 2465
   13. at Kernel->boot() in bootstrap.php.cache line 2496
   14. at Kernel->handle(object(Request)) in app_dev.php line 30

然而在我的容器中,我确实看到了 env 变量:

le-container:/var/www/my-app# env
SECRET=SOME_SECURITY_TOKEN
LOCALE=en

我做错了什么以及如何解决它?

出于某种原因,为我的环境变量添加前缀解决了这个问题:

parameters:
    locale: '%foo_locale%'
    secret: '%foo_secret%'

当然,每当设置变量时也是如此。我目前的工作理论是symfony不喜欢拥有相同的参数名称和env变量,但我不确定。

parameters:
    locale: '%locale%'
    secret: '%secret%'

无用的构造,语言环境和秘密已经是参数。只需删除此块。

有一个使用外部参数的解决方案。在变量前面加上"SYMFONY__"。在您的情况下,它将是:

my_app:
    ...
    environment:
         SYMFONY__APP__LOCALE: en
         SYMFONY__APP__SECRET: SOME_SECURITY_TOKEN
    ...

在您的参数中,您可以按如下方式调用它:

parameters:
    locale: %app.locale%
    secret: %app.secret%