PhpExecutableFinder.php 2.7 KB

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