From 707907841d9b21e0a05b9128fa69ed8df0508228 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 2 Jun 2018 09:27:15 +0200 Subject: [PATCH] add missing XML and PHP code examples --- configuration/external_parameters.rst | 263 ++++++++++++++++++++++---- 1 file changed, 223 insertions(+), 40 deletions(-) diff --git a/configuration/external_parameters.rst b/configuration/external_parameters.rst index f477ba2c645..b20eaf5060c 100644 --- a/configuration/external_parameters.rst +++ b/configuration/external_parameters.rst @@ -60,7 +60,7 @@ variable in your service container configuration, you can reference it using $container->loadFromExtension('doctrine', array( 'dbal' => array( 'host' => '%env(DATABASE_HOST)%', - ) + ), )); You can also give the ``env()`` parameters a default value: the default value @@ -145,7 +145,7 @@ Environment Variable Processors .. versionadded:: 3.4 Environment variable processors were introduced in Symfony 3.4. -The values of the environment variables are considered strings by default. +The values of environment variables are considered strings by default. However, your code may expect other data types, like integers or booleans. Symfony solves this problem with *processors*, which modify the contents of the given environment variables. The following example uses the integer processor to @@ -164,7 +164,6 @@ turn the value of the ``HTTP_PORT`` env var into an integer: - - + .. code-block:: php - // config/packages/doctrine.php + // config/packages/framework.php $container->loadFromExtension('framework', array( 'router' => array( 'http_port' => '%env(int:HTTP_PORT)%', - ) + ), )); Symfony provides the following env var processors: @@ -192,39 +191,134 @@ Symfony provides the following env var processors: ``env(string:FOO)`` Casts ``FOO`` to a string: - .. code-block:: yaml + .. configuration-block:: - parameters: - env(SECRET): "some_secret" - framework: - secret: '%env(string:SECRET)%' + .. code-block:: yaml + + # config/packages/framework.yaml + parameters: + env(SECRET): 'some_secret' + framework: + secret: '%env(string:SECRET)%' + + .. code-block:: xml + + + + + + + some_secret + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->setParameter('env(SECRET)', 'some_secret'); + $container->loadFromExtension('framework', array( + 'secret' => '%env(string:SECRET)%', + )); ``env(bool:FOO)`` Casts ``FOO`` to a bool: - .. code-block:: yaml + .. configuration-block:: - parameters: - env(HTTP_METHOD_OVERRIDE): "true" - framework: - http_method_override: '%env(bool:HTTP_METHOD_OVERRIDE)%' + .. code-block:: yaml + + # config/packages/framework.yaml + parameters: + env(HTTP_METHOD_OVERRIDE): 'true' + framework: + http_method_override: '%env(bool:HTTP_METHOD_OVERRIDE)%' + + .. code-block:: xml + + + + + + + true + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->setParameter('env(HTTP_METHOD_OVERRIDE)', 'true'); + $container->loadFromExtension('framework', array( + 'http_method_override' => '%env(bool:HTTP_METHOD_OVERRIDE)%', + )); ``env(int:FOO)`` Casts ``FOO`` to an int. ``env(float:FOO)`` - Casts ``FOO`` to an float. + Casts ``FOO`` to a float. ``env(const:FOO)`` Finds the const value named in ``FOO``: - .. code-block:: yaml - - parameters: - env(HEALTH_CHECK_METHOD): "Symfony\Component\HttpFoundation\Request:METHOD_HEAD" - security: - access_control: - - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' } + .. configuration-block:: + + .. code-block:: yaml + + # config/packages/security.yaml + parameters: + env(HEALTH_CHECK_METHOD): 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD' + security: + access_control: + - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' } + + .. code-block:: xml + + + + + + + Symfony\Component\HttpFoundation\Request::METHOD_HEAD + + + + + + + + .. code-block:: php + + // config/packages/security.php + $container->setParameter('env(HEALTH_CHECK_METHOD)', 'Symfony\Component\HttpFoundation\Request::METHOD_HEAD'); + $container->loadFromExtension('security', array( + 'access_control' => array( + array( + 'path' => '^/health-check$', + 'methods' => '%env(const:HEALTH_CHECK_METHOD)%', + ), + ), + )); ``env(base64:FOO)`` Decodes the content of ``FOO``, which is a base64 encoded string. @@ -233,34 +327,123 @@ Symfony provides the following env var processors: Decodes the content of ``FOO``, which is a JSON encoded string. It returns either an array or ``null``: - .. code-block:: yaml + .. configuration-block:: - parameters: - env(TRUSTED_HOSTS): "['10.0.0.1', '10.0.0.2']" - framework: - trusted_hosts: '%env(json:TRUSTED_HOSTS)%' + .. code-block:: yaml + + # config/packages/framework.yaml + parameters: + env(TRUSTED_HOSTS): '["10.0.0.1", "10.0.0.2"]' + framework: + trusted_hosts: '%env(json:TRUSTED_HOSTS)%' + + .. code-block:: xml + + + + + + + ["10.0.0.1", "10.0.0.2"] + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->setParameter('env(TRUSTED_HOSTS)', '["10.0.0.1", "10.0.0.2"]'); + $container->loadFromExtension('framework', array( + 'trusted_hosts' => '%env(json:TRUSTED_HOSTS)%', + )); ``env(resolve:FOO)`` Replaces the string ``FOO`` by the value of a config parameter with the same name: - .. code-block:: yaml + .. configuration-block:: - parameters: - env(HOST): '10.0.0.1' - env(SENTRY_DSN): "http://%env(HOST)%/project" - sentry: - dsn: '%env(resolve:SENTRY_DSN)%' + .. code-block:: yaml + + # config/packages/sentry.yaml + parameters: + env(HOST): '10.0.0.1' + env(SENTRY_DSN): 'http://%env(HOST)%/project' + sentry: + dsn: '%env(resolve:SENTRY_DSN)%' + + .. code-block:: xml + + + + + + + 10.0.0.1 + http://%env(HOST)%/project + + + + + + .. code-block:: php + + // config/packages/sentry.php + $container->setParameter('env(HOST)', '10.0.0.1'); + $container->setParameter('env(SENTRY_DSN)', 'http://%env(HOST)%/project'); + $container->loadFromExtension('sentry', array( + 'dsn' => '%env(resolve:SENTRY_DSN)%', + )); ``env(file:FOO)`` Returns the contents of a file whose path is the value of the ``FOO`` env var: - .. code-block:: yaml + .. configuration-block:: - parameters: - env(AUTH_FILE): "../config/auth.json" - google: - auth: '%env(file:AUTH_FILE)%' + .. code-block:: yaml + + # config/packages/framework.yaml + parameters: + env(AUTH_FILE): '../config/auth.json' + google: + auth: '%env(file:AUTH_FILE)%' + + .. code-block:: xml + + + + + + + ../config/auth.json + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->setParameter('env(AUTH_FILE)', '../config/auth.json'); + $container->loadFromExtension('google', array( + 'auth' => '%env(file:AUTH_FILE)%', + )); It is also possible to combine any number of processors: