@@ -99,6 +99,181 @@ will be used whenever the corresponding environment variable is *not* found:
9999 // config/services.php
100100 $container->setParameter('env(DATABASE_HOST)', 'localhost');
101101
102+ Environment Variable Processors
103+ -------------------------------
104+
105+ The values of the environment variables are considered strings by default.
106+ However, your code may expect other data types, like integers or booleans.
107+ Symfony solves this problem with *processors *, which modify the contents of the
108+ given environment variables. The following example uses the integer processor to
109+ turn the value of the ``HTTP_PORT `` env var into an integer:
110+
111+ .. configuration-block ::
112+
113+ .. code-block :: yaml
114+
115+ # config/packages/framework.yaml
116+ framework :
117+ router :
118+ http_port : env(int:HTTP_PORT)
119+
120+ .. code-block :: xml
121+
122+ <!-- config/packages/framework.xml -->
123+ <?xml version =" 1.0" encoding =" UTF-8" ?>
124+
125+ <container xmlns =" http://symfony.com/schema/dic/services"
126+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
127+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
128+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
129+ http://symfony.com/schema/dic/services/services-1.0.xsd
130+ http://symfony.com/schema/dic/symfony
131+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
132+
133+ <framework : config >
134+ <framework : router http_port =" %env(int:HTTP_PORT)%" />
135+ </framework : config >
136+ </container >
137+
138+ .. code-block :: php
139+
140+ // config/packages/doctrine.php
141+ $container->loadFromExtension('framework', array(
142+ 'router' => array(
143+ 'http_port' => '%env(int:HTTP_PORT)%',
144+ )
145+ ));
146+
147+ Symfony provides the following env var processors:
148+
149+ ``env(string:FOO) ``
150+ Casts ``FOO `` to a string:
151+
152+ .. code-block :: yaml
153+
154+ parameters :
155+ env(SECRET) : " some_secret"
156+ framework :
157+ secret : ' %env(string:SECRET)%'
158+
159+ ``env(bool:FOO) ``
160+ Casts ``FOO `` to a bool:
161+
162+ .. code-block :: yaml
163+
164+ parameters :
165+ env(HTTP_METHOD_OVERRIDE) : " true"
166+ framework :
167+ http_method_override : ' %env(bool:HTTP_METHOD_OVERRIDE)%'
168+
169+ ``env(int:FOO) ``
170+ Casts ``FOO `` to an int.
171+
172+ ``env(float:FOO) ``
173+ Casts ``FOO `` to an float.
174+
175+ ``env(const:FOO) ``
176+ Finds the const value named in ``FOO ``:
177+
178+ .. code-block :: yaml
179+
180+ parameters :
181+ env(HEALTH_CHECK_METHOD) : " Symfony\C omponent\H ttpFoundation\R equest:METHOD_HEAD"
182+ security :
183+ access_control :
184+ - { path: '^/health-check$', methods: '%env(const:HEALTH_CHECK_METHOD)%' }
185+
186+ ``env(base64:FOO) ``
187+ Decodes the content of ``FOO ``, which is a base64 encoded string.
188+
189+ ``env(json:FOO) ``
190+ Decodes the content of ``FOO ``, which is a JSON encoded string. It returns
191+ either an array or ``null ``:
192+
193+ .. code-block :: yaml
194+
195+ parameters :
196+ env(TRUSTED_HOSTS) : " ['10.0.0.1', '10.0.0.2']"
197+ framework :
198+ trusted_hosts : ' %env(json:TRUSTED_HOSTS)%'
199+
200+ ``env(resolve:FOO) ``
201+ Replaces the string ``FOO `` by the value of a config parameter with the
202+ same name:
203+
204+ .. code-block :: yaml
205+
206+ parameters :
207+ env(HOST) : ' 10.0.0.1'
208+ env(SENTRY_DSN) : " http://%env(HOST)%/project"
209+ sentry :
210+ dsn : ' %env(resolve:SENTRY_DSN)%'
211+
212+ ``env(csv:FOO) ``
213+ Decodes the content of ``FOO ``, which is a CSV-encoded string:
214+
215+ .. code-block :: yaml
216+
217+ parameters :
218+ env(TRUSTED_HOSTS) : " 10.0.0.1, 10.0.0.2"
219+ framework :
220+ trusted_hosts : ' %env(csv:TRUSTED_HOSTS)%'
221+
222+ ``env(file:FOO) ``
223+ Returns the contents of a file whose path is the value of the ``FOO `` env var:
224+
225+ .. code-block :: yaml
226+
227+ parameters :
228+ env(AUTH_FILE) : " ../config/auth.json"
229+ google :
230+ auth : ' %env(file:AUTH_FILE)%'
231+
232+ It is also possible to combine any number of processors:
233+
234+ .. code-block :: yaml
235+
236+ parameters :
237+ env(AUTH_FILE) : " %kernel.project_dir%/config/auth.json"
238+ google :
239+ # 1. gets the value of the AUTH_FILE env var
240+ # 2. replaces the values of any config param to get the config path
241+ # 3. gets the content of the file stored in that path
242+ # 4. JSON-decodes the content of the file and returns it
243+ auth : ' %env(json:file:resolve:AUTH_FILE)%'
244+
245+ Custom Environment Variable Processors
246+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247+
248+ It's also possible to add your own processors for environment variables. First,
249+ create a class that implements
250+ :class: `Symfony\\ Component\\ DependencyInjection\\ EnvVarProcessorInterface ` and
251+ then, define a service for that class::
252+
253+ class LowercasingEnvVarProcessor implements EnvVarProcessorInterface
254+ {
255+ private $container;
256+
257+ public function __construct(ContainerInterface $container)
258+ {
259+ $this->container = $container;
260+ }
261+
262+ public function getEnv($prefix, $name, \Closure $getEnv)
263+ {
264+ $env = $getEnv($name);
265+
266+ return strtolower($env);
267+ }
268+
269+ public static function getProvidedTypes()
270+ {
271+ return [
272+ 'lowercase' => 'string',
273+ ];
274+ }
275+ }
276+
102277.. _configuration-env-var-in-prod :
103278
104279Configuring Environment Variables in Production
0 commit comments