8000 [Configuration] Mention `EnvVarLoaderInterface` · symfony/symfony-docs@f50c781 · GitHub
[go: up one dir, main page]

Skip to content

Commit f50c781

Browse files
[Configuration] Mention EnvVarLoaderInterface
1 parent 8822e37 commit f50c781

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

configuration.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,76 @@ environment variables, with their values, referenced in Symfony's container conf
993993
# run this command to show all the details for a specific env var:
994994
$ php bin/console debug:container --env-var=FOO
995995
996+
Create Your Own Logic To Load Env Vars
997+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
998+
999+
You can implement your own logic to load environment variables in your
1000+
application if the default behavior doesn't exactly fit your needs. This
1001+
can be done by implementing the
1002+
:class:`Symfony\\Component\\DependencyInjection\\EnvVarLoaderInterface`.
1003+
1004+
.. note::
1005+
1006+
When using autoconfiguration, implementing the interface is the only
1007+
required step. Otherwise, you have to manually add the
1008+
``container.env_var_loader`` tag to your class. You can learn more about
1009+
it in :doc:`the dedicated page </service_container/tag>`.
1010+
1011+
Let's say you have a JSON file named ``env.json`` containing your environment
1012+
variables:
1013+
1014+
.. code-block:: json
1015+
1016+
{
1017+
"vars": {
1018+
"APP_ENV": "prod",
1019+
"APP_DEBUG": false
1020+
}
1021+
}
1022+
1023+
We can create a new class named ``JsonEnvVarLoader`` to populate our environment
1024+
variables from the file::
1025+
1026+
namespace App\DependencyInjection;
1027+
1028+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1029+
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
1030+
1031+
class JsonEnvVarLoader implements EnvVarLoaderInterface
1032+
{
1033+
private const ENV_VARS_FILE = 'env.json';
1034+
1035+
public function loadEnvVars(): array
1036+
{
1037+
$fileName = __DIR__.\DIRECTORY_SEPARATOR.self::ENV_VARS_FILE;
1038+
if (!is_file($fileName)) {
1039+
// throw an error or just ignore this loader, depending on your needs
1040+
}
1041+
1042+
$content = json_decode(file_get_contents($fileName), true);
1043+
1044+
return $content['vars'];
1045+
}
1046+
}
1047+
1048+
That's it! Now the application will look at a ``env.json`` file in the
1049+
current directory to populate environment variables, additionally to the
1050+
already existing ``.env`` files.
1051+
1052+
.. tip::
1053+
1054+
If you want an env var to have a value on a certain environment but to fallback
1055+
on loaders on another environment, assign an empty value to the env var for
1056+
the environment you want to use loaders:
1057+
1058+
.. code-block:: bash
1059+
1060+
# .env (or .env.local)
1061+
APP_ENV=prod
1062+
1063+
# .env.prod (or .env.local.prod) - this will fallback on the loaders you defined
1064+
APP_ENV=
1065+
9961066
.. _configuration-accessing-parameters:
9971067

9981068
Accessing Configuration Parameters

0 commit comments

Comments
 (0)
0