代码拉取完成,页面将自动刷新
同步操作将从 inhere/php-console 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-12-22
* Time: 11:44
*/
namespace Inhere\Console\Examples\Controllers;
use Inhere\Console\Controller;
use Inhere\Console\Utils\CliUtil;
use Inhere\Console\Utils\ProcessUtil;
/**
* Class ProcessController
* @package Inhere\Console\Examples\Controllers
*/
class ProcessController extends Controller
{
protected static $name = 'process';
protected static $description = 'Some simple process to create and use examples';
protected static function commandAliases(): array
{
return [
'cpr' => 'childProcess',
'mpr' => 'multiProcess',
'dr' => 'daemonRun',
'rs' => 'runScript',
'rb' => 'runInBackground',
];
}
/**
* simple process example for child-process
*/
public function runScriptCommand()
{
/*$script = '<?php echo "foo"; ?>';*/
$script = '<?php print_r($_SERVER); ?>';
// $tmpDir = CliUtil::getTempDir();
// $tmpFile = $tmpDir . '/' . md5($script) . '.php';
// file_put_contents($tmpFile, $script);
$descriptorSpec = [
0 => ['pipe', 'r'], // 标准输入,子进程从此管道中读取数据
1 => ['pipe', 'w'], // 标准输出,子进程向此管道中写入数据
2 => ['file', $this->app->getRootPath() . '/examples/tmp/error-output.log', 'a'] // 标准错误,写入到一个文件
];
$process = proc_open('php', $descriptorSpec, $pipes);
if (\is_resource($process)) {
// $pipes 现在看起来是这样的:
// 0 => 可以向子进程标准输入写入的句柄
// 1 => 可以从子进程标准输出读取的句柄
// 错误输出将被追加到文件 error-output.txt
fwrite($pipes[0], $script);
fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$this->write("RESULT:\n" . $result);
// 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。
$retVal = proc_close($process);
echo "command returned $retVal\n";
}
ProcessUtil::run();
}
/**
* simple process example for child-process
*/
public function childProcessCommand()
{
$ret = ProcessUtil::create(function ($pid) {
echo "print in process $pid";
sleep(5);
});
if ($ret === false) {
$this->output->liteError('current env is not support process create.');
}
}
/**
* simple process example for daemon run
*/
public function daemonRunCommand()
{
$ret = ProcessUtil::daemonRun(function ($pid){
$this->output->info("will running background by new process: $pid");
});
if ($ret === false) {
$this->output->liteError('current env is not support process create.');
}
}
/**
* simple process example for run In Background
*/
public function runInBackgroundCommand()
{
$script = '<?php print_r($_SERVER); ?>';
$ret = ProcessUtil::runInBackground("php $script");
if ($ret === false) {
$this->output->liteError('current env is not support process create.');
}
}
/**
* simple process example for multi-process
* @options
*
*/
public function multiProcessCommand()
{
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。