moccalotto/process 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moccalotto/process

最新稳定版本:0.7.0

Composer 安装命令:

composer require moccalotto/process

包简介

Tiny proc_open wrapper for PHP

README 文档

README

Build Status

A PHP process execution helper.

It assists in I/O from processes. See examples below or run demo.php to see for yourself.

Installation

To add this package as a local, per-project dependency to your project, simply add a dependency on moccalotto/process to your project's composer.json file like so:

{
    "require": {
        "moccalotto/process": "~0.7"
    }
}

Alternatively simply call composer require moccalotto/process

Demo

#!/usr/bin/env php
<?php

use Moccalotto\Process\Process;

require 'vendor/autoload.php';



/*
| Execute ls
|------------
| Execute ls and capture the output.
| Then print all the captured output.
*/
$proc = Process::execute('ls -a');
echo $proc->readToEnd();

echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;

/*
| Execute cat and talk to it interactively
|------------------------------------------
| Execute cat, and input a few lines into stdin.
| Cat should echo those lines out to stdout.
| Then, we read those lines from stdout.
| Then we ask cat to terminate.
| Then we print cat's exitcode.
*/
$proc = Process::execute('cat');
$proc->write('========================' . PHP_EOL);
$proc->write('====   CAT EXAMPLE  ====' . PHP_EOL);
$proc->write('==== cat is working ====' . PHP_EOL);
$proc->write('========================' . PHP_EOL);

// read the lines again.
// Beware that this process is blocking.
// if you call readline on a process that
// has not yet printed a newline character,
// your program will hang!
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();

$proc->signalStop();
echo $proc->readErrorToEnd();
echo PHP_EOL;
printf('Exit code: %d', $proc->exitCode());

echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;

/*
| Execute a terminal and run commands in it
|-------------------------------------------
| Execute bash.
| Run a few commands inside it, and capture their output.
| Then wait for the commands to finish executing,
| Then run exit to make bash exit neatly.
| Then ask bash to terminate if it has not executed the 'exit' command yet.
| Then print all the captured output.
*/
$proc = Process::execute('/bin/bash');
$proc->write('echo "=========================="' . PHP_EOL);
$proc->write('echo "====   BASH EXAMPLE   ===="' . PHP_EOL);
$proc->write('echo "==== several commands ===="' . PHP_EOL);
$proc->write('echo "====    in a shell    ===="' . PHP_EOL);
$proc->write('echo "=========================="' . PHP_EOL);
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
$proc->write('ls' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('uname' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('export | head' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('echo bye...' . PHP_EOL);
$proc->write("exit");

// give all commands time to execute.
usleep(100000);

$proc->signalStop();
echo $proc->readToEnd();

统计信息

  • 总下载量: 108
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-04