FriendsOfSymfony / FOSHttpCacheBundle

Use the FOSHttpCache library in your Symfony projects
http://foshttpcachebundle.readthedocs.io
Other
432 stars 81 forks source link

Incompatible use of dynamic environment variables when configuring fos_http_cache.proxy_client.http...base_url #449

Closed ghost closed 6 years ago

ghost commented 6 years ago

With this config

fos_http_cache.yaml
    fos_http_cache:
        proxy_client:
            default: varnish
            varnish:
                http:
                    base_url: test.local.net:443

replacing the explicit url with ENV var

.env
    BASE_URL='test.local.net'

services.yaml
    parameters:
        base.url:   '%env(BASE_URL)%'

fos_http_cache.yaml
    fos_http_cache:
        proxy_client:
            default: varnish
            varnish:
                http:
-                   base_url: test.local.net:443
+                   base_url: '%base.url%:443'

also 'works' (no errors, haven't yet fully tested varnish purge etc)

but if you try to inject the PORT dynamically,

.env
    BASE_URL='test.local.net'
+   BASE_PORT='443'
(or BASE_PORT=443)

services.yaml
    parameters:
        base.url:   '%env(BASE_URL)%'
+       base.port:  '%env(BASE_PORT)%'

fos_http_cache.yaml
    fos_http_cache:
        proxy_client:
            default: varnish
            varnish:
                http:
-                   base_url: test.local.net:443
-                   base_url: '%base.url%:443'
+                   base_url: '%base.url%:%base.port%'

profiler shows

(2/2) EnvParameterException

    Incompatible use of dynamic environment variables "BASE_URL", "BASE_PORT" found in parameters.
    in Compiler.php (line 112)
    at Compiler->compile(object(ContainerBuilder))in ContainerBuilder.php (line 713)
    at ContainerBuilder->compile()in Kernel.php (line 513)
    at Kernel->initializeContainer()in Kernel.php (line 125)
    at Kernel->boot()in Kernel.php (line 185)
    at Kernel->handle(object(Request))in index.php (line 37)

(1/2) InvalidConfigurationException

    Not a valid base path: "http://%env(BASE_URL)%:%env(BASE_PORT)%"
    in FOSHttpCacheExtension.php (line 566)
    at FOSHttpCacheExtension->validateUrl('http://env_BASE_URL_82a306f54867199848964602d02a1770:env_BASE_PORT_806e9358ff4bd1bbdd72ce13ad0ce3ca', 'Not a valid base path: "%s"')in FOSHttpCacheExtension.php (line 379)
    at FOSHttpCacheExtension->createHttpDispatcherDefinition(object(MergeExtensionConfigurationContainerBuilder), array('servers' => array('127.0.0.1:6081'), 'base_url' => 'env_BASE_URL_82a306f54867199848964602d02a1770:env_BASE_PORT_806e9358ff4bd1bbdd72ce13ad0ce3ca', 'http_client' => null), 'fos_http_cache.proxy_client.varnish.http_dispatcher')in FOSHttpCacheExtension.php (line 399)
    at FOSHttpCacheExtension->loadVarnish(object(MergeExtensionConfigurationContainerBuilder), object(XmlFileLoader), array('tags_header' => 'X-Cache-Tags', 'header_length' => 7500, 'http' => array('servers' => array('127.0.0.1:6081'), 'base_url' => 'env_BASE_URL_82a306f54867199848964602d02a1770:env_BASE_PORT_806e9358ff4bd1bbdd72ce13ad0ce3ca', 'http_client' => null)))in FOSHttpCacheExtension.php (line 347)
    at FOSHttpCacheExtension->loadProxyClient(object(MergeExtensionConfigurationContainerBuilder), object(XmlFileLoader), array('default' => 'varnish', 'varnish' => array('tags_header' => 'X-Cache-Tags', 'header_length' => 7500, 'http' => array('servers' => array('127.0.0.1:6081'), 'base_url' => 'env_BASE_URL_82a306f54867199848964602d02a1770:env_BASE_PORT_806e9358ff4bd1bbdd72ce13ad0ce3ca', 'http_client' => null))))in FOSHttpCacheExtension.php (line 68)
    at FOSHttpCacheExtension->load(array(array('proxy_client' => array('default' => 'varnish', 'varnish' => array('tags_header' => 'X-Cache-Tags', 'header_length' => 7500, 'default_ban_headers' => null, 'http' => array('servers' => array('127.0.0.1:6081'), 'base_url' => 'env_BASE_URL_82a306f54867199848964602d02a1770:env_BASE_PORT_806e9358ff4bd1bbdd72ce13ad0ce3ca'))))), object(MergeExtensionConfigurationContainerBuilder))in MergeExtensionConfigurationPass.php (line 71)
    at MergeExtensionConfigurationPass->process(object(ContainerBuilder))in MergeExtensionConfigurationPass.php (line 39)
    at MergeExtensionConfigurationPass->process(object(ContainerBuilder))in Compiler.php (line 95)
    at Compiler->compile(object(ContainerBuilder))in ContainerBuilder.php (line 713)
    at ContainerBuilder->compile()in Kernel.php (line 513)
    at Kernel->initializeContainer()in Kernel.php (line 125)
    at Kernel->boot()in Kernel.php (line 185)
    at Kernel->handle(object(Request))in index.php (line 37)

and at console

bcs cache:clear
string(99) "http://env_BASE_URL_91cc1a4dda9c935f7aa32b51209832e8:env_BASE_PORT_fae3ce89901ece848ab04cb8210721dd"

In Compiler.php line 112:

  Incompatible use of dynamic environment variables "BASE_URL", "BASE_PORT" found in parameters.

In FOSHttpCacheExtension.php line 566:

  Not a valid base path: "http://%env(BASE_URL)%:%env(BASE_PORT)%"

fwi, references to similar/relevant(?) issue, re: injection of dynamic env vars in other bundle, snd-redis

https://github.com/symfony/symfony/issues/20850 https://github.com/symfony/symfony/commit/2422f7da9b004ef1a13c1429e711c7d4c03c239d

And, just adding @dbu 's comment from Slack in case it disappears over there:

I'll have to check at which point variables are supposed to be resolved then. i thought it would be before we look at them ...

dbu commented 6 years ago

sorry, finally looked at this again. it seems we would need to actively do something in this case. (not fully sure if we need to adjust just the validation, or also how the service is set up). do you want to look into it and provide a pull request?