fi/osbundle
OsBundle (fi/osbundle) is a Symfony bundle providing integration components for Comune di Firenze projects. Install via Composer and add PSR-4 autoload mapping Fi\OsBundle\ to vendor/fi/osbundle, then require version 2.0.*.
Installation
Add to composer.json:
"require": {
"fi/osbundle": "2.0.*"
},
"autoload": {
"psr-4": {
"Fi\\OsBundle\\": "vendor/fi/osbundle/"
}
}
Run composer update.
First Use Case Detect the OS in a controller or service:
use Fi\OsBundle\OsBundle;
$osDetector = new OsBundle();
$osName = $osDetector->getOsName(); // e.g., "Windows", "Linux", "MacOS"
$osVersion = $osDetector->getOsVersion(); // e.g., "10.0", "5.4"
Where to Look First
src/OsBundle.php: Core logic for OS detection.tests/: Example use cases and edge-case handling.README.md: Quick reference for methods.Conditional Logic Based on OS Use OS detection to tailor behavior (e.g., file paths, CLI commands):
if ($osDetector->isWindows()) {
$command = 'dir';
} elseif ($osDetector->isLinux()) {
$command = 'ls';
}
Service Integration
Register the bundle in config/services.php:
return [
'os_detector' => Fi\OsBundle\OsBundle::class,
];
Inject via constructor:
public function __construct(private OsBundle $osDetector) {}
Environment-Specific Config
Load OS-dependent configs in config/os.php:
return [
'windows' => ['path_separator' => '\\'],
'linux' => ['path_separator' => '/'],
];
Access via:
$config = config('os.' . $osDetector->getOsName());
kernel.request:
$event->setResponse(new Response("Optimized for {$osDetector->getOsName()}"));
// src/Twig/AppExtension.php
public function getFunctions()
{
return [
new \Twig\TwigFunction('os_name', [$this->osDetector, 'getOsName']),
];
}
Usage in template:
{{ os_name() }} is detected.
False Positives in Detection
OsBundle or override detection logic:
$osDetector->setCustomOsName('Alpine'); // Force override
Case Sensitivity
getOsName() returns lowercase (e.g., "windows"), but isWindows() is case-insensitive.if (strtolower($osDetector->getOsName()) === 'windows') { ... }
Version Parsing Quirks
getOsVersion() may return raw output (e.g., "5.4.0-42-generic").preg_match('/\d+\.\d+/', $osDetector->getOsVersion(), $matches);
$version = $matches[0] ?? 'unknown';
Log OS Data Dump OS info for debugging:
\Log::debug('OS:', [
'name' => $osDetector->getOsName(),
'version' => $osDetector->getOsVersion(),
'raw' => $osDetector->getRawOsInfo(),
]);
Test Edge Cases Mock OS detection in tests:
$osDetector = $this->createMock(OsBundle::class);
$osDetector->method('getOsName')->willReturn('Linux');
Custom OS Detection Extend the base class:
class CustomOsBundle extends OsBundle {
public function isCustomOs(): bool {
return $this->getOsName() === 'custom';
}
}
Add New Methods Dynamically attach OS-specific helpers:
$osDetector->addMethod('isDocker', function() {
return file_exists('/.dockerenv');
});
Configuration Overrides
Override default behavior via config/os.php:
'detection' => [
'windows' => ['regex' => '/Windows NT 10.0/'],
],
How can I help you explore Laravel packages today?