boson-php/os-info
Lightweight PHP library to detect and describe the current operating system, exposing basic OS name/version/arch details for CLI and server environments. Useful for cross-platform scripts, installers, and runtime diagnostics.
Installation
composer require boson-php/os-info
No additional configuration is required—this is a lightweight, dependency-free package.
First Use Case Quickly fetch OS details in a Laravel controller or service:
use Boson\OSInfo\OSInfo;
$osInfo = new OSInfo();
$osDetails = $osInfo->getOSInfo();
// Example output:
// [
// 'name' => 'Linux',
// 'version' => '5.4.0-135-generic',
// 'family' => 'Debian',
// 'kernel' => '5.4.0-135-generic',
// ...
// ]
Where to Look First
OSInfo (minimal docs, but simple API).tests/ for edge-case examples (e.g., Windows, macOS, containers).src/OSInfo.php for direct inspection of parsing logic.OS Detection in Middleware Use to dynamically adjust behavior (e.g., redirect Windows users to a different setup guide):
namespace App\Http\Middleware;
use Boson\OSInfo\OSInfo;
use Closure;
class OSDetector
{
public function handle($request, Closure $next)
{
$osInfo = (new OSInfo())->getOSInfo();
if ($osInfo['name'] === 'Windows') {
return redirect()->route('windows.setup');
}
return $next($request);
}
}
Feature Flagging by OS Enable/disable features based on OS compatibility:
$osInfo = (new OSInfo())->getOSInfo();
if ($osInfo['family'] === 'Darwin') {
Feature::enable('mac-specific-feature');
}
Logging/Monitoring Log OS details for analytics or debugging:
\Log::info('User OS', [
'os' => $osInfo->getOSInfo(),
'user_agent' => $request->userAgent(),
]);
$osInfo = Cache::remember('os_info', 86400, fn() => (new OSInfo())->getOSInfo());
OSInfo to the container for DI:
$this->app->singleton(OSInfo::class, fn() => new OSInfo());
$osInfo = app(OSInfo::class)->getOSInfo();
$this->info("Server OS: {$osInfo['name']} {$osInfo['version']}");
Container/VM Detection
unknown for some containerized environments (e.g., Docker). Handle gracefully:
$osInfo = (new OSInfo())->getOSInfo();
if ($osInfo['name'] === 'unknown') {
$osInfo['name'] = 'Containerized';
}
php-uname for richer detection.Windows Quirks
if (strpos($osInfo['version'], '10.0') === 0) {
$osInfo['version'] = 'Windows 10+';
}
Performance
OSInfo repeatedly in loops. Reuse the object or cache results.Edge Cases
Darwin as name (not macOS). Normalize:
$osInfo['name'] = $osInfo['name'] === 'Darwin' ? 'macOS' : $osInfo['name'];
dd((new OSInfo())->getOSInfo());
composer test
Custom Parsing Extend the class to add OS-specific logic:
class ExtendedOSInfo extends OSInfo
{
public function isWindows11(): bool
{
$info = $this->getOSInfo();
return $info['name'] === 'Windows' && strpos($info['version'], '10.0.22000') === 0;
}
}
Add New OS Families
Modify the parsing logic in src/OSInfo.php to support niche systems (e.g., FreeBSD, Alpine Linux).
Integration with Laravel Scout Use OS data for search filtering:
User::where('os_family', $osInfo['family'])->search($query);
How can I help you explore Laravel packages today?