sebastian/environment
Runtime environment helper for PHP projects. Detects PHP/HHVM runtime specifics so you can choose safe execution paths, handle version and platform differences, and write portable code with minimal conditionals—useful in libraries and test tooling.
PHP_SAPI, stream_isatty(), or getenv() checks with a maintainable, feature-rich API (e.g., Runtime::isCli(), Host::supportsColor(), Runtime::canCollectCodeCoverage()).
FORCE_COLOR or TTY status.FORCE_COLOR=1.// Before (scattered)
if (PHP_SAPI === 'cli' && function_exists('stream_isatty') && stream_isatty(STDERR)) {
$this->output->setDecorated(true);
}
// After (centralized)
$this->output->setDecorated(Host::supportsColor());
FORCE_COLOR/NO_COLOR (v9.2.0+), aligning with Laravel’s push for consistent, accessible CLI tooling (used by npm, docker, pytest).Illuminate\Support\Console (both built on Symfony Console), reducing friction for Symfony-based packages.FORCE_COLOR (Laravel 9+). PHP 8.1 or lower can use ^8.0 (core features only).Runtime::isCli() or Host::supportsColor().composer require --dev sebastian/environment:^9.2 # Laravel 10+
composer require --dev sebastian/environment:^8.0 # Laravel 9.x
PHP_SAPI === 'cli' → Runtime::isCli()stream_isatty() → Host::supportsColor()getenv('FORCE_COLOR') → Host::getEnvironmentVariable('FORCE_COLOR')FORCE_COLOR:
FORCE_COLOR=1 in CI (.github/workflows/test.yml).Host::supportsColor() to auto-detect terminal capabilities.Runtime::getBinary()/getRawBinary() are deprecated (use PHP_BINARY constant). Audit CLI tools using these.^8.0 for older Laravel versions).FORCE_COLOR) are additive.FORCE_COLOR (e.g., GitHub Actions).PHP_SAPI checks may need refactoring.--ansi flags to scripts?FORCE_COLOR)?^8.0 (no FORCE_COLOR) suffice?FORCE_COLOR?Host::supportsColor() works in Docker/Sail environments?Host::supportsColor().Runtime::isCli() to conditionally enable/disable CLI-specific logic.Runtime::canCollectCodeCoverage() for Xdebug/opcache-aware logic.Illuminate\Support\Console (both use Symfony Console).Host::supportsColor() → $this->output->setDecorated().FORCE_COLOR=1 to enforce colored output.--ansi flags).Phase 1: Dependency Addition
composer.json (dev-only for tests/CLI tools):
"require-dev": {
"sebastian/environment": "^9.2" // Laravel 10+
// OR
"sebastian/environment": "^8.0" // Laravel 9.x
}
composer update.Phase 2: Core Logic Replacement
// Before
if (PHP_SAPI === 'cli' && stream_isatty(STDERR)) {
$this->output->setDecorated(true);
}
// After
$this->output->setDecorated(Host::supportsColor());
beforeAll(function () {
if (!Host::supportsColor()) {
putenv('FORCE_COLOR=1'); // Force colors in CI
}
});
use SebastianBergmann\Environment\Host;
if (Host::isCli() && Host::supportsColor()) {
// Enable colored output
}
Phase 3: CI/CD Configuration
.github/workflows/test.yml):
env:
FORCE_COLOR: "1" # Enforce colored output in CI
Phase 4: Deprecation Handling
Runtime::getBinary()/getRawBinary() and replace with PHP_BINARY.FORCE_COLOR (v9.2.0+). Use ^8.0 for older PHP.Console facade uses Symfony).Runtime/Host classes (avoid deprecated methods).require-dev to avoid bloating production builds.FORCE_COLOR=1.How can I help you explore Laravel packages today?