ProcessBuilder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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;
  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 a prefix to the command string.
  62. *
  63. * The prefix is preserved when resetting arguments.
  64. *
  65. * @param string $prefix A command prefix
  66. *
  67. * @return ProcessBuilder
  68. */
  69. public function setPrefix($prefix)
  70. {
  71. $this->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 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. * Sets the input of the process.
  131. *
  132. * @param string|null $stdin The input as a string
  133. *
  134. * @return ProcessBuilder
  135. *
  136. * @throws InvalidArgumentException In case the argument is invalid
  137. */
  138. public function setInput($stdin)
  139. {
  140. $this->stdin = ProcessUtils::validateInput(__METHOD__, $stdin);
  141. return $this;
  142. }
  143. /**
  144. * Sets the process timeout.
  145. *
  146. * To disable the timeout, set this value to null.
  147. *
  148. * @param float|null $timeout
  149. *
  150. * @return ProcessBuilder
  151. *
  152. * @throws InvalidArgumentException
  153. */
  154. public function setTimeout($timeout)
  155. {
  156. if (null === $timeout) {
  157. $this->timeout = null;
  158. return $this;
  159. }
  160. $timeout = (float) $timeout;
  161. if ($timeout < 0) {
  162. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  163. }
  164. $this->timeout = $timeout;
  165. return $this;
  166. }
  167. /**
  168. * Adds a proc_open option.
  169. *
  170. * @param string $name The option name
  171. * @param string $value The option value
  172. *
  173. * @return ProcessBuilder
  174. */
  175. public function setOption($name, $value)
  176. {
  177. $this->options[$name] = $value;
  178. return $this;
  179. }
  180. /**
  181. * Creates a Process instance and returns it.
  182. *
  183. * @return Process
  184. *
  185. * @throws LogicException In case no arguments have been provided
  186. */
  187. public function getProcess()
  188. {
  189. if (!$this->prefix && !count($this->arguments)) {
  190. throw new LogicException('You must add() command arguments before calling getProcess().');
  191. }
  192. $options = $this->options;
  193. $arguments = $this->prefix ? array_merge(array($this->prefix), $this->arguments) : $this->arguments;
  194. $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
  195. if ($this->inheritEnv) {
  196. $env = $this->env ? $this->env + $_ENV : null;
  197. } else {
  198. $env = $this->env;
  199. }
  200. return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
  201. }
  202. }