sebastian/environment
sebastian/environment helps PHP developers detect runtime details (PHP vs HHVM, OS, architecture, 32/64-bit, debugging extensions) to choose runtime-specific execution paths. Commonly used by testing tools to adapt behavior to the environment.
Installation:
composer require --dev sebastian/environment
(Use --dev if only needed for testing/validation.)
First Use Case: Detect the runtime environment in a Laravel service or Artisan command:
use SebastianBergmann\Environment\Runtime;
if (Runtime::isHhvm()) {
// HHVM-specific logic (e.g., legacy compatibility)
Log::warning('HHVM detected; enabling polyfills.');
}
if (Runtime::isPhp74()) {
// PHP 7.4+ features (e.g., typed properties)
$this->enableTypedProperties();
}
Where to Look First:
Runtime, Environment, OperatingSystem, PhpVersion.Runtime::isHhvm() / Runtime::isPhp()Runtime::getPhpVersion()Runtime::getCurrentSettings() (for php.ini overrides)OperatingSystem::isLinux() / OperatingSystem::isWindows()Dynamically enable/disable features based on PHP/HHVM:
// app/Providers/AppServiceProvider.php
public function register()
{
if (Runtime::isHhvm()) {
$this->app->register(HhvmPolyfillServiceProvider::class);
}
}
Fail builds on unsupported PHP versions in phpunit.xml or GitHub Actions:
<!-- phpunit.xml -->
<php>
<env name="MIN_PHP_VERSION" value="8.0.0"/>
<ini name="error_reporting" value="-1"/>
<phpunit bootloader="vendor/autotest/phpunit/bootstrap.php">
<listeners>
<listener class="SebastianBergmann\Environment\PhpVersionListener"/>
</listeners>
</phpunit>
Skip HHVM-specific tests on PHP-only CI:
// tests/Feature/HhvmTest.php
public function setUp(): void
{
if (!Runtime::isHhvm()) {
$this->markTestSkipped('HHVM required for this test.');
}
}
Load environment-specific configs:
// config/app.php
'php_version' => Runtime::getPhpVersion(),
'is_hhvm' => Runtime::isHhvm(),
Add runtime checks to commands:
// app/Console/Commands/CheckEnvironment.php
protected function handle()
{
if (Runtime::isPhp74() && !Runtime::isPhp80()) {
$this->error('PHP 7.4 is deprecated; upgrade to 8.0+.');
exit(1);
}
}
// app/Providers/EnvironmentServiceProvider.php
public function register()
{
$this->app->singleton(Runtime::class, function () {
return new \SebastianBergmann\Environment\Runtime();
});
}
use Illuminate\Support\Facades\App;
if (App::make(Runtime::class)->isPhp80()) {
// Enable PHP 8.0+ features
}
// app/Facades/Environment.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
use SebastianBergmann\Environment\Runtime;
class Environment extends Facade
{
protected static function getFacadeAccessor()
{
return Runtime::class;
}
public static function isPhp81()
{
return static::getFacadeRoot()->isPhp('8.1');
}
}
@if(App\Facades\Environment::isPhp81())
<p>PHP 8.1 features enabled.</p>
@endif
// tests/TestCase.php
protected function getEnvironmentStub()
{
return $this->mock(Runtime::class, function ($mock) {
$mock->shouldReceive('isHhvm')->andReturn(true);
});
}
public function testHhvmFeatureFlag()
{
$this->getEnvironmentStub();
$this->assertTrue(app()->hasHhvmFeatures());
}
Avoid Overuse:
app()->environment() for high-level checks (e.g., local, production).sebastian/environment for low-level runtime details (e.g., PHP version, HHVM, OS).Combine with Laravel Helpers:
if (app()->environment('local') && Runtime::isPhp74()) {
// Local dev + PHP 7.4: enable debug tools
}
Leverage Runtime::getCurrentSettings():
php.ini overrides in tests or CLI tools:
$settings = Runtime::getCurrentSettings();
if ($settings['opcache.enable'] !== '1') {
$this->error('OPcache is disabled.');
}
OS-Specific Logic:
if (OperatingSystem::isLinux()) {
$binaryPath = '/usr/bin/php';
}
Deprecation Strategy:
if (Runtime::isHhvm()) {
Log::warning('HHVM is deprecated; upgrade to PHP.');
}
HHVM is Deprecated:
if (Runtime::isHhvm()) {
throw new \RuntimeException('HHVM is no longer supported. Use PHP 8.0+.');
}
False Positives in Runtime::isPhp():
false for HHVM, even if HHVM emulates PHP. Use Runtime::isHhvm() for explicit checks.Runtime::getCurrentSettings() Quirks:
php -d round-trip (fixed in v9.3.2+).php.ini settings (e.g., xdebug.mode) may not be detected.$_SERVER or ini_get_all() if needed.PHP Version Detection Edge Cases:
Runtime::isPhp('8.0') includes PHP 8.0.x but not 8.1+.Runtime::getPhpVersion() for precise comparisons:
$version = Runtime::getPhpVersion();
if (version_compare($version, '8.1.0') >= 0) {
// PHP 8.1+
}
TTY/Non-TTY Warnings:
Console::hasColorSupport()) may trigger warnings in non-TTY environments (fixed in v9.0.1+).@Runtime::hasColorSupport();
Deprecated Methods:
Runtime::getBinary() (use escapeshellarg(PHP_BINARY)).Runtime::getRawBinary() (use PHP_BINARY constant).Inspect Runtime Settings:
dd(Runtime::getCurrentSettings());
php.ini or environment mismatches.Check PHP Version:
dd(Runtime::getPhpVersion(), PHP_VERSION);
php -v output to verify detection.OS Detection:
dd(OperatingSystem::getName());
/usr/bin/php vs. C:\php\php.exe).**HHVM vs.
How can I help you explore Laravel packages today?