ProcessBuilder.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 $stdin;
  24. private $timeout = 60;
  25. private $options = array();
  26. private $inheritEnv = true;
  27. private $prefix = array();
  28. /**
  29. * Constructor
  30. *
  31. * @param string[] $arguments An array of arguments
  32. */
  33. public function __construct(array $arguments = array())
  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 ProcessBuilder
  43. */
  44. public static function create(array $arguments = array())
  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 ProcessBuilder
  54. */
  55. public function add($argument)
  56. {
  57. $this->arguments[] = $argument;
  58. return $this;
  59. }
  60. /**
  61. * Adds an unescaped 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 ProcessBuilder
  68. */
  69. public function setPrefix($prefix)
  70. {
  71. $this->prefix = is_array($prefix) ? $prefix : array($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 ProcessBuilder
  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 null|string $cwd The working directory
  93. *
  94. * @return ProcessBuilder
  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 ProcessBuilder
  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 null|string $value The variable value
  121. *
  122. * @return ProcessBuilder
  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 ProcessBuilder
  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 string|null $stdin The input as a string
  149. *
  150. * @return ProcessBuilder
  151. *
  152. * @throws InvalidArgumentException In case the argument is invalid
  153. */
  154. public function setInput($stdin)
  155. {
  156. $this->stdin = ProcessUtils::validateInput(sprintf('%s::%s', __CLASS__, __FUNCTION__), $stdin);
  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
  165. *
  166. * @return ProcessBuilder
  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 ProcessBuilder
  190. */
  191. public function setOption($name, $value)
  192. {
  193. $this->options[$name] = $value;
  194. return $this;
  195. }
  196. /**
  197. * Creates a Process instance and returns it.
  198. *
  199. * @return Process
  200. *
  201. * @throws LogicException In case no arguments have been provided
  202. */
  203. public function getProcess()
  204. {
  205. if (0 === count($this->prefix) && 0 === count($this->arguments)) {
  206. throw new LogicException('You must add() command arguments before calling getProcess().');
  207. }
  208. $options = $this->options;
  209. $arguments = array_merge($this->prefix, $this->arguments);
  210. $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
  211. if ($this->inheritEnv) {
  212. // include $_ENV for BC purposes
  213. $env = array_replace($_ENV, $_SERVER, $this->env);
  214. } else {
  215. $env = $this->env;
  216. }
  217. return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
  218. }
  219. }