Skip to content

Commit

Permalink
App::phpExecutable()
Browse files Browse the repository at this point in the history
Resolves #13681
  • Loading branch information
brandonkelly committed Sep 25, 2023
1 parent e0067a7 commit d9d42cf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Craft CMS 3.x

## Unreleased

- Added `craft\helpers\App::phpExecutable()`.
- Improved the reliability of Composer operations when PHP is running via FastCGI. ([#13681](https://github.com/craftcms/cms/issues/13681))

## 3.9.3 - 2023-09-14

- Added the `maxGraphqlBatchSize` config setting. ([#13693](https://github.com/craftcms/cms/issues/13693))
Expand Down
5 changes: 2 additions & 3 deletions src/console/controllers/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use craft\models\Updates;
use craft\models\Updates as UpdatesModel;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use yii\base\InvalidConfigException;
use yii\console\ExitCode;
Expand Down Expand Up @@ -415,7 +414,7 @@ private function _migrate(): bool

$this->stdout('Applying new migrations ... ', Console::FG_YELLOW);

$php = (new PhpExecutableFinder())->find() ?: 'php';
$php = App::phpExecutable() ?? 'php';
$process = new Process([$php, $script, 'migrate/all', '--no-backup', '--no-content']);
$process->setTimeout(null);
try {
Expand Down Expand Up @@ -507,7 +506,7 @@ private function _revertComposerChanges()

$this->stdout('Reverting Composer changes ... ', Console::FG_YELLOW);

$php = (new PhpExecutableFinder())->find() ?: 'php';
$php = App::phpExecutable() ?? 'php';
$process = new Process([$php, $script, 'update/composer-install']);
$process->setTimeout(null);
try {
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use craft\web\Session;
use craft\web\User as WebUser;
use craft\web\View;
use Symfony\Component\Process\PhpExecutableFinder;
use yii\base\Event;
use yii\base\InvalidArgumentException;
use yii\base\InvalidValueException;
Expand Down Expand Up @@ -421,6 +422,35 @@ public static function isPathAllowed(string $path): bool
return false;
}

/**
* Returns the path to a PHP executable which should be used by sub processes.
*
* @return string|null The PHP executable path, or `null` if it can’t be determined.
* @since 3.9.4
*/
public static function phpExecutable(): ?string
{
// If PHP_BINARY was set to $_SERVER, update the environment variable to match
if (isset($_SERVER['PHP_BINARY']) && $_SERVER['PHP_BINARY'] !== getenv('PHP_BINARY')) {
putenv(sprintf('PHP_BINARY=%s', $_SERVER['PHP_BINARY']));
}

if (
getenv('PHP_BINARY') === false &&
PHP_BINARY &&
PHP_SAPI === 'cgi-fcgi' &&
str_ends_with(PHP_BINARY, 'php-cgi')
) {
// See if a `php` file exists alongside `php-cgi`, and if so, use that
$file = dirname(PHP_BINARY) . DIRECTORY_SEPARATOR . 'php';
if (@is_executable($file) && !@is_dir($file)) {
return $file;
}
}

return (new PhpExecutableFinder())->find() ?: null;
}

/**
* Tests whether ini_set() works.
*
Expand Down
4 changes: 2 additions & 2 deletions src/services/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use Composer\IO\NullIO;
use Composer\Json\JsonFile;
use Craft;
use craft\helpers\App;
use craft\helpers\FileHelper;
use craft\helpers\Json;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use yii\base\Component;
use yii\base\Exception;
Expand Down Expand Up @@ -203,7 +203,7 @@ private function runComposerCommand(IOInterface $io, string $jsonPath, array $co
copy(Craft::getAlias('@lib/composer.phar'), $pharPath);

$command = array_merge([
(new PhpExecutableFinder())->find() ?: 'php',
App::phpExecutable() ?? 'php',
$pharPath,
], $command, [
'--working-dir',
Expand Down

0 comments on commit d9d42cf

Please sign in to comment.