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