|
| 1 | +# Operating System Info |
| 2 | + |
| 3 | +<show-structure for="chapter" depth="2"/> |
| 4 | + |
| 5 | +The OS Info component provides a robust and flexible way to detect and work with |
| 6 | +operating system information in your applications. It offers a comprehensive |
| 7 | +set of features for identifying operating systems, their families, and |
| 8 | +supported standards. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +<tldr> |
| 13 | + <p> |
| 14 | + Via <a href="https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies">Composer</a>: |
| 15 | + </p> |
| 16 | + <p> |
| 17 | + <code lang="bash">composer require boson-php/os-info</code> |
| 18 | + </p> |
| 19 | +</tldr> |
| 20 | + |
| 21 | +**Requirements:** |
| 22 | + |
| 23 | +* `PHP ^8.4` |
| 24 | +* `ext-ffi` (optional, provides more detailed and accurate information about the OS) |
| 25 | +* `ext-com_dotnet` (optional, provides more detailed and accurate information about the OS) |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Basic Detection |
| 30 | + |
| 31 | +```php |
| 32 | +use Boson\Component\OsInfo\OperatingSystem; |
| 33 | + |
| 34 | +// Get current operating system information |
| 35 | +$os = OperatingSystem::createFromGlobals(); |
| 36 | + |
| 37 | +// Access basic information |
| 38 | +echo 'Family: ' . $os->family . "\n"; |
| 39 | +echo 'Name: ' . $os->name . "\n"; |
| 40 | +echo 'Version: ' . $os->version . "\n"; |
| 41 | +echo 'Codename: ' . ($os->codename ?? '~') . "\n"; |
| 42 | +echo 'Edition: ' . ($os->edition ?? '~') . "\n"; |
| 43 | +echo 'Standards: ' . implode(', ', $os->standards) . "\n"; |
| 44 | +``` |
| 45 | + |
| 46 | +This code will output something like the following information |
| 47 | + |
| 48 | +<tabs> |
| 49 | + <tab title="Windows"> |
| 50 | + <code-block> |
| 51 | + Family: Windows |
| 52 | + Name: Windows 10 Pro |
| 53 | + Version: 10.0.19045 |
| 54 | + Codename: 22H2 |
| 55 | + Edition: Professional |
| 56 | + Standards: ~ |
| 57 | + </code-block> |
| 58 | + </tab> |
| 59 | + <tab title="Linux"> |
| 60 | + <code-block> |
| 61 | + Family: Linux |
| 62 | + Name: Ubuntu |
| 63 | + Version: 20.04.6 |
| 64 | + Codename: Focal Fossa |
| 65 | + Edition: ~ |
| 66 | + Standards: POSIX |
| 67 | + </code-block> |
| 68 | + </tab> |
| 69 | +</tabs> |
| 70 | + |
| 71 | +### OS Families |
| 72 | + |
| 73 | +You can get the OS family information from the OS information |
| 74 | +object (`$os->family`). However, if you do not need all the OS information, |
| 75 | +it is enough to get the family separately using the `Family::createFromGlobals()` |
| 76 | +method. |
| 77 | + |
| 78 | +```php |
| 79 | +use Boson\Component\OsInfo\Family; |
| 80 | + |
| 81 | +// Get current OS family |
| 82 | +$family = Family::createFromGlobals(); |
| 83 | + |
| 84 | +// Check for specific family |
| 85 | +if ($family->is(Family::Windows)) { |
| 86 | + // Windows-specific (or windows-like) code |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +Please note that the `$family->is()` check includes the check of the family |
| 91 | +itself and its parents. |
| 92 | + |
| 93 | +```php |
| 94 | +if ($family->is(Family::Unix)) { |
| 95 | + // All operating systems from the Unix family |
| 96 | + // will be subject to this check: |
| 97 | + // - Darwin (macOS) |
| 98 | + // - Linux |
| 99 | + // - BSD |
| 100 | + // - ...and other Unix-like |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### Standards Support |
| 105 | + |
| 106 | +```php |
| 107 | +use Boson\Component\OsInfo\OperatingSystem; |
| 108 | +use Boson\Component\OsInfo\Standard; |
| 109 | + |
| 110 | +$os = OperatingSystem::createFromGlobals(); |
| 111 | + |
| 112 | +// Check if OS supports a specific standard |
| 113 | +if ($os->isSupports(Standard::Posix)) { |
| 114 | + // Standard is supported |
| 115 | +} |
| 116 | +``` |
0 commit comments