PhpExecutableFinder.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process;
  11. /**
  12. * An executable finder specifically designed for the PHP executable.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class PhpExecutableFinder
  18. {
  19. private ExecutableFinder $executableFinder;
  20. public function __construct()
  21. {
  22. $this->executableFinder = new ExecutableFinder();
  23. }
  24. /**
  25. * Finds The PHP executable.
  26. */
  27. public function find(bool $includeArgs = true): string|false
  28. {
  29. if ($php = getenv('PHP_BINARY')) {
  30. if (!is_executable($php) && !$php = $this->executableFinder->find($php)) {
  31. return false;
  32. }
  33. if (@is_dir($php)) {
  34. return false;
  35. }
  36. return $php;
  37. }
  38. $args = $this->findArguments();
  39. $args = $includeArgs && $args ? ' '.implode(' ', $args) : '';
  40. // PHP_BINARY return the current sapi executable
  41. if (\PHP_BINARY && \in_array(\PHP_SAPI, ['cli', 'cli-server', 'phpdbg'], true)) {
  42. return \PHP_BINARY.$args;
  43. }
  44. if ($php = getenv('PHP_PATH')) {
  45. if (!@is_executable($php) || @is_dir($php)) {
  46. return false;
  47. }
  48. return $php;
  49. }
  50. if ($php = getenv('PHP_PEAR_PHP_BIN')) {
  51. if (@is_executable($php) && !@is_dir($php)) {
  52. return $php;
  53. }
  54. }
  55. if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php')) && !@is_dir($php)) {
  56. return $php;
  57. }
  58. $dirs = [\PHP_BINDIR];
  59. if ('\\' === \DIRECTORY_SEPARATOR) {
  60. $dirs[] = 'C:\xampp\php\\';
  61. }
  62. if ($herdPath = getenv('HERD_HOME')) {
  63. $dirs[] = $herdPath.\DIRECTORY_SEPARATOR.'bin';
  64. }
  65. return $this->executableFinder->find('php', false, $dirs);
  66. }
  67. /**
  68. * Finds the PHP executable arguments.
  69. *
  70. * @return list<non-empty-string>
  71. */
  72. public function findArguments(): array
  73. {
  74. $arguments = [];
  75. if ('phpdbg' === \PHP_SAPI) {
  76. $arguments[] = '-qrr';
  77. }
  78. return $arguments;
  79. }
  80. }