Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Platform | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| isWindows | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBinary | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getExecutable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Karla Imagemagick wrapper library |
| 5 | * |
| 6 | * PHP Version 8.0< |
| 7 | * |
| 8 | * @category Utility |
| 9 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
| 10 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
| 11 | * @link https://github.com/localgod/karla Karla |
| 12 | * @since 2026-03-25 |
| 13 | */ |
| 14 | |
| 15 | declare(strict_types=1); |
| 16 | |
| 17 | namespace Karla; |
| 18 | |
| 19 | /** |
| 20 | * Platform utility class for OS detection |
| 21 | * |
| 22 | * Centralises OS detection logic using the modern PHP_OS_FAMILY constant, |
| 23 | * replacing the scattered `strtoupper(substr(PHP_OS, 0, 3)) == "WIN"` pattern. |
| 24 | * |
| 25 | * @category Utility |
| 26 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
| 27 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
| 28 | * @link https://github.com/localgod/karla Karla |
| 29 | */ |
| 30 | class Platform |
| 31 | { |
| 32 | /** |
| 33 | * Determine whether the current OS is Windows. |
| 34 | * |
| 35 | * Uses PHP_OS_FAMILY (available since PHP 7.2) which returns 'Windows' on |
| 36 | * all Windows variants, making the check both readable and reliable. |
| 37 | */ |
| 38 | public static function isWindows(): bool |
| 39 | { |
| 40 | return PHP_OS_FAMILY === 'Windows'; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Return the platform-appropriate binary name. |
| 45 | * |
| 46 | * On Windows the `.exe` extension is appended; on all other platforms the |
| 47 | * name is returned unchanged. |
| 48 | * |
| 49 | * @param string $binary Base binary name (e.g. 'convert', 'magick') |
| 50 | * |
| 51 | * @return string Binary name suitable for the current platform |
| 52 | */ |
| 53 | public static function getBinary(string $binary): string |
| 54 | { |
| 55 | return self::isWindows() ? $binary . '.exe' : $binary; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Return the shell invocation of a binary in the given bin path. |
| 60 | * |
| 61 | * On Windows the bin path is a directory prefix, and the full path is quoted |
| 62 | * so install locations containing spaces (e.g. `C:\Program Files\...`) work. |
| 63 | * On other platforms the bin path is an `export PATH=...;` prefix and the |
| 64 | * binary is invoked by name. |
| 65 | * |
| 66 | * @param string $binPath Bin path prefix as stored by Karla |
| 67 | * @param string $binary Platform binary name (see getBinary()) |
| 68 | * |
| 69 | * @return string Executable invocation suitable for the current platform |
| 70 | */ |
| 71 | public static function getExecutable(string $binPath, string $binary): string |
| 72 | { |
| 73 | return self::isWindows() ? escapeshellarg($binPath . $binary) : $binPath . $binary; |
| 74 | } |
| 75 | } |