Limit services per environment
This module allows you to conditionally enable or disable Drupal services based on the current environment, helping to use different service implementations for different environments without adding conditional logic to your code.
This module allows you to enable or disable Drupal services based on the current environment, inspired by Symfony's per-environment service configuration.
This is useful when you need different service implementations per environment — for example, using a real API client in production and a logger or mock in development/staging, without conditional logic in your code.
Features
#[ConditionalServices\Attribute\WhenEnv]— register a service only whenDRUPAL_ENVmatches a given value#[ConditionalServices\Attribute\WhenNotEnv]— register a service only whenDRUPAL_ENVdoes NOT match a given value- Works with any service tagged with
kernel.event_subscriber,logger, or custom tags viatagsattribute parameter
Requirements
Set the DRUPAL_ENV environment variable to any string identifying your environment (e.g. production, development, staging).
Examples
The module includes a conditional_services_examples submodule with sample controllers and services demonstrating both attributes in action.