ProcessBuilder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  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 ProcessBuilder
  152. *
  153. * @throws InvalidArgumentException In case the argument is invalid
  154. */
  155. public function setInput($input)
  156. {
  157. $this->input = ProcessUtils::validateInput(__METHOD__, $input);
  158. return $this;
  159. }
  160. /**
  161. * Sets the process timeout.
  162. *
  163. * To disable the timeout, set this value to null.
  164. *
  165. * @param float|null $timeout
  166. *
  167. * @return ProcessBuilder
  168. *
  169. * @throws InvalidArgumentException
  170. */
  171. public function setTimeout($timeout)
  172. {
  173. if (null === $timeout) {
  174. $this->timeout = null;
  175. return $this;
  176. }
  177. $timeout = (float) $timeout;
  178. if ($timeout < 0) {
  179. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  180. }
  181. $this->timeout = $timeout;
  182. return $this;
  183. }
  184. /**
  185. * Adds a proc_open option.
  186. *
  187. * @param string $name The option name
  188. * @param string $value The option value
  189. *
  190. * @return ProcessBuilder
  191. */
  192. public function setOption($name, $value)
  193. {
  194. $this->options[$name] = $value;
  195. return $this;
  196. }
  197. /**
  198. * Disables fetching output and error output from the underlying process.
  199. *
  200. * @return ProcessBuilder
  201. */
  202. public function disableOutput()
  203. {
  204. $this->outputDisabled = true;
  205. return $this;
  206. }
  207. /**
  208. * Enables fetching output and error output from the underlying process.
  209. *
  210. * @return ProcessBuilder
  211. */
  212. public function enableOutput()
  213. {
  214. $this->outputDisabled = false;
  215. return $this;
  216. }
  217. /**
  218. * Creates a Process instance and returns it.
  219. *
  220. * @return Process
  221. *
  222. * @throws LogicException In case no arguments have been provided
  223. */
  224. public function getProcess()
  225. {
  226. if (0 === count($this->prefix) && 0 === count($this->arguments)) {
  227. throw new LogicException('You must add() command arguments before calling getProcess().');
  228. }
  229. $options = $this->options;
  230. $arguments = array_merge($this->prefix, $this->arguments);
  231. $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
  232. if ($this->inheritEnv) {
  233. $env = array_replace($_ENV, $_SERVER, $this->env);
  234. } else {
  235. $env = $this->env;
  236. }
  237. $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
  238. if ($this->outputDisabled) {
  239. $process->disableOutput();
  240. }
  241. return $process;
  242. }
  243. }