johnitvn / cli-runtime
PHP 运行时执行命令库
1.0.2
2015-07-22 16:31 UTC
Requires
- php: >=5.3.2
This package is auto-updated.
Last update: 2024-09-19 10:43:09 UTC
README
PHP 运行时执行命令库
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist johnitvn/cli-runtime "*"
或者将以下内容添加到您的 composer.json
文件的 require 部分:
"johnitvn/cli-runtime": "*"
用法
- 创建处理器
$workingdir = 'path/to/working_dir'; $process = new johnitvn\cliruntime\CliRuntimeProcess($workingdir);
如果在 CliRuntimeProcess 构造函数中将工作目录设置为 null,则当前工作目录将是调用 CliRuntimeProcess 的文件路径
- 运行命令而不返回并显示输出
$process->run('echo Hello');
- 运行命令并捕获输出
$output = array(); $process->runCapture('echo Hello',$output); var_dump($output);
例如,引用变量 $output 将获取命令输出行作为数组
- 运行命令并显示输出
$process->runDisplayOutput('echo Hello');
runDisplayout()
将运行命令并显示输出
- 命令构建器您可以使用
johnitvn\cliruntime\CommandBuidler
作为创建命令的实用工具
以下是一个使用 CommandBuilder 的简单示例
$command = new johnitvn\cliruntime\CommandBuidler('echo'); $command->addParam('Hello world'); echo $command->getCommand();
结果如下
echo Hello world
您可以通过以下代码片段了解如何使用 CommandBuilder 类
$builder = new CommandBuidler('phpunit'); $builder->addFlag('v') ->addArgument('stop-on-failure') ->addArgument('configuration', 'phpunit.xml') ->addParam('TestCase.php'); echo $builder->getCommand();
结果如下
phpunit TestCase.php -v --stop-on-failure --configuration=phpunit.xml
- 命令查找器(自 1.0.2 版本以来)您可以使用
johnitvn\cliruntime\CommandFinder
作为查找系统环境变量下命令真实路径的实用工具
use johnitvn\cliruntime\CommandFinder;
$realCommand = CommandFinder::findCommand('composer');
echo $realCommand;
结果如下:C:\\xampp\\php\\composer
您可以通过查看类注释了解更多关于使用方法的信息