PhpExecutableFinder.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $args = $this->findArguments();
  34. $args = $includeArgs && $args ? ' '.implode(' ', $args) : '';
  35. // HHVM support
  36. if (defined('HHVM_VERSION')) {
  37. return (getenv('PHP_BINARY') ?: PHP_BINARY).$args;
  38. }
  39. // PHP_BINARY return the current sapi executable
  40. if (PHP_BINARY && in_array(PHP_SAPI, array('cli', 'cli-server', 'phpdbg')) && is_file(PHP_BINARY)) {
  41. return PHP_BINARY.$args;
  42. }
  43. if ($php = getenv('PHP_PATH')) {
  44. if (!is_executable($php)) {
  45. return false;
  46. }
  47. return $php;
  48. }
  49. if ($php = getenv('PHP_PEAR_PHP_BIN')) {
  50. if (is_executable($php)) {
  51. return $php;
  52. }
  53. }
  54. $dirs = array(PHP_BINDIR);
  55. if ('\\' === DIRECTORY_SEPARATOR) {
  56. $dirs[] = 'C:\xampp\php\\';
  57. }
  58. return $this->executableFinder->find('php', false, $dirs);
  59. }
  60. /**
  61. * Finds the PHP executable arguments.
  62. *
  63. * @return array The PHP executable arguments
  64. */
  65. public function findArguments()
  66. {
  67. $arguments = array();
  68. if (defined('HHVM_VERSION')) {
  69. $arguments[] = '--php';
  70. } elseif ('phpdbg' === PHP_SAPI) {
  71. $arguments[] = '-qrr';
  72. }
  73. return $arguments;
  74. }
  75. }