|
136 | 136 | }); |
137 | 137 |
|
138 | 138 | set('local/bin/php', function () { |
| 139 | + $rawPhpVersion = null; |
139 | 140 | if (currentHost()->hasOwn('php_version')) { |
140 | 141 | $rawPhpVersion = get('php_version'); |
141 | 142 | } |
142 | 143 |
|
143 | 144 | if (empty($rawPhpVersion) && file_exists('composer.json')) { |
144 | | - $composerJson = json_decode(file_get_contents('composer.json'), true); |
145 | | - |
146 | | - if (isset($composerJson['config']['platform']['php'])) { |
147 | | - $rawPhpVersion = $composerJson['config']['platform']['php']; |
148 | | - } |
149 | | - if (empty($rawPhpVersion) && isset($composerJson['require']['php'])) { |
150 | | - $rawPhpVersion = $composerJson['require']['php']; |
| 145 | + try { |
| 146 | + $composerJson = json_decode(file_get_contents('composer.json'), true); |
| 147 | + if (is_array($composerJson)) { |
| 148 | + if (isset($composerJson['config']['platform']['php'])) { |
| 149 | + $rawPhpVersion = $composerJson['config']['platform']['php']; |
| 150 | + } |
| 151 | + if (empty($rawPhpVersion) && isset($composerJson['require']['php'])) { |
| 152 | + $rawPhpVersion = $composerJson['require']['php']; |
| 153 | + } |
| 154 | + } |
| 155 | + } catch (\Throwable $e) { |
| 156 | + // Silently handle any errors reading composer.json |
151 | 157 | } |
152 | 158 | } |
153 | 159 |
|
|
159 | 165 | } |
160 | 166 |
|
161 | 167 | $fileUtility = new FileUtility(); |
162 | | - if ($phpVersionMajorMinor) { |
| 168 | + if ($phpVersionMajorMinor !== null) { |
163 | 169 | try { |
164 | 170 | return $fileUtility->locateLocalBinaryPath('php' . $phpVersionMajorMinor); |
165 | 171 | } catch (\Throwable $e) { |
166 | | - return $fileUtility->locateLocalBinaryPath('php' . str_replace('.', '', $phpVersionMajorMinor)); |
| 172 | + try { |
| 173 | + return $fileUtility->locateLocalBinaryPath('php' . str_replace('.', '', $phpVersionMajorMinor)); |
| 174 | + } catch (\Throwable $e) { |
| 175 | + output()->writeln( |
| 176 | + '<comment>PHP binary with version ' . $phpVersionMajorMinor . ' not found, falling back to search for "php"</comment>', |
| 177 | + \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_VERBOSE |
| 178 | + ); |
| 179 | + } |
167 | 180 | } |
168 | 181 | } |
169 | 182 |
|
170 | | - return $fileUtility->locateLocalBinaryPath('php'); |
| 183 | + try { |
| 184 | + $phpBinaryPath = $fileUtility->locateLocalBinaryPath('php'); |
| 185 | + $actualVersionMajorMinor = trim(runLocally($phpBinaryPath . ' -r "echo PHP_MAJOR_VERSION.\".\" . PHP_MINOR_VERSION;"')); |
| 186 | + |
| 187 | + if ($phpVersionMajorMinor !== null && $actualVersionMajorMinor !== $phpVersionMajorMinor) { |
| 188 | + $phpVersionStrict = get('php_version_strict', false); |
| 189 | + if ($phpVersionStrict) { |
| 190 | + throw new \RuntimeException(sprintf( |
| 191 | + 'PHP version mismatch: required %s, found %s', |
| 192 | + $phpVersionMajorMinor, |
| 193 | + $actualVersionMajorMinor |
| 194 | + ), 1715438658); |
| 195 | + } |
| 196 | + output()->writeln( |
| 197 | + '<warning>Found PHP binary version (' . $phpBinaryPath . ' ' . $actualVersionMajorMinor . ') does not match required version (' . $phpVersionMajorMinor . ')</warning>', |
| 198 | + \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL |
| 199 | + ); |
| 200 | + } |
| 201 | + return $phpBinaryPath; |
| 202 | + } catch (\Throwable $e) { |
| 203 | + if ($e->getCode() === 1715438658) { |
| 204 | + throw $e; |
| 205 | + } |
| 206 | + |
| 207 | + output()->writeln( |
| 208 | + '<comment>"php" command not found in PATH, using just "php" directly</comment>', |
| 209 | + \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_VERBOSE |
| 210 | + ); |
| 211 | + $phpBinaryPath = 'php'; |
| 212 | + if ($phpVersionMajorMinor !== null) { |
| 213 | + $actualVersionMajorMinor = trim(runLocally($phpBinaryPath . ' -r "echo PHP_MAJOR_VERSION.\".\" . PHP_MINOR_VERSION;"')); |
| 214 | + |
| 215 | + if ($actualVersionMajorMinor !== $phpVersionMajorMinor) { |
| 216 | + $phpVersionStrict = get('php_version_strict', false); |
| 217 | + if ($phpVersionStrict) { |
| 218 | + throw new \RuntimeException(sprintf( |
| 219 | + 'PHP version mismatch: required %s, found %s', |
| 220 | + $phpVersionMajorMinor, |
| 221 | + $actualVersionMajorMinor |
| 222 | + ), 1715438658); |
| 223 | + } |
| 224 | + output()->writeln( |
| 225 | + '<warning>PHP version found when running just "php" directly ( ' . $actualVersionMajorMinor . ') does not match required version (' . $phpVersionMajorMinor . ')</warning>', |
| 226 | + \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_NORMAL |
| 227 | + ); |
| 228 | + } |
| 229 | + } |
| 230 | + return $phpBinaryPath; |
| 231 | + } |
171 | 232 | }); |
0 commit comments