Drupal investigation

ProcessBuilder.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. use Symfony\Component\Process\Exception\InvalidArgumentException;
  12. use Symfony\Component\Process\Exception\LogicException;
  13. /**
  14. * Process builder.
  15. *
  16. * @author Kris Wallsmith <kris@symfony.com>
  17. */
  18. class ProcessBuilder
  19. {
  20. private $arguments;
  21. private $cwd;
  22. private $env = array();
  23. private $input;
  24. private $timeout = 60;
  25. private $options = array();
  26. private $inheritEnv = true;
  27. private $prefix = array();
  28. private $outputDisabled = false;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string[] $arguments An array of arguments
  33. */
  34. public function __construct(array $arguments = array())
  35. {
  36. $this->arguments = $arguments;
  37. }
  38. /**
  39. * Creates a process builder instance.
  40. *
  41. * @param string[] $arguments An array of arguments
  42. *
  43. * @return static
  44. */
  45. public static function create(array $arguments = array())
  46. {
  47. return new static($arguments);
  48. }
  49. /**
  50. * Adds an unescaped argument to the command string.
  51. *
  52. * @param string $argument A command argument
  53. *
  54. * @return $this
  55. */
  56. public function add($argument)
  57. {
  58. $this->arguments[] = $argument;
  59. return $this;
  60. }
  61. /**
  62. * Adds a prefix to the command string.
  63. *
  64. * The prefix is preserved when resetting arguments.
  65. *
  66. * @param string|array $prefix A command prefix or an array of command prefixes
  67. *
  68. * @return $this
  69. */
  70. public function setPrefix($prefix)
  71. {
  72. $this->prefix = is_array($prefix) ? $prefix : array($prefix);
  73. return $this;
  74. }
  75. /**
  76. * Sets the arguments of the process.
  77. *
  78. * Arguments must not be escaped.
  79. * Previous arguments are removed.
  80. *
  81. * @param string[] $arguments
  82. *
  83. * @return $this
  84. */
  85. public function setArguments(array $arguments)
  86. {
  87. $this->arguments = $arguments;
  88. return $this;
  89. }
  90. /**
  91. * Sets the working directory.
  92. *
  93. * @param null|string $cwd The working directory
  94. *
  95. * @return $this
  96. */
  97. public function setWorkingDirectory($cwd)
  98. {
  99. $this->cwd = $cwd;
  100. return $this;
  101. }
  102. /**
  103. * Sets whether environment variables will be inherited or not.
  104. *
  105. * @param bool $inheritEnv
  106. *
  107. * @return $this
  108. */
  109. public function inheritEnvironmentVariables($inheritEnv = true)
  110. {
  111. $this->inheritEnv = $inheritEnv;
  112. return $this;
  113. }
  114. /**
  115. * Sets an environment variable.
  116. *
  117. * Setting a variable overrides its previous value. Use `null` to unset a
  118. * defined environment variable.
  119. *
  120. * @param string $name The variable name
  121. * @param null|string $value The variable value
  122. *
  123. * @return $this
  124. */
  125. public function setEnv($name, $value)
  126. {
  127. $this->env[$name] = $value;
  128. return $this;
  129. }
  130. /**
  131. * Adds a set of environment variables.
  132. *
  133. * Already existing environment variables with the same name will be
  134. * overridden by the new values passed to this method. Pass `null` to unset
  135. * a variable.
  136. *
  137. * @param array $variables The variables
  138. *
  139. * @return $this
  140. */
  141. public function addEnvironmentVariables(array $variables)
  142. {
  143. $this->env = array_replace($this->env, $variables);
  144. return $this;
  145. }
  146. /**
  147. * Sets the input of the process.
  148. *
  149. * @param mixed $input The input as a string
  150. *
  151. * @return $this
  152. *
  153. * @throws InvalidArgumentException In case the argument is invalid
  154. *
  155. * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.
  156. */
  157. public function setInput($input)
  158. {
  159. $this->input = ProcessUtils::validateInput(__METHOD__, $input);
  160. return $this;
  161. }
  162. /**
  163. * Sets the process timeout.
  164. *
  165. * To disable the timeout, set this value to null.
  166. *
  167. * @param float|null $timeout
  168. *
  169. * @return $this
  170. *
  171. * @throws InvalidArgumentException
  172. */
  173. public function setTimeout($timeout)
  174. {
  175. if (null === $timeout) {
  176. $this->timeout = null;
  177. return $this;
  178. }
  179. $timeout = (float) $timeout;
  180. if ($timeout < 0) {
  181. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  182. }
  183. $this->timeout = $timeout;
  184. return $this;
  185. }
  186. /**
  187. * Adds a proc_open option.
  188. *
  189. * @param string $name The option name
  190. * @param string $value The option value
  191. *
  192. * @return $this
  193. */
  194. public function setOption($name, $value)
  195. {
  196. $this->options[$name] = $value;
  197. return $this;
  198. }
  199. /**
  200. * Disables fetching output and error output from the underlying process.
  201. *
  202. * @return $this
  203. */
  204. public function disableOutput()
  205. {
  206. $this->outputDisabled = true;
  207. return $this;
  208. }
  209. /**
  210. * Enables fetching output and error output from the underlying process.
  211. *
  212. * @return $this
  213. */
  214. public function enableOutput()
  215. {
  216. $this->outputDisabled = false;
  217. return $this;
  218. }
  219. /**
  220. * Creates a Process instance and returns it.
  221. *
  222. * @return Process
  223. *
  224. * @throws LogicException In case no arguments have been provided
  225. */
  226. public function getProcess()
  227. {
  228. if (0 === count($this->prefix) && 0 === count($this->arguments)) {
  229. throw new LogicException('You must add() command arguments before calling getProcess().');
  230. }
  231. $options = $this->options;
  232. $arguments = array_merge($this->prefix, $this->arguments);
  233. $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
  234. if ($this->inheritEnv) {
  235. // include $_ENV for BC purposes
  236. $env = array_replace($_ENV, $_SERVER, $this->env);
  237. } else {
  238. $env = $this->env;
  239. }
  240. $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
  241. if ($this->outputDisabled) {
  242. $process->disableOutput();
  243. }
  244. return $process;
  245. }
  246. }