Builder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\process;
  12. use think\Process;
  13. class Builder
  14. {
  15. private $arguments;
  16. private $cwd;
  17. private $env = null;
  18. private $input;
  19. private $timeout = 60;
  20. private $options = [];
  21. private $inheritEnv = true;
  22. private $prefix = [];
  23. private $outputDisabled = false;
  24. /**
  25. * 构造方法
  26. * @param string[] $arguments 参数
  27. */
  28. public function __construct(array $arguments = [])
  29. {
  30. $this->arguments = $arguments;
  31. }
  32. /**
  33. * 创建一个实例
  34. * @param string[] $arguments 参数
  35. * @return self
  36. */
  37. public static function create(array $arguments = [])
  38. {
  39. return new static($arguments);
  40. }
  41. /**
  42. * 添加一个参数
  43. * @param string $argument 参数
  44. * @return self
  45. */
  46. public function add($argument)
  47. {
  48. $this->arguments[] = $argument;
  49. return $this;
  50. }
  51. /**
  52. * 添加一个前缀
  53. * @param string|array $prefix
  54. * @return self
  55. */
  56. public function setPrefix($prefix)
  57. {
  58. $this->prefix = is_array($prefix) ? $prefix : [$prefix];
  59. return $this;
  60. }
  61. /**
  62. * 设置参数
  63. * @param string[] $arguments
  64. * @return self
  65. */
  66. public function setArguments(array $arguments)
  67. {
  68. $this->arguments = $arguments;
  69. return $this;
  70. }
  71. /**
  72. * 设置工作目录
  73. * @param null|string $cwd
  74. * @return self
  75. */
  76. public function setWorkingDirectory($cwd)
  77. {
  78. $this->cwd = $cwd;
  79. return $this;
  80. }
  81. /**
  82. * 是否初始化环境变量
  83. * @param bool $inheritEnv
  84. * @return self
  85. */
  86. public function inheritEnvironmentVariables($inheritEnv = true)
  87. {
  88. $this->inheritEnv = $inheritEnv;
  89. return $this;
  90. }
  91. /**
  92. * 设置环境变量
  93. * @param string $name
  94. * @param null|string $value
  95. * @return self
  96. */
  97. public function setEnv($name, $value)
  98. {
  99. $this->env[$name] = $value;
  100. return $this;
  101. }
  102. /**
  103. * 添加环境变量
  104. * @param array $variables
  105. * @return self
  106. */
  107. public function addEnvironmentVariables(array $variables)
  108. {
  109. $this->env = array_replace($this->env, $variables);
  110. return $this;
  111. }
  112. /**
  113. * 设置输入
  114. * @param mixed $input
  115. * @return self
  116. */
  117. public function setInput($input)
  118. {
  119. $this->input = Utils::validateInput(sprintf('%s::%s', __CLASS__, __FUNCTION__), $input);
  120. return $this;
  121. }
  122. /**
  123. * 设置超时时间
  124. * @param float|null $timeout
  125. * @return self
  126. */
  127. public function setTimeout($timeout)
  128. {
  129. if (null === $timeout) {
  130. $this->timeout = null;
  131. return $this;
  132. }
  133. $timeout = (float) $timeout;
  134. if ($timeout < 0) {
  135. throw new \InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  136. }
  137. $this->timeout = $timeout;
  138. return $this;
  139. }
  140. /**
  141. * 设置proc_open选项
  142. * @param string $name
  143. * @param string $value
  144. * @return self
  145. */
  146. public function setOption($name, $value)
  147. {
  148. $this->options[$name] = $value;
  149. return $this;
  150. }
  151. /**
  152. * 禁止输出
  153. * @return self
  154. */
  155. public function disableOutput()
  156. {
  157. $this->outputDisabled = true;
  158. return $this;
  159. }
  160. /**
  161. * 开启输出
  162. * @return self
  163. */
  164. public function enableOutput()
  165. {
  166. $this->outputDisabled = false;
  167. return $this;
  168. }
  169. /**
  170. * 创建一个Process实例
  171. * @return Process
  172. */
  173. public function getProcess()
  174. {
  175. if (0 === count($this->prefix) && 0 === count($this->arguments)) {
  176. throw new \LogicException('You must add() command arguments before calling getProcess().');
  177. }
  178. $options = $this->options;
  179. $arguments = array_merge($this->prefix, $this->arguments);
  180. $script = implode(' ', array_map([__NAMESPACE__ . '\\Utils', 'escapeArgument'], $arguments));
  181. if ($this->inheritEnv) {
  182. // include $_ENV for BC purposes
  183. $env = array_replace($_ENV, $_SERVER, $this->env);
  184. } else {
  185. $env = $this->env;
  186. }
  187. $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
  188. if ($this->outputDisabled) {
  189. $process->disableOutput();
  190. }
  191. return $process;
  192. }
  193. }