php-standard-library/os
Type-safe OS detection for PHP via an OperatingSystemFamily enum. Quickly identify Windows, macOS, Linux, and more without brittle string checks. Part of PHP Standard Library; lightweight and focused on reliable environment detection.
Installation
composer require php-standard-library/os
Add to compser.json under require:
"php-standard-library/os": "^1.0"
First Usage Detect the current OS family in a Laravel service or controller:
use PhpStandardLibrary\Os\OperatingSystemFamily;
$osFamily = OperatingSystemFamily::current();
$isLinux = $osFamily->is(OperatingSystemFamily::LINUX);
Key Entry Points
OperatingSystemFamily::current() → Returns the detected OperatingSystemFamily enum.$family->is($target) → Type-safe comparison (e.g., is(OperatingSystemFamily::WINDOWS)).$family->value → Raw string (e.g., "linux", "darwin").Use the enum to gate functionality in Laravel services:
public function handle()
{
$os = OperatingSystemFamily::current();
if ($os->is(OperatingSystemFamily::WINDOWS)) {
$this->configureWindowsSpecificSettings();
} elseif ($os->is(OperatingSystemFamily::LINUX)) {
$this->configureLinuxSpecificSettings();
}
}
Bind the enum to Laravel’s container for reusable access:
// In a service provider
$this->app->singleton(OperatingSystemFamily::class, function () {
return OperatingSystemFamily::current();
});
// Inject anywhere
public function __construct(private OperatingSystemFamily $os) {}
Use the enum to load OS-specific configs (e.g., config/os.php):
$configPath = match ($os->value) {
'windows' => 'config/windows.php',
'linux' => 'config/linux.php',
default => 'config/default.php',
};
config([$configPath => require $configPath]);
Detect OS in CLI tools:
use PhpStandardLibrary\Os\OperatingSystemFamily;
protected function handle()
{
if (OperatingSystemFamily::current()->is(OperatingSystemFamily::DARWIN)) {
$this->info('Running on macOS. Skipping Windows-specific tasks.');
}
}
Mock OS detection in tests:
use PhpStandardLibrary\Os\OperatingSystemFamily;
public function testLinuxBehavior()
{
$this->app->instance(OperatingSystemFamily::class, OperatingSystemFamily::LINUX);
$this->assertTrue(app(OperatingSystemFamily::class)->is(OperatingSystemFamily::LINUX));
}
Case Sensitivity
The enum uses lowercase values ("linux", "windows"), but some systems (e.g., uname on macOS) return "Darwin". The package normalizes this, but verify edge cases like:
// Avoid hardcoding strings; always use the enum.
if ($os->value === 'darwin') { ... } // ❌ Fragile
if ($os->is(OperatingSystemFamily::DARWIN)) { ... } // ✅ Safe
Container Binding Conflicts
If you bind OperatingSystemFamily::class to the container, ensure it’s not overridden by Laravel’s service providers. Use when() or unless() in bindings:
$this->app->when(OperatingSystemFamily::class)
->needs('$os')
->give(fn () => OperatingSystemFamily::current());
Dynamic Environments The package detects OS at runtime. For Docker/Kubernetes, test in isolated environments:
# Test in a Linux container
docker run --rm -it ubuntu bash -c "composer require php-standard-library/os && php -r 'var_dump(\PhpStandardLibrary\Os\OperatingSystemFamily::current()->value);'"
Unexpected Values? Log the raw detection:
$detector = new \PhpStandardLibrary\Os\OperatingSystemDetector();
logger()->debug('Raw OS detection:', ['value' => $detector->detect()]);
Common culprits: Docker containers reporting "linux" even on macOS hosts.
Enum Extensions The enum is final, but you can extend detection logic:
class CustomDetector extends \PhpStandardLibrary\Os\OperatingSystemDetector
{
public function detect(): string
{
$os = parent::detect();
return $os === 'linux' && $this->isDocker() ? 'docker' : $os;
}
private function isDocker(): bool { ... }
}
Laravel Facades Create a facade for cleaner syntax:
// app/Facades/Os.php
public static function family(): OperatingSystemFamily { return OperatingSystemFamily::current(); }
Usage:
if (Os::family()->is(OperatingSystemFamily::WINDOWS)) { ... }
Performance Cache the detection in a singleton if called frequently:
$this->app->singleton(OperatingSystemFamily::class, fn () => OperatingSystemFamily::current());
CI/CD Integration Use the enum to skip OS-specific tests in GitHub Actions:
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: php -r "if (\PhpStandardLibrary\Os\OperatingSystemFamily::current()->is(\PhpStandardLibrary\Os\OperatingSystemFamily::WINDOWS)) exit(1);"
Local Development Override OS detection for testing:
// config/os.php
'override' => env('APP_OS_OVERRIDE', null), // e.g., "windows"
Then extend the detector to respect this env var.
How can I help you explore Laravel packages today?