sebastian/environment
sebastian/environment helps PHP libraries handle runtime-specific execution paths by detecting and describing the current environment (PHP version, features, etc.). Useful for writing portable code that adapts to different runtimes and configurations.
phpversion()/extension_loaded() calls across the codebase.if (Runtime::isPhp81() && !Runtime::isHhvm()) {
$this->enableNewFeature();
}
Artisan commands or custom scripts.Runtime::isOpcacheActive() or Runtime::getSettings().app()->environment() (high-level: local, production). Use this for low-level runtime details (PHP version, extensions, HHVM, OS).Runtime as a singleton for dependency injection:
$this->app->singleton(Runtime::class, fn() => new \SebastianBergmann\Environment\Runtime());
Access via:
public function __construct(private Runtime $runtime) {}
Environment) for consistency:
// app/Facades/Environment.php
public static function isPhp80(): bool {
return app(Runtime::class)->isPhpVersion('8.0');
}
config([
'app.supported_php_versions' => Environment::getSupportedPhpVersions(),
'app.enable_opcache' => Runtime::isOpcacheActive(),
]);
if (Runtime::isLinux()) {
$this->info('Linux-specific optimizations enabled');
}
public function testHhvmFeature() {
if (!Runtime::isHhvm()) {
$this->markTestSkipped('HHVM required');
}
}
Runtime::isHhvm() or Environment::detect() are stable; check changelog for deprecated methods (e.g., Runtime::getBinary()).version_compare(PHP_VERSION, '8.0.0') → Runtime::isPhp80()).Runtime::isHhvm() or Environment::isPhp81() to gate tests.Runtime::getSettings()) are called in hot paths.phpversion()/extension_loaded() checks exist? Audit and replace them systematically.if (!Environment::isPhp81()) exit(1)) or warn (e.g., log deprecation notices)?Runtime class or a Laravel facade (e.g., Environment::isPhp80()) for consistency?phpversion(), extension_loaded(), and HHVM_VERSION checks.composer require --dev sebastian/environment
if (version_compare(PHP_VERSION, '8.0.0') >= 0) → if (Runtime::isPhp80())).Environment) to standardize usage:
// app/Facades/Environment.php
public static function isPhp80(): bool {
return app(Runtime::class)->isPhpVersion('8.0');
}
Runtime to Laravel’s container for global access.Environment::isPhp81() instead of phpversion()").Runtime::hasExtension('intl')) without conflicts.Runtime::isLinux(), Runtime::isWindows(), etc.if (!Environment::isPhp81()) {
throw new \RuntimeException('PHP 8.1+ required');
}
if (Runtime::isHhvm()) $this->markTestSkipped('HHVM not supported');
Runtime::isPhp80() in feature toggles.if (Runtime::isHhvm()) {
Log::warning('HHVM is deprecated; migrating to PHP');
}
5
How can I help you explore Laravel packages today?