菜谱项目

ProcessBuilder.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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;
  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 null|string $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. * @deprecated since version 3.3, to be removed in 4.0.
  106. */
  107. public function inheritEnvironmentVariables($inheritEnv = true)
  108. {
  109. @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  110. $this->inheritEnv = $inheritEnv;
  111. return $this;
  112. }
  113. /**
  114. * Sets an environment variable.
  115. *
  116. * Setting a variable overrides its previous value. Use `null` to unset a
  117. * defined environment variable.
  118. *
  119. * @param string $name The variable name
  120. * @param null|string $value The variable value
  121. *
  122. * @return $this
  123. */
  124. public function setEnv($name, $value)
  125. {
  126. $this->env[$name] = $value;
  127. return $this;
  128. }
  129. /**
  130. * Adds a set of environment variables.
  131. *
  132. * Already existing environment variables with the same name will be
  133. * overridden by the new values passed to this method. Pass `null` to unset
  134. * a variable.
  135. *
  136. * @param array $variables The variables
  137. *
  138. * @return $this
  139. */
  140. public function addEnvironmentVariables(array $variables)
  141. {
  142. $this->env = array_replace($this->env, $variables);
  143. return $this;
  144. }
  145. /**
  146. * Sets the input of the process.
  147. *
  148. * @param resource|scalar|\Traversable|null $input The input content
  149. *
  150. * @return $this
  151. *
  152. * @throws InvalidArgumentException In case the argument is invalid
  153. */
  154. public function setInput($input)
  155. {
  156. $this->input = ProcessUtils::validateInput(__METHOD__, $input);
  157. return $this;
  158. }
  159. /**
  160. * Sets the process timeout.
  161. *
  162. * To disable the timeout, set this value to null.
  163. *
  164. * @param float|null $timeout
  165. *
  166. * @return $this
  167. *
  168. * @throws InvalidArgumentException
  169. */
  170. public function setTimeout($timeout)
  171. {
  172. if (null === $timeout) {
  173. $this->timeout = null;
  174. return $this;
  175. }
  176. $timeout = (float) $timeout;
  177. if ($timeout < 0) {
  178. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  179. }
  180. $this->timeout = $timeout;
  181. return $this;
  182. }
  183. /**
  184. * Adds a proc_open option.
  185. *
  186. * @param string $name The option name
  187. * @param string $value The option value
  188. *
  189. * @return $this
  190. *
  191. * @deprecated since version 3.3, to be removed in 4.0.
  192. */
  193. public function setOption($name, $value)
  194. {
  195. @trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  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. $arguments = array_merge($this->prefix, $this->arguments);
  232. $process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
  233. // to preserve the BC with symfony <3.3, we convert the array structure
  234. // to a string structure to avoid the prefixing with the exec command
  235. $process->setCommandLine($process->getCommandLine());
  236. if ($this->inheritEnv) {
  237. $process->inheritEnvironmentVariables();
  238. }
  239. if ($this->outputDisabled) {
  240. $process->disableOutput();
  241. }
  242. return $process;
  243. }
  244. }