You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB

3 years ago
  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;
  20. public function __construct()
  21. {
  22. $this->executableFinder = new ExecutableFinder();
  23. }
  24. /**
  25. * Finds The PHP executable.
  26. *
  27. * @return string|false The PHP executable path or false if it cannot be found
  28. */
  29. public function find(bool $includeArgs = true)
  30. {
  31. if ($php = getenv('PHP_BINARY')) {
  32. if (!is_executable($php)) {
  33. $command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v';
  34. if ($php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
  35. if (!is_executable($php)) {
  36. return false;
  37. }
  38. } else {
  39. return false;
  40. }
  41. }
  42. return $php;
  43. }
  44. $args = $this->findArguments();
  45. $args = $includeArgs && $args ? ' '.implode(' ', $args) : '';
  46. // PHP_BINARY return the current sapi executable
  47. if (\PHP_BINARY && \in_array(\PHP_SAPI, ['cgi-fcgi', 'cli', 'cli-server', 'phpdbg'], true)) {
  48. return \PHP_BINARY.$args;
  49. }
  50. if ($php = getenv('PHP_PATH')) {
  51. if (!@is_executable($php)) {
  52. return false;
  53. }
  54. return $php;
  55. }
  56. if ($php = getenv('PHP_PEAR_PHP_BIN')) {
  57. if (@is_executable($php)) {
  58. return $php;
  59. }
  60. }
  61. if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php'))) {
  62. return $php;
  63. }
  64. $dirs = [\PHP_BINDIR];
  65. if ('\\' === \DIRECTORY_SEPARATOR) {
  66. $dirs[] = 'C:\xampp\php\\';
  67. }
  68. return $this->executableFinder->find('php', false, $dirs);
  69. }
  70. /**
  71. * Finds the PHP executable arguments.
  72. *
  73. * @return array The PHP executable arguments
  74. */
  75. public function findArguments()
  76. {
  77. $arguments = [];
  78. if ('phpdbg' === \PHP_SAPI) {
  79. $arguments[] = '-qrr';
  80. }
  81. return $arguments;
  82. }
  83. }