LimeShell.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Provides an interface to execute PHP code or files.
  13. *
  14. * @package lime
  15. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id: LimeShell.php 24323 2009-11-24 11:27:51Z bschussek $
  18. */
  19. abstract class LimeShell
  20. {
  21. const
  22. SUCCESS = 0,
  23. FAILED = 1,
  24. UNKNOWN = 255;
  25. protected static
  26. $executable = null;
  27. /**
  28. * Sets the preferred PHP executable.
  29. *
  30. * @param string $executable
  31. */
  32. public static function setExecutable($executable)
  33. {
  34. self::$executable = $executable;
  35. }
  36. /**
  37. * Tries to find the system's PHP executable and returns it.
  38. *
  39. * @return string
  40. */
  41. public static function getExecutable()
  42. {
  43. if (is_null(self::$executable))
  44. {
  45. if (getenv('PHP_PATH'))
  46. {
  47. self::$executable = getenv('PHP_PATH');
  48. if (!is_executable(self::$executable))
  49. {
  50. throw new Exception('The defined PHP_PATH environment variable is not a valid PHP executable.');
  51. }
  52. }
  53. else
  54. {
  55. self::$executable = PHP_BINDIR.DIRECTORY_SEPARATOR.'php';
  56. }
  57. }
  58. if (!is_executable(self::$executable))
  59. {
  60. $path = getenv('PATH') ? getenv('PATH') : getenv('Path');
  61. $extensions = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array('');
  62. foreach (array('php5', 'php') as $executable)
  63. {
  64. foreach ($extensions as $extension)
  65. {
  66. foreach (explode(PATH_SEPARATOR, $path) as $dir)
  67. {
  68. $file = $dir.DIRECTORY_SEPARATOR.$executable.$extension;
  69. if (is_executable($file))
  70. {
  71. self::$executable = $file;
  72. break 3;
  73. }
  74. }
  75. }
  76. }
  77. if (!is_executable(self::$executable))
  78. {
  79. throw new Exception("Unable to find PHP executable.");
  80. }
  81. }
  82. return self::$executable;
  83. }
  84. /**
  85. * Parses the given CLI arguments and returns an array of options.
  86. *
  87. * @param array $arguments
  88. * @return array
  89. */
  90. public static function parseArguments(array $arguments)
  91. {
  92. $options = array();
  93. foreach ($GLOBALS['argv'] as $parameter)
  94. {
  95. if (preg_match('/^--([a-zA-Z\-]+)=(.+)$/', $parameter, $matches))
  96. {
  97. if (in_array($matches[2], array('true', 'false')))
  98. {
  99. $matches[2] = eval($matches[2]);
  100. }
  101. $options[$matches[1]] = $matches[2];
  102. }
  103. else if (preg_match('/^--([a-zA-Z\-]+)$/', $parameter, $matches))
  104. {
  105. $options[$matches[1]] = true;
  106. }
  107. }
  108. return $options;
  109. }
  110. }