1atdev/yii2-shell-wrapper

面向对象的shell命令包装器

安装: 63

依赖者: 0

建议者: 0

安全性: 0

星星: 0

分支: 0

类型:yii2-extension

1.0.7 2024-03-21 21:17 UTC

This package is auto-updated.

Last update: 2024-09-21 15:33:03 UTC


README

此包基于 adambrett/shell-wrapper

PHP Shell Wrapper

PHP Shell Wrapper 是一个高级面向对象的包装器,用于访问 PHP 中的程序执行函数。

其主要目的是抽象出应用程序中的低级程序执行函数,使您可以在测试中模拟 PHP Shell Wrapper,从而使调用 shell 函数的应用程序易于测试。

安装

使用 composer

composer require isatdev/yii2-shell-wrapper '^1.0'

基本用法

Hello World

将所需类导入您的命名空间

use IsAtDev\ShellWrapper\Command;
use IsAtDev\ShellWrapper\Command\Param;
use IsAtDev\ShellWrapper\Runners\Exec;

实例化一个新的 shell 运行器

$shell = new Exec();

创建命令

$command = new Command('echo');

添加一些参数

$command->addParam(new Param('Hello World'));

现在运行命令

$shell->run($command);

这将运行命令

echo 'Hello World'

命令构建器

虽然这个库在幕后高度面向对象,但您可能不想这样使用它,这就是命令构建器的作用所在。命令构建器在幕后构建一个 Command 对象,然后构建每个调用的正确类,这样您就不用担心了。

命令构建器还具有流畅的接口,提供了额外的语法糖。以下是使用命令构建器重写的上述示例

use IsAtDev\ShellWrapper\Runners\Exec;
use IsAtDev\ShellWrapper\Command\Builder as CommandBuilder;

$shell = new Exec();
$command = new CommandBuilder('echo');
$command->addParam('Hello World');
$shell->run($command);

以下是另一个稍微复杂一些的示例

use IsAtDev\ShellWrapper\Runners\Exec;
use IsAtDev\ShellWrapper\Command\Builder as CommandBuilder;

$shell = new Exec();
$command = new CommandBuilder('phpunit');
$command->addFlag('v')
    ->addArgument('stop-on-failure')
    ->addArgument('configuration', '~/phpunit.xml')
    ->addParam('~/tests/TestCase.php');
$shell->run($command);

这将运行

phpunit -v --stop-on-failure --configuration '~/phpunit.xml' '~/tests/TestCase.php'

并且另一个

use IsAtDev\ShellWrapper\Runners\Exec;
use IsAtDev\ShellWrapper\Command\Builder as CommandBuilder;

$shell = new Exec();
$command = new CommandBuilder('/usr/bin/jekyll');
$command->addSubCommand('serve')
    ->addArgument('watch');
$shell->run($command);

这将运行

/usr/bin/jekyll serve --watch

运行器

运行器是直接进入 PHP 程序执行函数 的路径,并且精确地按名称映射。运行器都应该实现 \IsAtDev\ShellWrapper\Runners\Runner,这意味着您可以在需要使用 shell 时对它进行类型提示,它们应该都是可互换的。

一些运行器还将实现 \IsAtDev\ShellWrapper\Runners\ReturnValue,但这仅适用于适合低级函数的情况。

一些运行器(标记为 *)仅模拟命令运行。此功能对于测试很有用。

运行器返回值清除getOutputgetReturnValue
Exec最后一行xx
Passthruxx
ShellExec完整输出或 null
系统最后一行或 falsexx
Dry*退出代码xx
Fake*退出代码xx

您可以在单元测试中使用 FakeRunner 来模拟命令运行。您可以使用 DryRunner 进行调试,或者当您的应用程序使用 --dry-run 类型的参数,并且您想输出命令而不是运行它时。

子命令

用法

use IsAtDev\ShellWrapper\Command\SubCommand;

$shell->addSubCommand(new SubCommand($subCommand));

子命令将不会被转义或修改,它们的使用方法如下

use IsAtDev\ShellWrapper\Command\Command;
use IsAtDev\ShellWrapper\Command\SubCommand;

$command = new Command('jekyll')
$shell->addSubCommand(new SubCommand('build'));

这将运行命令 jekyll build

参数

用法

use IsAtDev\ShellWrapper\Command\Argument;

$shell->addArgument(new Argument($name, $value));

$value 将在幕后自动转义,但 $name 不会,所以请确保您永远不会在 $name 中包含用户输入,或者如果您确实这样做,请自行转义它。

如果您想使用相同名称的多个参数,则 $value 可以是一个数组,如下所示

use IsAtDev\ShellWrapper\Command\Argument;

$shell->addArgument(new Argument('exclude', ['.git*', 'cache']));

这将产生以下结果

somecommand --exclude '.git*' --exclude 'cache'

标志

用法

use IsAtDev\ShellWrapper\Command\Flag;

$shell->addFlag(new Flag($flag));

$flag 不会被转义,但它可以是一个字符串,而不是单个字符,所以 new Flag('lla') 是完全有效的。

参数

用法

use IsAtDev\ShellWrapper\Command\Param;

$shell->addParam(new Param($param));

$param 将在幕后自动转义,但将保持不变。

需求

  • PHP >= 8.3