From 2437240f3072875dc95de64d2ff11a452818dd12 Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Thu, 14 Apr 2022 19:12:50 +0000
Subject: [PATCH] Issue #3275324 by phenaproxima: If the PHP binary is not in
 the PHP interpreter's PATH, Composer invocations fail

---
 package_manager/src/ProcessFactory.php | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/package_manager/src/ProcessFactory.php b/package_manager/src/ProcessFactory.php
index 78ef902b2d..fa1a8565d0 100644
--- a/package_manager/src/ProcessFactory.php
+++ b/package_manager/src/ProcessFactory.php
@@ -74,11 +74,29 @@ final class ProcessFactory implements ProcessFactoryInterface {
     if ($this->isComposerCommand($command)) {
       $env['COMPOSER_HOME'] = $this->getComposerHomePath();
     }
-    // Ensure that the running PHP binary is in the PATH.
-    $env['PATH'] = $this->getEnv('PATH') . ':' . dirname(PHP_BINARY);
+    // Ensure that the PHP interpreter is in the PATH.
+    $env['PATH'] = $this->getEnv('PATH') . ':' . static::getPhpDirectory();
     return $process->setEnv($env);
   }
 
+  /**
+   * Returns the directory which contains the PHP interpreter.
+   *
+   * @return string
+   *   The path of the directory containing the PHP interpreter. If the server
+   *   is running in a command-line interface, the directory portion of
+   *   PHP_BINARY is returned; otherwise, the compile-time PHP_BINDIR is.
+   *
+   * @see php_sapi_name()
+   * @see https://www.php.net/manual/en/reserved.constants.php
+   */
+  protected static function getPhpDirectory(): string {
+    if (PHP_SAPI === 'cli' || PHP_SAPI === 'cli-server') {
+      return dirname(PHP_BINARY);
+    }
+    return PHP_BINDIR;
+  }
+
   /**
    * Returns the path to use as the COMPOSER_HOME environment variable.
    *
-- 
GitLab