adambrett/shell-wrapper

面向对象的shell命令封装

1.0.1 2022-12-06 16:08 UTC

This package is auto-updated.

Last update: 2024-09-23 12:01:04 UTC


README

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

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

安装

使用 composer

composer require adambrett/shell-wrapper '~1.0'

基本用法

Hello World

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

use AdamBrett\ShellWrapper\Command;
use AdamBrett\ShellWrapper\Command\Param;
use AdamBrett\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 AdamBrett\ShellWrapper\Runners\Exec;
use AdamBrett\ShellWrapper\Command\Builder as CommandBuilder;

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

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

use AdamBrett\ShellWrapper\Runners\Exec;
use AdamBrett\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 AdamBrett\ShellWrapper\Runners\Exec;
use AdamBrett\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 程序执行函数 的路径,并且与它们精确对应。运行器应该实现 \AdamBrett\ShellWrapper\Runners\Runner,这意味着您可以在需要使用 shell 时进行类型提示,它们应该可以互换使用。

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

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

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

子命令

用法

use AdamBrett\ShellWrapper\Command\SubCommand;

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

子命令不会以任何方式转义或修改,它们打算这样使用

use AdamBrett\ShellWrapper\Command\Command;
use AdamBrett\ShellWrapper\Command\SubCommand;

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

这将运行命令 jekyll build

参数

用法

use AdamBrett\ShellWrapper\Command\Argument;

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

$value 将在幕后自动转义,但 $name 不会,因此请确保您永远不会在 $name 中有用户输入,或者如果您有,请自行转义。

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

use AdamBrett\ShellWrapper\Command\Argument;

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

这将产生以下结果

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

标志

用法

use AdamBrett\ShellWrapper\Command\Flag;

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

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

参数

用法

use AdamBrett\ShellWrapper\Command\Param;

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

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

要求

  • PHP >= 8.1

贡献

拉取请求

  1. 从 php-shell-wrapper 仓库分叉
  2. 为每个功能或改进创建一个新的分支
  3. 从每个功能分支发送拉取请求到 master 分支

风格指南

本软件包符合PSR-4PSR-12规范。如果您发现不符合规范的地方,请通过pull request提交补丁。

测试

该库采用测试驱动开发。所有pull request都必须附带通过100%覆盖率单元测试。测试使用phpunit进行,模拟使用mockery