PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/process/ProcessBuilder.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 287 lines | 111 code | 41 blank | 135 comment | 7 complexity | 6399ffedb444f79222b926202e301cdd MD5 | raw file
  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. * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.
  156. */
  157. public function setInput($input)
  158. {
  159. $this->input = ProcessUtils::validateInput(__METHOD__, $input);
  160. return $this;
  161. }
  162. /**
  163. * Sets the process timeout.
  164. *
  165. * To disable the timeout, set this value to null.
  166. *
  167. * @param float|null $timeout
  168. *
  169. * @return ProcessBuilder
  170. *
  171. * @throws InvalidArgumentException
  172. */
  173. public function setTimeout($timeout)
  174. {
  175. if (null === $timeout) {
  176. $this->timeout = null;
  177. return $this;
  178. }
  179. $timeout = (float) $timeout;
  180. if ($timeout < 0) {
  181. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  182. }
  183. $this->timeout = $timeout;
  184. return $this;
  185. }
  186. /**
  187. * Adds a proc_open option.
  188. *
  189. * @param string $name The option name
  190. * @param string $value The option value
  191. *
  192. * @return ProcessBuilder
  193. */
  194. public function setOption($name, $value)
  195. {
  196. $this->options[$name] = $value;
  197. return $this;
  198. }
  199. /**
  200. * Disables fetching output and error output from the underlying process.
  201. *
  202. * @return ProcessBuilder
  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 ProcessBuilder
  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. $options = $this->options;
  232. $arguments = array_merge($this->prefix, $this->arguments);
  233. $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
  234. if ($this->inheritEnv) {
  235. // include $_ENV for BC purposes
  236. $env = array_replace($_ENV, $_SERVER, $this->env);
  237. } else {
  238. $env = $this->env;
  239. }
  240. $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
  241. if ($this->outputDisabled) {
  242. $process->disableOutput();
  243. }
  244. return $process;
  245. }
  246. }