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.
composer require --dev sebastian/environment
use SebastianBergmann\Environment\Host;
class MyCommand extends Command {
protected function configure() {
$this->getOutput()->setDecorated(Host::supportsColor());
}
}
Host: For terminal/color detection (supportsColor(), getEnvironmentVariable()).Runtime: For PHP runtime checks (isCli(), canCollectCodeCoverage(), isOpcacheActive()).FORCE_COLOR (v9.2.0) and PHP 8.3+ support.Host::supportsColor() in all CLI tools (Artisan, Pest, custom scripts).
if (Host::supportsColor()) {
$this->info('<fg=green>Success!</>');
}
# GitHub Actions
env:
FORCE_COLOR: "1"
if (Host::getEnvironmentVariable('FORCE_COLOR') === '0') {
putenv('FORCE_COLOR=1'); // Force colors in containers
}
Runtime for conditional execution paths.
if (Runtime::isCli()) {
// CLI-only logic (e.g., Artisan commands)
}
if (Runtime::canCollectCodeCoverage()) {
// Enable Xdebug coverage
}
if (Runtime::isOpcacheActive()) {
$this->warn('Opcache is enabled. Clear it for testing.');
}
APP_DEBUG, FORCE_COLOR).
$debugMode = Host::getEnvironmentVariable('APP_DEBUG') === 'true';
$forceColor = Host::getEnvironmentVariable('FORCE_COLOR') === '1';
use Illuminate\Support\Command;
use SebastianBergmann\Environment\Host;
class DeployCommand extends Command {
protected function configure() {
$this->getOutput()->setDecorated(Host::supportsColor());
}
}
beforeAll(function () {
if (!Host::supportsColor()) {
putenv('FORCE_COLOR=1'); // Ensure CI has colors
}
});
Illuminate\Support\Console (backed by Symfony).Runtime::isCli() instead of PHP_SAPI === 'cli' for broader SAPI support (e.g., PHP-CGI).Host::supportsColor() are lazy-loaded—no runtime overhead in non-CLI contexts.Host/Runtime in unit tests:
$host = $this->createMock(Host::class);
$host->method('supportsColor')->willReturn(true);
PHP Version Mismatch:
Class 'SebastianBergmann\Environment\Host' not found if using PHP <8.2.^8.0 for PHP 8.2–8.3 (no FORCE_COLOR support).^9.2.^8.0.Deprecated Methods:
Runtime::getBinary() or getRawBinary() triggers deprecation warnings.escapeshellarg(PHP_BINARY) or PHP_BINARY.TTY Detection in Docker:
Host::supportsColor() may return false in non-interactive Docker containers.FORCE_COLOR=1 in your Dockerfile or CI:
ENV FORCE_COLOR=1
Xdebug Coverage Edge Cases:
Runtime::canCollectCodeCoverage() may return false even when Xdebug is enabled (e.g., in CI).extension_loaded('xdebug') if needed.Windows CMD Quirks:
Host::supportsColor() (handles Windows-specific logic internally).FORCE_COLOR:
echo Host::getEnvironmentVariable('FORCE_COLOR'); // Check current value
var_dump([
'isCli' => Runtime::isCli(),
'supportsColor' => Host::supportsColor(),
'opcacheActive' => Runtime::isOpcacheActive(),
]);
env:
FORCE_COLOR: "1"
APP_DEBUG: "true"
Host to read project-specific vars:
$customVar = Host::getEnvironmentVariable('MY_CUSTOM_VAR', 'default');
Host for custom logic:
class CustomHost extends Host {
public function supportsColor(): bool {
return parent::supportsColor() || $this->isCustomCondition();
}
}
Runtime for project-specific needs:
Runtime::registerCustomCheck('my_check', fn() => someLogic());
$this->app->singleton(Host::class, fn() => new Host());
FORCE_COLOR Priority:
The package respects FORCE_COLOR over TTY detection. Set it in CI to enforce colors:
FORCE_COLOR=1 php artisan test
NO_COLOR Support:
While not natively supported, you can manually check:
if (Host::getEnvironmentVariable('NO_COLOR') === '1') {
putenv('FORCE_COLOR=0');
}
FORCE_COLOR = force_color). Use Host::getEnvironmentVariable() for consistency.How can I help you explore Laravel packages today?