ProcessBuilder.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  12. * Process builder.
  13. *
  14. * @author Kris Wallsmith <kris@symfony.com>
  15. */
  16. class ProcessBuilder
  17. {
  18. private $arguments;
  19. private $cwd;
  20. private $env;
  21. private $stdin;
  22. private $timeout;
  23. private $options;
  24. private $inheritEnv;
  25. public function __construct(array $arguments = array())
  26. {
  27. $this->arguments = $arguments;
  28. $this->timeout = 60;
  29. $this->options = array();
  30. $this->env = array();
  31. $this->inheritEnv = true;
  32. }
  33. public static function create(array $arguments = array())
  34. {
  35. return new static($arguments);
  36. }
  37. /**
  38. * Adds an unescaped argument to the command string.
  39. *
  40. * @param string $argument A command argument
  41. */
  42. public function add($argument)
  43. {
  44. $this->arguments[] = $argument;
  45. return $this;
  46. }
  47. public function setWorkingDirectory($cwd)
  48. {
  49. $this->cwd = $cwd;
  50. return $this;
  51. }
  52. public function inheritEnvironmentVariables($inheritEnv = true)
  53. {
  54. $this->inheritEnv = $inheritEnv;
  55. return $this;
  56. }
  57. public function setEnv($name, $value)
  58. {
  59. $this->env[$name] = $value;
  60. return $this;
  61. }
  62. public function setInput($stdin)
  63. {
  64. $this->stdin = $stdin;
  65. return $this;
  66. }
  67. /**
  68. * Sets the process timeout.
  69. *
  70. * To disable the timeout, set this value to null.
  71. *
  72. * @param float|null
  73. */
  74. public function setTimeout($timeout)
  75. {
  76. if (null === $timeout) {
  77. $this->timeout = null;
  78. return $this;
  79. }
  80. $timeout = (float) $timeout;
  81. if ($timeout < 0) {
  82. throw new \InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  83. }
  84. $this->timeout = $timeout;
  85. return $this;
  86. }
  87. public function setOption($name, $value)
  88. {
  89. $this->options[$name] = $value;
  90. return $this;
  91. }
  92. public function getProcess()
  93. {
  94. if (!count($this->arguments)) {
  95. throw new \LogicException('You must add() command arguments before calling getProcess().');
  96. }
  97. $options = $this->options;
  98. $script = implode(' ', array_map('escapeshellarg', $this->arguments));
  99. if ($this->inheritEnv) {
  100. $env = $this->env ? $this->env + $_ENV : null;
  101. } else {
  102. $env = $this->env;
  103. }
  104. return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
  105. }
  106. }