@@ -993,20 +993,19 @@ environment variables, with their values, referenced in Symfony's container conf
993
993
# run this command to show all the details for a specific env var:
994
994
$ php bin/console debug:container --env-var=FOO
995
995
996
- Create Your Own Logic To Load Env Vars
997
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
996
+ Creating Your Own Logic To Load Env Vars
997
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
998
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 `.
999
+ You can implement your own logic to load environment variables if the default
1000
+ Symfony behavior doesn't fit your needs. To do so, create a service whose class
1001
+ implements :class: `Symfony\\ Component\\ DependencyInjection\\ EnvVarLoaderInterface `.
1003
1002
1004
1003
.. note ::
1005
1004
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/tags >` .
1005
+ If you're using the :ref: ` default services.yaml configuration < service-container-services-load-example >`,
1006
+ the autoconfiguration feature will enable and tag thise service automatically.
1007
+ Otherwise, you need to register and :doc: ` tag your service < /service_container/tags >`
1008
+ with the `` container.env_var_loader `` tag .
1010
1009
1011
1010
Let's say you have a JSON file named ``env.json `` containing your environment
1012
1011
variables:
@@ -1020,22 +1019,22 @@ variables:
1020
1019
}
1021
1020
}
1022
1021
1023
- We can create a new class named ``JsonEnvVarLoader `` to populate our environment
1024
- variables from the file::
1022
+ You can define a class like the following ``JsonEnvVarLoader `` to populate the
1023
+ environment variables from the file::
1025
1024
1026
1025
namespace App\DependencyInjection;
1027
1026
1028
1027
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
1029
1028
1030
- class JsonEnvVarLoader implements EnvVarLoaderInterface
1029
+ final class JsonEnvVarLoader implements EnvVarLoaderInterface
1031
1030
{
1032
1031
private const ENV_VARS_FILE = 'env.json';
1033
1032
1034
1033
public function loadEnvVars(): array
1035
1034
{
1036
1035
$fileName = __DIR__.\DIRECTORY_SEPARATOR.self::ENV_VARS_FILE;
1037
1036
if (!is_file($fileName)) {
1038
- // throw an error or just ignore this loader, depending on your needs
1037
+ // throw an exception or just ignore this loader, depending on your needs
1039
1038
}
1040
1039
1041
1040
$content = json_decode(file_get_contents($fileName), true);
@@ -1044,9 +1043,9 @@ variables from the file::
1044
1043
}
1045
1044
}
1046
1045
1047
- That's it! Now the application will look at a ``env.json `` file in the
1048
- current directory to populate environment variables, additionally to the
1049
- already existing ``.env `` files.
1046
+ That's it! Now the application will look for a ``env.json `` file in the
1047
+ current directory to populate environment variables (in addition to the
1048
+ already existing ``.env `` files) .
1050
1049
1051
1050
.. tip ::
1052
1051
0 commit comments