Environment Management
Detect and manage environments in Pollora, from local to staging and production, with one API that works in both WordPress and Laravel contexts.
The Pollora framework provides a comprehensive environment management system that works seamlessly in both WordPress and Laravel contexts. This system follows hexagonal architecture principles to ensure clean separation of concerns and easy testability.
Overview
Section titled “Overview”The environment management system consists of:
- Environment Detection: Automatic detection of the current environment (development, production, staging, etc.)
- Context-Aware Implementation: Different detection strategies for WordPress and Laravel environments
- Service Layer: High-level service for environment-based decision making
- Performance Optimization: Environment-based caching strategies
Architecture
Section titled “Architecture”Domain Layer
Section titled “Domain Layer”EnvironmentDetectorInterface (src/Application/Domain/Contracts/EnvironmentDetectorInterface.php)
The core contract defining environment detection capabilities:
interface EnvironmentDetectorInterface{ public function getEnvironment(): string; public function isProduction(): bool; public function isDevelopment(): bool; public function isStaging(): bool; public function isEnvironment(string $environment): bool;}Infrastructure Layer
Section titled “Infrastructure Layer”WordPressEnvironmentDetector (src/Application/Infrastructure/Services/WordPressEnvironmentDetector.php)
Uses WordPress’s native wp_get_environment_type() function:
class WordPressEnvironmentDetector implements EnvironmentDetectorInterface{ public function getEnvironment(): string { return wp_get_environment_type() ?: 'production'; }
public function isDevelopment(): bool { return $this->getEnvironment() === 'development'; }}LaravelEnvironmentDetector (src/Application/Infrastructure/Services/LaravelEnvironmentDetector.php)
Uses Laravel’s application instance for environment detection:
class LaravelEnvironmentDetector implements EnvironmentDetectorInterface{ public function __construct(private readonly Application $app) {}
public function getEnvironment(): string { return $this->app->environment() ?? 'production'; }
public function isDevelopment(): bool { return $this->app->environment('local', 'development'); }}Application Layer
Section titled “Application Layer”ApplicationEnvironmentService (src/Application/Application/Services/ApplicationEnvironmentService.php)
High-level service providing environment-based utilities:
class ApplicationEnvironmentService{ public function __construct( private readonly EnvironmentDetectorInterface $detector ) {}
public function shouldEnableDebug(): bool { return $this->isDevelopment() || $this->isStaging(); }
public function shouldEnableCache(): bool { return $this->isProduction() || $this->isStaging(); }
public function getConfigPrefix(): string { return match ($this->getEnvironment()) { 'development' => 'dev', 'staging' => 'stage', 'production' => 'prod', default => 'default', }; }}Basic Environment Detection
Section titled “Basic Environment Detection”use Pollora\Application\Application\Services\ApplicationEnvironmentService;
class MyController{ public function __construct( private readonly ApplicationEnvironmentService $environment ) {}
public function index() { if ($this->environment->isProduction()) { // Production-specific logic $this->enableCaching(); }
if ($this->environment->isDevelopment()) { // Development-specific logic $this->enableDebugMode(); } }}Service Container Resolution
Section titled “Service Container Resolution”The environment service is automatically registered and can be resolved in multiple ways:
// Via dependency injectionpublic function __construct(ApplicationEnvironmentService $env) {}
// Via service locator$environment = app(ApplicationEnvironmentService::class);
// Via alias$environment = app('pollora.environment');Environment-Specific Configuration
Section titled “Environment-Specific Configuration”class ConfigurationService{ public function __construct( private readonly ApplicationEnvironmentService $environment ) {}
public function getCacheSettings(): array { $prefix = $this->environment->getConfigPrefix();
return [ 'enabled' => $this->environment->shouldEnableCache(), 'ttl' => $this->environment->isProduction() ? 3600 : 60, 'prefix' => $prefix . '_cache_', ]; }}Laravel Environment Configuration
Section titled “Laravel Environment Configuration”Laravel environments are configured through the standard .env file:
APP_ENV=localAPP_DEBUG=true
# Other environment-specific settingsCACHE_DRIVER=fileSESSION_DRIVER=fileQUEUE_CONNECTION=syncPerformance Integration
Section titled “Performance Integration”Debug mode decides how discovery caches its scans. With APP_DEBUG=true the persistent cache is off, so a class you add is found on the next request, and a slow location is logged; otherwise scans are stored in Laravel’s cache until php artisan discovery:clear. The choice is made when the framework builds the discovery cache manager (LaravelDiscoveryCacheManagerFactory), from the application’s debug mode rather than its environment name.
Advanced Usage
Section titled “Advanced Usage”Custom Environment Types
Section titled “Custom Environment Types”// Check for custom environmentif ($environment->isEnvironment('testing')) { $this->setupTestDatabase();}
// Environment-specific feature flags$features = [ 'debug_toolbar' => $environment->isDevelopment(), 'query_logging' => !$environment->isProduction(), 'cache_warmup' => $environment->isProduction(),];Environment-Based Service Registration
Section titled “Environment-Based Service Registration”class CustomServiceProvider extends ServiceProvider{ public function register(): void { $environment = $this->app->make(ApplicationEnvironmentService::class);
if ($environment->isDevelopment()) { $this->app->register(DevelopmentServiceProvider::class); }
if ($environment->isProduction()) { $this->app->register(ProductionOptimizationProvider::class); } }}