ProcessBuilder.php 3.2 KB

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