You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
233
thinkphp/library/think/process/Builder.php
Executable file
233
thinkphp/library/think/process/Builder.php
Executable file
@ -0,0 +1,233 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process;
|
||||
|
||||
use think\Process;
|
||||
|
||||
class Builder
|
||||
{
|
||||
private $arguments;
|
||||
private $cwd;
|
||||
private $env = null;
|
||||
private $input;
|
||||
private $timeout = 60;
|
||||
private $options = [];
|
||||
private $inheritEnv = true;
|
||||
private $prefix = [];
|
||||
private $outputDisabled = false;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @param string[] $arguments 参数
|
||||
*/
|
||||
public function __construct(array $arguments = [])
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个实例
|
||||
* @param string[] $arguments 参数
|
||||
* @return self
|
||||
*/
|
||||
public static function create(array $arguments = [])
|
||||
{
|
||||
return new static($arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个参数
|
||||
* @param string $argument 参数
|
||||
* @return self
|
||||
*/
|
||||
public function add($argument)
|
||||
{
|
||||
$this->arguments[] = $argument;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个前缀
|
||||
* @param string|array $prefix
|
||||
* @return self
|
||||
*/
|
||||
public function setPrefix($prefix)
|
||||
{
|
||||
$this->prefix = is_array($prefix) ? $prefix : [$prefix];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置参数
|
||||
* @param string[] $arguments
|
||||
* @return self
|
||||
*/
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置工作目录
|
||||
* @param null|string $cwd
|
||||
* @return self
|
||||
*/
|
||||
public function setWorkingDirectory($cwd)
|
||||
{
|
||||
$this->cwd = $cwd;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否初始化环境变量
|
||||
* @param bool $inheritEnv
|
||||
* @return self
|
||||
*/
|
||||
public function inheritEnvironmentVariables($inheritEnv = true)
|
||||
{
|
||||
$this->inheritEnv = $inheritEnv;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置环境变量
|
||||
* @param string $name
|
||||
* @param null|string $value
|
||||
* @return self
|
||||
*/
|
||||
public function setEnv($name, $value)
|
||||
{
|
||||
$this->env[$name] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加环境变量
|
||||
* @param array $variables
|
||||
* @return self
|
||||
*/
|
||||
public function addEnvironmentVariables(array $variables)
|
||||
{
|
||||
$this->env = array_replace($this->env, $variables);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置输入
|
||||
* @param mixed $input
|
||||
* @return self
|
||||
*/
|
||||
public function setInput($input)
|
||||
{
|
||||
$this->input = Utils::validateInput(sprintf('%s::%s', __CLASS__, __FUNCTION__), $input);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置超时时间
|
||||
* @param float|null $timeout
|
||||
* @return self
|
||||
*/
|
||||
public function setTimeout($timeout)
|
||||
{
|
||||
if (null === $timeout) {
|
||||
$this->timeout = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$timeout = (float) $timeout;
|
||||
|
||||
if ($timeout < 0) {
|
||||
throw new \InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
|
||||
}
|
||||
|
||||
$this->timeout = $timeout;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置proc_open选项
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return self
|
||||
*/
|
||||
public function setOption($name, $value)
|
||||
{
|
||||
$this->options[$name] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁止输出
|
||||
* @return self
|
||||
*/
|
||||
public function disableOutput()
|
||||
{
|
||||
$this->outputDisabled = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启输出
|
||||
* @return self
|
||||
*/
|
||||
public function enableOutput()
|
||||
{
|
||||
$this->outputDisabled = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个Process实例
|
||||
* @return Process
|
||||
*/
|
||||
public function getProcess()
|
||||
{
|
||||
if (0 === count($this->prefix) && 0 === count($this->arguments)) {
|
||||
throw new \LogicException('You must add() command arguments before calling getProcess().');
|
||||
}
|
||||
|
||||
$options = $this->options;
|
||||
|
||||
$arguments = array_merge($this->prefix, $this->arguments);
|
||||
$script = implode(' ', array_map([__NAMESPACE__ . '\\Utils', 'escapeArgument'], $arguments));
|
||||
|
||||
if ($this->inheritEnv) {
|
||||
// include $_ENV for BC purposes
|
||||
$env = array_replace($_ENV, $_SERVER, $this->env);
|
||||
} else {
|
||||
$env = $this->env;
|
||||
}
|
||||
|
||||
$process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
|
||||
|
||||
if ($this->outputDisabled) {
|
||||
$process->disableOutput();
|
||||
}
|
||||
|
||||
return $process;
|
||||
}
|
||||
}
|
75
thinkphp/library/think/process/Utils.php
Executable file
75
thinkphp/library/think/process/Utils.php
Executable file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TopThink [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2015 http://www.topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: zhangyajun <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process;
|
||||
|
||||
class Utils
|
||||
{
|
||||
|
||||
/**
|
||||
* 转义字符串
|
||||
* @param string $argument
|
||||
* @return string
|
||||
*/
|
||||
public static function escapeArgument($argument)
|
||||
{
|
||||
|
||||
if ('' === $argument) {
|
||||
return escapeshellarg($argument);
|
||||
}
|
||||
$escapedArgument = '';
|
||||
$quote = false;
|
||||
foreach (preg_split('/(")/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
|
||||
if ('"' === $part) {
|
||||
$escapedArgument .= '\\"';
|
||||
} elseif (self::isSurroundedBy($part, '%')) {
|
||||
// Avoid environment variable expansion
|
||||
$escapedArgument .= '^%"' . substr($part, 1, -1) . '"^%';
|
||||
} else {
|
||||
// escape trailing backslash
|
||||
if ('\\' === substr($part, -1)) {
|
||||
$part .= '\\';
|
||||
}
|
||||
$quote = true;
|
||||
$escapedArgument .= $part;
|
||||
}
|
||||
}
|
||||
if ($quote) {
|
||||
$escapedArgument = '"' . $escapedArgument . '"';
|
||||
}
|
||||
return $escapedArgument;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证并进行规范化Process输入。
|
||||
* @param string $caller
|
||||
* @param mixed $input
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function validateInput($caller, $input)
|
||||
{
|
||||
if (null !== $input) {
|
||||
if (is_resource($input)) {
|
||||
return $input;
|
||||
}
|
||||
if (is_scalar($input)) {
|
||||
return (string) $input;
|
||||
}
|
||||
throw new \InvalidArgumentException(sprintf('%s only accepts strings or stream resources.', $caller));
|
||||
}
|
||||
return $input;
|
||||
}
|
||||
|
||||
private static function isSurroundedBy($arg, $char)
|
||||
{
|
||||
return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
|
||||
}
|
||||
|
||||
}
|
43
thinkphp/library/think/process/exception/Faild.php
Executable file
43
thinkphp/library/think/process/exception/Faild.php
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process\exception;
|
||||
|
||||
|
||||
use think\Process;
|
||||
|
||||
class Failed extends \RuntimeException
|
||||
{
|
||||
|
||||
private $process;
|
||||
|
||||
public function __construct(Process $process)
|
||||
{
|
||||
if ($process->isSuccessful()) {
|
||||
throw new \InvalidArgumentException('Expected a failed process, but the given process was successful.');
|
||||
}
|
||||
|
||||
$error = sprintf('The command "%s" failed.' . "\nExit Code: %s(%s)", $process->getCommandLine(), $process->getExitCode(), $process->getExitCodeText());
|
||||
|
||||
if (!$process->isOutputDisabled()) {
|
||||
$error .= sprintf("\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", $process->getOutput(), $process->getErrorOutput());
|
||||
}
|
||||
|
||||
parent::__construct($error);
|
||||
|
||||
$this->process = $process;
|
||||
}
|
||||
|
||||
public function getProcess()
|
||||
{
|
||||
return $this->process;
|
||||
}
|
||||
}
|
42
thinkphp/library/think/process/exception/Failed.php
Executable file
42
thinkphp/library/think/process/exception/Failed.php
Executable file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process\exception;
|
||||
|
||||
use think\Process;
|
||||
|
||||
class Failed extends \RuntimeException
|
||||
{
|
||||
|
||||
private $process;
|
||||
|
||||
public function __construct(Process $process)
|
||||
{
|
||||
if ($process->isSuccessful()) {
|
||||
throw new \InvalidArgumentException('Expected a failed process, but the given process was successful.');
|
||||
}
|
||||
|
||||
$error = sprintf('The command "%s" failed.' . "\nExit Code: %s(%s)", $process->getCommandLine(), $process->getExitCode(), $process->getExitCodeText());
|
||||
|
||||
if (!$process->isOutputDisabled()) {
|
||||
$error .= sprintf("\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", $process->getOutput(), $process->getErrorOutput());
|
||||
}
|
||||
|
||||
parent::__construct($error);
|
||||
|
||||
$this->process = $process;
|
||||
}
|
||||
|
||||
public function getProcess()
|
||||
{
|
||||
return $this->process;
|
||||
}
|
||||
}
|
61
thinkphp/library/think/process/exception/Timeout.php
Executable file
61
thinkphp/library/think/process/exception/Timeout.php
Executable file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process\exception;
|
||||
|
||||
use think\Process;
|
||||
|
||||
class Timeout extends \RuntimeException
|
||||
{
|
||||
|
||||
const TYPE_GENERAL = 1;
|
||||
const TYPE_IDLE = 2;
|
||||
|
||||
private $process;
|
||||
private $timeoutType;
|
||||
|
||||
public function __construct(Process $process, $timeoutType)
|
||||
{
|
||||
$this->process = $process;
|
||||
$this->timeoutType = $timeoutType;
|
||||
|
||||
parent::__construct(sprintf('The process "%s" exceeded the timeout of %s seconds.', $process->getCommandLine(), $this->getExceededTimeout()));
|
||||
}
|
||||
|
||||
public function getProcess()
|
||||
{
|
||||
return $this->process;
|
||||
}
|
||||
|
||||
public function isGeneralTimeout()
|
||||
{
|
||||
return $this->timeoutType === self::TYPE_GENERAL;
|
||||
}
|
||||
|
||||
public function isIdleTimeout()
|
||||
{
|
||||
return $this->timeoutType === self::TYPE_IDLE;
|
||||
}
|
||||
|
||||
public function getExceededTimeout()
|
||||
{
|
||||
switch ($this->timeoutType) {
|
||||
case self::TYPE_GENERAL:
|
||||
return $this->process->getTimeout();
|
||||
|
||||
case self::TYPE_IDLE:
|
||||
return $this->process->getIdleTimeout();
|
||||
|
||||
default:
|
||||
throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType));
|
||||
}
|
||||
}
|
||||
}
|
93
thinkphp/library/think/process/pipes/Pipes.php
Executable file
93
thinkphp/library/think/process/pipes/Pipes.php
Executable file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process\pipes;
|
||||
|
||||
abstract class Pipes
|
||||
{
|
||||
|
||||
/** @var array */
|
||||
public $pipes = [];
|
||||
|
||||
/** @var string */
|
||||
protected $inputBuffer = '';
|
||||
/** @var resource|null */
|
||||
protected $input;
|
||||
|
||||
/** @var bool */
|
||||
private $blocked = true;
|
||||
|
||||
const CHUNK_SIZE = 16384;
|
||||
|
||||
/**
|
||||
* 返回用于 proc_open 描述符的数组
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getDescriptors();
|
||||
|
||||
/**
|
||||
* 返回一个数组的索引由其相关的流,以防这些管道使用的临时文件的文件名。
|
||||
* @return string[]
|
||||
*/
|
||||
abstract public function getFiles();
|
||||
|
||||
/**
|
||||
* 文件句柄和管道中读取数据。
|
||||
* @param bool $blocking 是否使用阻塞调用
|
||||
* @param bool $close 是否要关闭管道,如果他们已经到达 EOF。
|
||||
* @return string[]
|
||||
*/
|
||||
abstract public function readAndWrite($blocking, $close = false);
|
||||
|
||||
/**
|
||||
* 返回当前状态如果有打开的文件句柄或管道。
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function areOpen();
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
foreach ($this->pipes as $pipe) {
|
||||
fclose($pipe);
|
||||
}
|
||||
$this->pipes = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查系统调用已被中断
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasSystemCallBeenInterrupted()
|
||||
{
|
||||
$lastError = error_get_last();
|
||||
|
||||
return isset($lastError['message']) && false !== stripos($lastError['message'], 'interrupted system call');
|
||||
}
|
||||
|
||||
protected function unblock()
|
||||
{
|
||||
if (!$this->blocked) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->pipes as $pipe) {
|
||||
stream_set_blocking($pipe, 0);
|
||||
}
|
||||
if (null !== $this->input) {
|
||||
stream_set_blocking($this->input, 0);
|
||||
}
|
||||
|
||||
$this->blocked = false;
|
||||
}
|
||||
}
|
196
thinkphp/library/think/process/pipes/Unix.php
Executable file
196
thinkphp/library/think/process/pipes/Unix.php
Executable file
@ -0,0 +1,196 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process\pipes;
|
||||
|
||||
use think\Process;
|
||||
|
||||
class Unix extends Pipes
|
||||
{
|
||||
|
||||
/** @var bool */
|
||||
private $ttyMode;
|
||||
/** @var bool */
|
||||
private $ptyMode;
|
||||
/** @var bool */
|
||||
private $disableOutput;
|
||||
|
||||
public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
|
||||
{
|
||||
$this->ttyMode = (bool) $ttyMode;
|
||||
$this->ptyMode = (bool) $ptyMode;
|
||||
$this->disableOutput = (bool) $disableOutput;
|
||||
|
||||
if (is_resource($input)) {
|
||||
$this->input = $input;
|
||||
} else {
|
||||
$this->inputBuffer = (string) $input;
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescriptors()
|
||||
{
|
||||
if ($this->disableOutput) {
|
||||
$nullstream = fopen('/dev/null', 'c');
|
||||
|
||||
return [
|
||||
['pipe', 'r'],
|
||||
$nullstream,
|
||||
$nullstream,
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->ttyMode) {
|
||||
return [
|
||||
['file', '/dev/tty', 'r'],
|
||||
['file', '/dev/tty', 'w'],
|
||||
['file', '/dev/tty', 'w'],
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->ptyMode && Process::isPtySupported()) {
|
||||
return [
|
||||
['pty'],
|
||||
['pty'],
|
||||
['pty'],
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
['pipe', 'r'],
|
||||
['pipe', 'w'], // stdout
|
||||
['pipe', 'w'], // stderr
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFiles()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readAndWrite($blocking, $close = false)
|
||||
{
|
||||
|
||||
if (1 === count($this->pipes) && [0] === array_keys($this->pipes)) {
|
||||
fclose($this->pipes[0]);
|
||||
unset($this->pipes[0]);
|
||||
}
|
||||
|
||||
if (empty($this->pipes)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$this->unblock();
|
||||
|
||||
$read = [];
|
||||
|
||||
if (null !== $this->input) {
|
||||
$r = array_merge($this->pipes, ['input' => $this->input]);
|
||||
} else {
|
||||
$r = $this->pipes;
|
||||
}
|
||||
|
||||
unset($r[0]);
|
||||
|
||||
$w = isset($this->pipes[0]) ? [$this->pipes[0]] : null;
|
||||
$e = null;
|
||||
|
||||
if (false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
|
||||
|
||||
if (!$this->hasSystemCallBeenInterrupted()) {
|
||||
$this->pipes = [];
|
||||
}
|
||||
|
||||
return $read;
|
||||
}
|
||||
|
||||
if (0 === $n) {
|
||||
return $read;
|
||||
}
|
||||
|
||||
foreach ($r as $pipe) {
|
||||
|
||||
$type = (false !== $found = array_search($pipe, $this->pipes)) ? $found : 'input';
|
||||
$data = '';
|
||||
while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
|
||||
$data .= $dataread;
|
||||
}
|
||||
|
||||
if ('' !== $data) {
|
||||
if ('input' === $type) {
|
||||
$this->inputBuffer .= $data;
|
||||
} else {
|
||||
$read[$type] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $data || (true === $close && feof($pipe) && '' === $data)) {
|
||||
if ('input' === $type) {
|
||||
$this->input = null;
|
||||
} else {
|
||||
fclose($this->pipes[$type]);
|
||||
unset($this->pipes[$type]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $w && 0 < count($w)) {
|
||||
while (strlen($this->inputBuffer)) {
|
||||
$written = fwrite($w[0], $this->inputBuffer, 2 << 18); // write 512k
|
||||
if ($written > 0) {
|
||||
$this->inputBuffer = (string) substr($this->inputBuffer, $written);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
|
||||
fclose($this->pipes[0]);
|
||||
unset($this->pipes[0]);
|
||||
}
|
||||
|
||||
return $read;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function areOpen()
|
||||
{
|
||||
return (bool) $this->pipes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 UnixPipes 实例
|
||||
* @param Process $process
|
||||
* @param string|resource $input
|
||||
* @return self
|
||||
*/
|
||||
public static function create(Process $process, $input)
|
||||
{
|
||||
return new static($process->isTty(), $process->isPty(), $input, $process->isOutputDisabled());
|
||||
}
|
||||
}
|
228
thinkphp/library/think/process/pipes/Windows.php
Executable file
228
thinkphp/library/think/process/pipes/Windows.php
Executable file
@ -0,0 +1,228 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\process\pipes;
|
||||
|
||||
use think\Process;
|
||||
|
||||
class Windows extends Pipes
|
||||
{
|
||||
|
||||
/** @var array */
|
||||
private $files = [];
|
||||
/** @var array */
|
||||
private $fileHandles = [];
|
||||
/** @var array */
|
||||
private $readBytes = [
|
||||
Process::STDOUT => 0,
|
||||
Process::STDERR => 0,
|
||||
];
|
||||
/** @var bool */
|
||||
private $disableOutput;
|
||||
|
||||
public function __construct($disableOutput, $input)
|
||||
{
|
||||
$this->disableOutput = (bool) $disableOutput;
|
||||
|
||||
if (!$this->disableOutput) {
|
||||
|
||||
$this->files = [
|
||||
Process::STDOUT => tempnam(sys_get_temp_dir(), 'sf_proc_stdout'),
|
||||
Process::STDERR => tempnam(sys_get_temp_dir(), 'sf_proc_stderr'),
|
||||
];
|
||||
foreach ($this->files as $offset => $file) {
|
||||
$this->fileHandles[$offset] = fopen($this->files[$offset], 'rb');
|
||||
if (false === $this->fileHandles[$offset]) {
|
||||
throw new \RuntimeException('A temporary file could not be opened to write the process output to, verify that your TEMP environment variable is writable');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_resource($input)) {
|
||||
$this->input = $input;
|
||||
} else {
|
||||
$this->inputBuffer = $input;
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
$this->removeFiles();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescriptors()
|
||||
{
|
||||
if ($this->disableOutput) {
|
||||
$nullstream = fopen('NUL', 'c');
|
||||
|
||||
return [
|
||||
['pipe', 'r'],
|
||||
$nullstream,
|
||||
$nullstream,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
['pipe', 'r'],
|
||||
['file', 'NUL', 'w'],
|
||||
['file', 'NUL', 'w'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFiles()
|
||||
{
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function readAndWrite($blocking, $close = false)
|
||||
{
|
||||
$this->write($blocking, $close);
|
||||
|
||||
$read = [];
|
||||
$fh = $this->fileHandles;
|
||||
foreach ($fh as $type => $fileHandle) {
|
||||
if (0 !== fseek($fileHandle, $this->readBytes[$type])) {
|
||||
continue;
|
||||
}
|
||||
$data = '';
|
||||
$dataread = null;
|
||||
while (!feof($fileHandle)) {
|
||||
if (false !== $dataread = fread($fileHandle, self::CHUNK_SIZE)) {
|
||||
$data .= $dataread;
|
||||
}
|
||||
}
|
||||
if (0 < $length = strlen($data)) {
|
||||
$this->readBytes[$type] += $length;
|
||||
$read[$type] = $data;
|
||||
}
|
||||
|
||||
if (false === $dataread || (true === $close && feof($fileHandle) && '' === $data)) {
|
||||
fclose($this->fileHandles[$type]);
|
||||
unset($this->fileHandles[$type]);
|
||||
}
|
||||
}
|
||||
|
||||
return $read;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function areOpen()
|
||||
{
|
||||
return (bool) $this->pipes && (bool) $this->fileHandles;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
parent::close();
|
||||
foreach ($this->fileHandles as $handle) {
|
||||
fclose($handle);
|
||||
}
|
||||
$this->fileHandles = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 WindowsPipes 实例。
|
||||
* @param Process $process
|
||||
* @param $input
|
||||
* @return self
|
||||
*/
|
||||
public static function create(Process $process, $input)
|
||||
{
|
||||
return new static($process->isOutputDisabled(), $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除临时文件
|
||||
*/
|
||||
private function removeFiles()
|
||||
{
|
||||
foreach ($this->files as $filename) {
|
||||
if (file_exists($filename)) {
|
||||
@unlink($filename);
|
||||
}
|
||||
}
|
||||
$this->files = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入到 stdin 输入
|
||||
* @param bool $blocking
|
||||
* @param bool $close
|
||||
*/
|
||||
private function write($blocking, $close)
|
||||
{
|
||||
if (empty($this->pipes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->unblock();
|
||||
|
||||
$r = null !== $this->input ? ['input' => $this->input] : null;
|
||||
$w = isset($this->pipes[0]) ? [$this->pipes[0]] : null;
|
||||
$e = null;
|
||||
|
||||
if (false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
|
||||
if (!$this->hasSystemCallBeenInterrupted()) {
|
||||
$this->pipes = [];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 === $n) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (null !== $w && 0 < count($r)) {
|
||||
$data = '';
|
||||
while ($dataread = fread($r['input'], self::CHUNK_SIZE)) {
|
||||
$data .= $dataread;
|
||||
}
|
||||
|
||||
$this->inputBuffer .= $data;
|
||||
|
||||
if (false === $data || (true === $close && feof($r['input']) && '' === $data)) {
|
||||
$this->input = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $w && 0 < count($w)) {
|
||||
while (strlen($this->inputBuffer)) {
|
||||
$written = fwrite($w[0], $this->inputBuffer, 2 << 18);
|
||||
if ($written > 0) {
|
||||
$this->inputBuffer = (string) substr($this->inputBuffer, $written);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
|
||||
fclose($this->pipes[0]);
|
||||
unset($this->pipes[0]);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user