You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
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