|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace HtmlShot\Composer; |
| 6 | + |
| 7 | +/** |
| 8 | + * Platform detection and matching for selecting the correct binary artifact. |
| 9 | + * |
| 10 | + * Adapted from codewithkyrian/platform-package-installer (MIT). |
| 11 | + */ |
| 12 | +class Platform |
| 13 | +{ |
| 14 | + public static function current(): array |
| 15 | + { |
| 16 | + $osName = strtolower(php_uname('s')); |
| 17 | + $arch = php_uname('m'); |
| 18 | + |
| 19 | + return [ |
| 20 | + 'os' => $osName, |
| 21 | + 'arch' => self::normalizeArchitecture($arch), |
| 22 | + 'full' => php_uname(), |
| 23 | + ]; |
| 24 | + } |
| 25 | + |
| 26 | + public static function normalizeArchitecture(string $arch): string |
| 27 | + { |
| 28 | + $arch = strtolower($arch); |
| 29 | + |
| 30 | + $archMap = [ |
| 31 | + 'x86_64' => 'x86_64', |
| 32 | + 'amd64' => 'x86_64', |
| 33 | + 'i386' => 'x86', |
| 34 | + 'i686' => 'x86', |
| 35 | + 'x64' => 'x86_64', |
| 36 | + 'x86' => 'x86', |
| 37 | + '32' => 'x86', |
| 38 | + '64' => 'x86_64', |
| 39 | + 'arm64' => 'arm64', |
| 40 | + 'aarch64' => 'arm64', |
| 41 | + 'armv7' => 'arm', |
| 42 | + 'armv8' => 'arm64', |
| 43 | + 'arm64v8' => 'arm64', |
| 44 | + 'ppc64' => 'ppc64', |
| 45 | + 'ppc64le' => 'ppc64le', |
| 46 | + 's390x' => 's390x', |
| 47 | + ]; |
| 48 | + |
| 49 | + return $archMap[$arch] ?? $arch; |
| 50 | + } |
| 51 | + |
| 52 | + public static function matches(string $definedPlatform, ?array $currentPlatform = null): bool |
| 53 | + { |
| 54 | + $currentPlatform ??= self::current(); |
| 55 | + $definedPlatform = strtolower($definedPlatform); |
| 56 | + |
| 57 | + if ($definedPlatform === 'all') { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + $parts = explode('-', $definedPlatform); |
| 62 | + $os = $parts[0]; |
| 63 | + $arch = count($parts) > 1 ? $parts[1] : null; |
| 64 | + |
| 65 | + $archMatch = $arch === null |
| 66 | + || self::normalizeArchitecture($arch) === $currentPlatform['arch']; |
| 67 | + |
| 68 | + return self::matchesOS($os, $currentPlatform['os']) && $archMatch; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Return the value from $platformEntries whose key best matches the current platform, |
| 73 | + * preferring more-specific (os-arch) keys over os-only keys, or false if none match. |
| 74 | + * |
| 75 | + * @template T |
| 76 | + * |
| 77 | + * @param array<string, T> $platformEntries |
| 78 | + * @param array{os: string, arch: string}|null $currentPlatform |
| 79 | + * @return T|false |
| 80 | + */ |
| 81 | + public static function findBestMatch(array $platformEntries, ?array $currentPlatform = null): mixed |
| 82 | + { |
| 83 | + $currentPlatform ??= self::current(); |
| 84 | + |
| 85 | + $matches = array_filter( |
| 86 | + array_keys($platformEntries), |
| 87 | + fn ($p) => self::matches($p, $currentPlatform) |
| 88 | + ); |
| 89 | + |
| 90 | + usort($matches, function (string $a, string $b): int { |
| 91 | + if ($a === 'all') { |
| 92 | + return 1; |
| 93 | + } |
| 94 | + if ($b === 'all') { |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + $aHasArch = str_contains($a, '-'); |
| 99 | + $bHasArch = str_contains($b, '-'); |
| 100 | + |
| 101 | + if ($aHasArch && ! $bHasArch) { |
| 102 | + return -1; |
| 103 | + } |
| 104 | + if (! $aHasArch && $bHasArch) { |
| 105 | + return 1; |
| 106 | + } |
| 107 | + |
| 108 | + return 0; |
| 109 | + }); |
| 110 | + |
| 111 | + foreach ($matches as $platform) { |
| 112 | + return $platformEntries[$platform]; |
| 113 | + } |
| 114 | + |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + private static function matchesOS(string $definedOs, string $currentOs): bool |
| 119 | + { |
| 120 | + $osAliases = [ |
| 121 | + 'windows' => ['windows', 'win32', 'win64', 'windows nt'], |
| 122 | + 'darwin' => ['macos', 'mac', 'darwin'], |
| 123 | + 'linux' => ['linux', 'gnu/linux'], |
| 124 | + 'raspberrypi' => ['raspbian', 'raspberry pi'], |
| 125 | + ]; |
| 126 | + |
| 127 | + if ($definedOs === $currentOs) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + foreach ($osAliases as $alias => $variations) { |
| 132 | + if ($definedOs === $alias && in_array($currentOs, $variations, true)) { |
| 133 | + return true; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return false; |
| 138 | + } |
| 139 | +} |
0 commit comments