Skip to content

Commit 507dc42

Browse files
committed
[BUGFIX] Fix the logic for resolving PHP binary. If the binary specified by php_version or composer.json is not found, it now gracefully falls back to search for php binary and finally defaults to plain php.
1 parent 38ab772 commit 507dc42

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ master
77

88
1) [FEATURE] Add dbCodeFilter option to filter database (credits to `SimonVanacco <https://github.com/SimonVanacco>`_ )
99
2) [BUGFIX] SSL options in drop tables command (credits to `maikschneider <https://github.com/maikschneider>`_ )
10-
3) [BUGFIX] Make strict boolean conditions for `post_sql_in_markers` and `post_sql_in`.
10+
3) [BUGFIX] Make strict boolean conditions for ``post_sql_in_markers`` and ``post_sql_in``.
11+
4) [BUGFIX] Fix the logic for resolving PHP binary. If the binary specified by ``php_version`` or ``composer.json``
12+
is not found, it now gracefully falls back to search for ``php`` binary and finally defaults to plain ``php``.
1113

1214
20.0.1
1315
------

deployer/db/config/set.php

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,24 @@
136136
});
137137

138138
set('local/bin/php', function () {
139+
$rawPhpVersion = null;
139140
if (currentHost()->hasOwn('php_version')) {
140141
$rawPhpVersion = get('php_version');
141142
}
142143

143144
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
151157
}
152158
}
153159

@@ -159,13 +165,68 @@
159165
}
160166

161167
$fileUtility = new FileUtility();
162-
if ($phpVersionMajorMinor) {
168+
if ($phpVersionMajorMinor !== null) {
163169
try {
164170
return $fileUtility->locateLocalBinaryPath('php' . $phpVersionMajorMinor);
165171
} 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+
}
167180
}
168181
}
169182

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+
}
171232
});

0 commit comments

Comments
 (0)