PhpExecutableFinder.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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
  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. if (@is_dir($php)) {
  43. return false;
  44. }
  45. return $php;
  46. }
  47. $args = $this->findArguments();
  48. $args = $includeArgs && $args ? ' '.implode(' ', $args) : '';
  49. // PHP_BINARY return the current sapi executable
  50. if (\PHP_BINARY && \in_array(\PHP_SAPI, ['cli', 'cli-server', 'phpdbg'], true)) {
  51. return \PHP_BINARY.$args;
  52. }
  53. if ($php = getenv('PHP_PATH')) {
  54. if (!@is_executable($php) || @is_dir($php)) {
  55. return false;
  56. }
  57. return $php;
  58. }
  59. if ($php = getenv('PHP_PEAR_PHP_BIN')) {
  60. if (@is_executable($php) && !@is_dir($php)) {
  61. return $php;
  62. }
  63. }
  64. if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php')) && !@is_dir($php)) {
  65. return $php;
  66. }
  67. $dirs = [\PHP_BINDIR];
  68. if ('\\' === \DIRECTORY_SEPARATOR) {
  69. $dirs[] = 'C:\xampp\php\\';
  70. }
  71. return $this->executableFinder->find('php', false, $dirs);
  72. }
  73. /**
  74. * Finds the PHP executable arguments.
  75. *
  76. * @return array
  77. */
  78. public function findArguments()
  79. {
  80. $arguments = [];
  81. if ('phpdbg' === \PHP_SAPI) {
  82. $arguments[] = '-qrr';
  83. }
  84. return $arguments;
  85. }
  86. }