ProcessBuilder.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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;
  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. * @deprecated since version 3.3, to be removed in 4.0.
  110. */
  111. public function inheritEnvironmentVariables($inheritEnv = true)
  112. {
  113. @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  114. $this->inheritEnv = $inheritEnv;
  115. return $this;
  116. }
  117. /**
  118. * Sets an environment variable.
  119. *
  120. * Setting a variable overrides its previous value. Use `null` to unset a
  121. * defined environment variable.
  122. *
  123. * @param string $name The variable name
  124. * @param null|string $value The variable value
  125. *
  126. * @return $this
  127. */
  128. public function setEnv($name, $value)
  129. {
  130. $this->env[$name] = $value;
  131. return $this;
  132. }
  133. /**
  134. * Adds a set of environment variables.
  135. *
  136. * Already existing environment variables with the same name will be
  137. * overridden by the new values passed to this method. Pass `null` to unset
  138. * a variable.
  139. *
  140. * @param array $variables The variables
  141. *
  142. * @return $this
  143. */
  144. public function addEnvironmentVariables(array $variables)
  145. {
  146. $this->env = array_replace($this->env, $variables);
  147. return $this;
  148. }
  149. /**
  150. * Sets the input of the process.
  151. *
  152. * @param resource|scalar|\Traversable|null $input The input content
  153. *
  154. * @return $this
  155. *
  156. * @throws InvalidArgumentException In case the argument is invalid
  157. */
  158. public function setInput($input)
  159. {
  160. $this->input = ProcessUtils::validateInput(__METHOD__, $input);
  161. return $this;
  162. }
  163. /**
  164. * Sets the process timeout.
  165. *
  166. * To disable the timeout, set this value to null.
  167. *
  168. * @param float|null $timeout
  169. *
  170. * @return $this
  171. *
  172. * @throws InvalidArgumentException
  173. */
  174. public function setTimeout($timeout)
  175. {
  176. if (null === $timeout) {
  177. $this->timeout = null;
  178. return $this;
  179. }
  180. $timeout = (float) $timeout;
  181. if ($timeout < 0) {
  182. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  183. }
  184. $this->timeout = $timeout;
  185. return $this;
  186. }
  187. /**
  188. * Adds a proc_open option.
  189. *
  190. * @param string $name The option name
  191. * @param string $value The option value
  192. *
  193. * @return $this
  194. *
  195. * @deprecated since version 3.3, to be removed in 4.0.
  196. */
  197. public function setOption($name, $value)
  198. {
  199. @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  200. $this->options[$name] = $value;
  201. return $this;
  202. }
  203. /**
  204. * Disables fetching output and error output from the underlying process.
  205. *
  206. * @return $this
  207. */
  208. public function disableOutput()
  209. {
  210. $this->outputDisabled = true;
  211. return $this;
  212. }
  213. /**
  214. * Enables fetching output and error output from the underlying process.
  215. *
  216. * @return $this
  217. */
  218. public function enableOutput()
  219. {
  220. $this->outputDisabled = false;
  221. return $this;
  222. }
  223. /**
  224. * Creates a Process instance and returns it.
  225. *
  226. * @return Process
  227. *
  228. * @throws LogicException In case no arguments have been provided
  229. */
  230. public function getProcess()
  231. {
  232. if (0 === count($this->prefix) && 0 === count($this->arguments)) {
  233. throw new LogicException('You must add() command arguments before calling getProcess().');
  234. }
  235. $arguments = array_merge($this->prefix, $this->arguments);
  236. $process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
  237. if ($this->inheritEnv) {
  238. $process->inheritEnvironmentVariables();
  239. }
  240. if ($this->outputDisabled) {
  241. $process->disableOutput();
  242. }
  243. return $process;
  244. }
  245. }