EnvironmentDetector
in package
Detects the current application environment
Provides consistent environment detection across the framework. Checks various sources (environment variables, server variables) to determine if the application is running in development, test, staging, or production.
IMPORTANT: Production environments MUST explicitly set APP_ENV=production This is especially critical for CLI contexts (cron jobs, queue workers, artisan commands) which will default to 'development' if no environment is explicitly configured.
Priority order for environment detection:
- APP_ENV environment variable
- NEURON_ENV environment variable
- APPLICATION_ENV environment variable
- ENVIRONMENT environment variable
- Common development indicators (localhost, debug tools)
- Default to 'development' (fail-safe)
Table of Contents
Constants
- ENV_VARIABLES = ['APP_ENV', 'NEURON_ENV', 'APPLICATION_ENV', 'ENVIRONMENT']
- Environment variable names to check (in priority order)
- VALID_ENVIRONMENTS = ['development', 'test', 'staging', 'production']
- Valid environment names
Methods
- detect() : string
- Detect the current environment
- getValidEnvironments() : array<string|int, mixed>
- Get all valid environment names
- isDevelopment() : bool
- Check if current environment is development
- isProduction() : bool
- Check if current environment is production
- isStaging() : bool
- Check if current environment is staging
- isTest() : bool
- Check if current environment is test
- isValidEnvironment() : bool
- Check if an environment name is valid
- isDevelopmentEnvironment() : bool
- Check for common development environment indicators
- normalizeEnvironment() : string|null
- Normalize environment name to standard format
Constants
ENV_VARIABLES
Environment variable names to check (in priority order)
private
mixed
ENV_VARIABLES
= ['APP_ENV', 'NEURON_ENV', 'APPLICATION_ENV', 'ENVIRONMENT']
VALID_ENVIRONMENTS
Valid environment names
private
mixed
VALID_ENVIRONMENTS
= ['development', 'test', 'staging', 'production']
Methods
detect()
Detect the current environment
public
static detect() : string
Return values
string —One of: development, test, staging, production
getValidEnvironments()
Get all valid environment names
public
static getValidEnvironments() : array<string|int, mixed>
Return values
array<string|int, mixed>isDevelopment()
Check if current environment is development
public
static isDevelopment() : bool
Return values
boolisProduction()
Check if current environment is production
public
static isProduction() : bool
Return values
boolisStaging()
Check if current environment is staging
public
static isStaging() : bool
Return values
boolisTest()
Check if current environment is test
public
static isTest() : bool
Return values
boolisValidEnvironment()
Check if an environment name is valid
public
static isValidEnvironment(string $environment) : bool
Parameters
- $environment : string
Return values
boolisDevelopmentEnvironment()
Check for common development environment indicators
private
static isDevelopmentEnvironment() : bool
NOTE: This method only checks for obvious development indicators. Production environments should ALWAYS explicitly set APP_ENV=production to avoid any ambiguity, especially for CLI contexts like cron jobs, queue workers, and artisan commands.
Return values
boolnormalizeEnvironment()
Normalize environment name to standard format
private
static normalizeEnvironment(string $environment) : string|null
Parameters
- $environment : string
-
Raw environment name
Return values
string|null —Normalized environment or null if invalid