My use case was a cache warmer that should only be run on the servers but not in other places. The cache warmer connects to a server to pre-fetch data. The warmer is configured with environment variables / DotEnv variables.
This might come in handy if you need your own conditional services.
- Prevent autoconfigure for this service
- Have a compiler pass that checks if the environment variable is defined, tag service if it exists
To know if a variable exists, let the ContainerBuilder resolve the env placeholders with format
as true
:
$container->resolveEnvPlaceholders('%env(API_DOMAIN)%', true);
Note that this evaluates the environment variable in the context where the container is built. If you build the container in a ci environment and then push to services, this would not see environment variables available only on the server. For that scenario, you would need to run a separate command on your servers when initializing the application.