Skip to content

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.

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

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;
}

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');
}
}

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',
};
}
}
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();
}
}
}

The environment service is automatically registered and can be resolved in multiple ways:

// Via dependency injection
public function __construct(ApplicationEnvironmentService $env) {}
// Via service locator
$environment = app(ApplicationEnvironmentService::class);
// Via alias
$environment = app('pollora.environment');
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 environments are configured through the standard .env file:

Terminal window
APP_ENV=local
APP_DEBUG=true
# Other environment-specific settings
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_CONNECTION=sync

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.

// Check for custom environment
if ($environment->isEnvironment('testing')) {
$this->setupTestDatabase();
}
// Environment-specific feature flags
$features = [
'debug_toolbar' => $environment->isDevelopment(),
'query_logging' => !$environment->isProduction(),
'cache_warmup' => $environment->isProduction(),
];
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);
}
}
}