ProcessBuilder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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;
  23. private $stdin;
  24. private $timeout;
  25. private $options;
  26. private $inheritEnv;
  27. private $prefix;
  28. public function __construct(array $arguments = array())
  29. {
  30. $this->arguments = $arguments;
  31. $this->timeout = 60;
  32. $this->options = array();
  33. $this->env = array();
  34. $this->inheritEnv = true;
  35. }
  36. public static function create(array $arguments = array())
  37. {
  38. return new static($arguments);
  39. }
  40. /**
  41. * Adds an unescaped argument to the command string.
  42. *
  43. * @param string $argument A command argument
  44. *
  45. * @return ProcessBuilder
  46. */
  47. public function add($argument)
  48. {
  49. $this->arguments[] = $argument;
  50. return $this;
  51. }
  52. /**
  53. * Adds an unescaped prefix to the command string.
  54. *
  55. * The prefix is preserved when reseting arguments.
  56. *
  57. * @param string $prefix A command prefix
  58. *
  59. * @return ProcessBuilder
  60. */
  61. public function setPrefix($prefix)
  62. {
  63. $this->prefix = $prefix;
  64. return $this;
  65. }
  66. /**
  67. * @param array $arguments
  68. *
  69. * @return ProcessBuilder
  70. */
  71. public function setArguments(array $arguments)
  72. {
  73. $this->arguments = $arguments;
  74. return $this;
  75. }
  76. public function setWorkingDirectory($cwd)
  77. {
  78. $this->cwd = $cwd;
  79. return $this;
  80. }
  81. public function inheritEnvironmentVariables($inheritEnv = true)
  82. {
  83. $this->inheritEnv = $inheritEnv;
  84. return $this;
  85. }
  86. public function setEnv($name, $value)
  87. {
  88. $this->env[$name] = $value;
  89. return $this;
  90. }
  91. public function setInput($stdin)
  92. {
  93. $this->stdin = $stdin;
  94. return $this;
  95. }
  96. /**
  97. * Sets the process timeout.
  98. *
  99. * To disable the timeout, set this value to null.
  100. *
  101. * @param float|null
  102. *
  103. * @return ProcessBuilder
  104. *
  105. * @throws InvalidArgumentException
  106. */
  107. public function setTimeout($timeout)
  108. {
  109. if (null === $timeout) {
  110. $this->timeout = null;
  111. return $this;
  112. }
  113. $timeout = (float) $timeout;
  114. if ($timeout < 0) {
  115. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  116. }
  117. $this->timeout = $timeout;
  118. return $this;
  119. }
  120. public function setOption($name, $value)
  121. {
  122. $this->options[$name] = $value;
  123. return $this;
  124. }
  125. public function getProcess()
  126. {
  127. if (!$this->prefix && !count($this->arguments)) {
  128. throw new LogicException('You must add() command arguments before calling getProcess().');
  129. }
  130. $options = $this->options;
  131. $arguments = $this->prefix ? array_merge(array($this->prefix), $this->arguments) : $this->arguments;
  132. $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
  133. if ($this->inheritEnv) {
  134. $env = $this->env ? $this->env + $_ENV : null;
  135. } else {
  136. $env = $this->env;
  137. }
  138. return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
  139. }
  140. }