Skip to content

Commit de3b614

Browse files
committed
minor #9507 Document the built in env var processors (mcfedr, javiereguiluz)
This PR was merged into the 3.4 branch. Discussion ---------- Document the built in env var processors This is start at adding documentation for env var processors, e.g. `%env(int:FOO)%` I would welcome some feedback, if this a appropriate way to go about this, I think it needs some more examples to show how its used. It also would ideally add a description of how custom processors are added, although that might be a later request. starts to fix #8382, #9094 Commits ------- aed1607 Minor rewords and formatting fixes e3fe5fe Example of customer env var processor 67227a6 Add examples for parameter processors cd353c7 Add description of the built in envvar processors
1 parent 022bd2f commit de3b614

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

configuration/external_parameters.rst

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,184 @@ of the following:
139139
:doc:`Symfony profiler </profiler>`. In practice this shouldn't be a
140140
problem because the web profiler must **never** be enabled in production.
141141

142+
Environment Variable Processors
143+
-------------------------------
144+
145+
.. versionadded:: 3.4
146+
Environment variable processors were introduced in Symfony 3.4.
147+
148+
The values of the environment variables are considered strings by default.
149+
However, your code may expect other data types, like integers or booleans.
150+
Symfony solves this problem with *processors*, which modify the contents of the
151+
given environment variables. The following example uses the integer processor to
152+
turn the value of the ``HTTP_PORT`` env var into an integer:
153+
154+
.. configuration-block::
155+
156+
.. code-block:: yaml
157+
158+
# config/packages/framework.yaml
159+
framework:
160+
router:
161+
http_port: env(int:HTTP_PORT)
162+
163+
.. code-block:: xml
164+
165+
<!-- config/packages/framework.xml -->
166+
<?xml version="1.0" encoding="UTF-8" ?>
167+
168+
<container xmlns="http://symfony.com/schema/dic/services"
169+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
170+
xmlns:framework="http://symfony.com/schema/dic/symfony"
171+
xsi:schemaLocation="http://symfony.com/schema/dic/services
172+
http://symfony.com/schema/dic/services/services-1.0.xsd
173+
http://symfony.com/schema/dic/symfony
174+
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
175+
176+
<framework:config>
177+
<framework:router http_port="%env(int:HTTP_PORT)%" />
178+
</framework:config>
179+
</container>
180+
181+
.. code-block:: php
182+
183+
// config/packages/doctrine.php
184+
$container->loadFromExtension('framework', array(
185+
'router' => array(
186+
'http_port' => '%env(int:HTTP_PORT)%',
187+
)
188+
));
189+
190+
Symfony provides the following env var processors:
191+
192+
``env(string:FOO)``
193+
Casts ``FOO`` to a string:
194+
195+
.. code-block:: yaml
196+
197+
parameters:
198+
env(SECRET): "some_secret"
199+
framework:
200+
secret: '%env(string:SECRET)%'
201+
202+
``env(bool:FOO)``
203+
Casts ``FOO`` to a bool:
204+
205+
.. code-block:: yaml
206+
207+
parameters:
208+
env(HTTP_METHOD_OVERRIDE): "true"
209+
framework:
210+
http_method_override: '%env(bool:HTTP_METHOD_OVERRIDE)%'
211+
212+
``env(int:FOO)``
213+
Casts ``FOO`` to an int.
214+
215+
``env(float:FOO)``
216+
Casts ``FOO`` to an float.
217+
218+
``env(const:FOO)``
219+
Finds the const value named in ``FOO``:
220+
221+
.. code-block:: yaml
222+
223+
parameters:
224+
env(HEALTH_CHECK_METHOD): "Symfony\Component\HttpFoundation\Request:METHOD_HEAD"
225+
security:
226+
access_control:
227+
- { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
228+
229+
``env(base64:FOO)``
230+
Decodes the content of ``FOO``, which is a base64 encoded string.
231+
232+
``env(json:FOO)``
233+
Decodes the content of ``FOO``, which is a JSON encoded string. It returns
234+
either an array or ``null``:
235+
236+
.. code-block:: yaml
237+
238+
parameters:
239+
env(TRUSTED_HOSTS): "['10.0.0.1', '10.0.0.2']"
240+
framework:
241+
trusted_hosts: '%env(json:TRUSTED_HOSTS)%'
242+
243+
``env(resolve:FOO)``
244+
Replaces the string ``FOO`` by the value of a config parameter with the
245+
same name:
246+
247+
.. code-block:: yaml
248+
249+
parameters:
250+
env(HOST): '10.0.0.1'
251+
env(SENTRY_DSN): "http://%env(HOST)%/project"
252+
sentry:
253+
dsn: '%env(resolve:SENTRY_DSN)%'
254+
255+
``env(csv:FOO)``
256+
Decodes the content of ``FOO``, which is a CSV-encoded string:
257+
258+
.. code-block:: yaml
259+
260+
parameters:
261+
env(TRUSTED_HOSTS): "10.0.0.1, 10.0.0.2"
262+
framework:
263+
trusted_hosts: '%env(csv:TRUSTED_HOSTS)%'
264+
265+
``env(file:FOO)``
266+
Returns the contents of a file whose path is the value of the ``FOO`` env var:
267+
268+
.. code-block:: yaml
269+
270+
parameters:
271+
env(AUTH_FILE): "../config/auth.json"
272+
google:
273+
auth: '%env(file:AUTH_FILE)%'
274+
275+
It is also possible to combine any number of processors:
276+
277+
.. code-block:: yaml
278+
279+
parameters:
280+
env(AUTH_FILE): "%kernel.project_dir%/config/auth.json"
281+
google:
282+
# 1. gets the value of the AUTH_FILE env var
283+
# 2. replaces the values of any config param to get the config path
284+
# 3. gets the content of the file stored in that path
285+
# 4. JSON-decodes the content of the file and returns it
286+
auth: '%env(json:file:resolve:AUTH_FILE)%'
287+
288+
Custom Environment Variable Processors
289+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290+
291+
It's also possible to add your own processors for environment variables. First,
292+
create a class that implements
293+
:class:`Symfony\\Component\\DependencyInjection\\EnvVarProcessorInterface` and
294+
then, define a service for that class::
295+
296+
class LowercasingEnvVarProcessor implements EnvVarProcessorInterface
297+
{
298+
private $container;
299+
300+
public function __construct(ContainerInterface $container)
301+
{
302+
$this->container = $container;
303+
}
304+
305+
public function getEnv($prefix, $name, \Closure $getEnv)
306+
{
307+
$env = $getEnv($name);
308+
309+
return strtolower($env);
310+
}
311+
312+
public static function getProvidedTypes()
313+
{
314+
return [
315+
'lowercase' => 'string',
316+
];
317+
}
318+
}
319+
142320
Constants
143321
---------
144322

0 commit comments

Comments
 (0)