1atdev / yii2-shell-wrapper
面向对象的shell命令包装器
Requires
- php: ^8.2
- composer-plugin-api: ^2.0
- 1atdev/yii2-env: ^1.0
Requires (Dev)
- composer/composer: ^2.2
- mockery/mockery: ^1.5
- phploc/phploc: ^7
- phpmd/phpmd: @stable
- phpunit/phpunit: ^9
- sebastian/phpcpd: ^6
- squizlabs/php_codesniffer: 3.*
- yiisoft/yii2: ^2.0
README
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
,但这仅适用于适合低级函数的情况。
一些运行器(标记为 *)仅模拟命令运行。此功能对于测试很有用。
运行器 | 返回值 | 清除 | getOutput | getReturnValue |
---|---|---|---|---|
Exec | 最后一行 | x | x | |
Passthru | x | x | ||
ShellExec | 完整输出或 null | |||
系统 | 最后一行或 false | x | x | |
Dry* | 退出代码 | x | x | |
Fake* | 退出代码 | x | x |
您可以在单元测试中使用 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