ProcessBuilder.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. @trigger_error(sprintf('The %s class is deprecated since Symfony 3.4 and will be removed in 4.0. Use the Process class instead.', ProcessBuilder::class), E_USER_DEPRECATED);
  12. use Symfony\Component\Process\Exception\InvalidArgumentException;
  13. use Symfony\Component\Process\Exception\LogicException;
  14. /**
  15. * @author Kris Wallsmith <kris@symfony.com>
  16. *
  17. * @deprecated since version 3.4, to be removed in 4.0. Use the Process class instead.
  18. */
  19. class ProcessBuilder
  20. {
  21. private $arguments;
  22. private $cwd;
  23. private $env = [];
  24. private $input;
  25. private $timeout = 60;
  26. private $options;
  27. private $inheritEnv = true;
  28. private $prefix = [];
  29. private $outputDisabled = false;
  30. /**
  31. * @param string[] $arguments An array of arguments
  32. */
  33. public function __construct(array $arguments = [])
  34. {
  35. $this->arguments = $arguments;
  36. }
  37. /**
  38. * Creates a process builder instance.
  39. *
  40. * @param string[] $arguments An array of arguments
  41. *
  42. * @return static
  43. */
  44. public static function create(array $arguments = [])
  45. {
  46. return new static($arguments);
  47. }
  48. /**
  49. * Adds an unescaped argument to the command string.
  50. *
  51. * @param string $argument A command argument
  52. *
  53. * @return $this
  54. */
  55. public function add($argument)
  56. {
  57. $this->arguments[] = $argument;
  58. return $this;
  59. }
  60. /**
  61. * Adds a prefix to the command string.
  62. *
  63. * The prefix is preserved when resetting arguments.
  64. *
  65. * @param string|array $prefix A command prefix or an array of command prefixes
  66. *
  67. * @return $this
  68. */
  69. public function setPrefix($prefix)
  70. {
  71. $this->prefix = \is_array($prefix) ? $prefix : [$prefix];
  72. return $this;
  73. }
  74. /**
  75. * Sets the arguments of the process.
  76. *
  77. * Arguments must not be escaped.
  78. * Previous arguments are removed.
  79. *
  80. * @param string[] $arguments
  81. *
  82. * @return $this
  83. */
  84. public function setArguments(array $arguments)
  85. {
  86. $this->arguments = $arguments;
  87. return $this;
  88. }
  89. /**
  90. * Sets the working directory.
  91. *
  92. * @param string|null $cwd The working directory
  93. *
  94. * @return $this
  95. */
  96. public function setWorkingDirectory($cwd)
  97. {
  98. $this->cwd = $cwd;
  99. return $this;
  100. }
  101. /**
  102. * Sets whether environment variables will be inherited or not.
  103. *
  104. * @param bool $inheritEnv
  105. *
  106. * @return $this
  107. */
  108. public function inheritEnvironmentVariables($inheritEnv = true)
  109. {
  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 string|null $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|string|int|float|bool|\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. public function setOption($name, $value)
  192. {
  193. $this->options[$name] = $value;
  194. return $this;
  195. }
  196. /**
  197. * Disables fetching output and error output from the underlying process.
  198. *
  199. * @return $this
  200. */
  201. public function disableOutput()
  202. {
  203. $this->outputDisabled = true;
  204. return $this;
  205. }
  206. /**
  207. * Enables fetching output and error output from the underlying process.
  208. *
  209. * @return $this
  210. */
  211. public function enableOutput()
  212. {
  213. $this->outputDisabled = false;
  214. return $this;
  215. }
  216. /**
  217. * Creates a Process instance and returns it.
  218. *
  219. * @return Process
  220. *
  221. * @throws LogicException In case no arguments have been provided
  222. */
  223. public function getProcess()
  224. {
  225. if (0 === \count($this->prefix) && 0 === \count($this->arguments)) {
  226. throw new LogicException('You must add() command arguments before calling getProcess().');
  227. }
  228. $arguments = array_merge($this->prefix, $this->arguments);
  229. $process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
  230. // to preserve the BC with symfony <3.3, we convert the array structure
  231. // to a string structure to avoid the prefixing with the exec command
  232. $process->setCommandLine($process->getCommandLine());
  233. if ($this->inheritEnv) {
  234. $process->inheritEnvironmentVariables();
  235. }
  236. if ($this->outputDisabled) {
  237. $process->disableOutput();
  238. }
  239. return $process;
  240. }
  241. }