adambrett/shell-wrapper 问题修复 & 功能扩展

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

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

adambrett/shell-wrapper

Composer 安装命令:

composer require adambrett/shell-wrapper

包简介

An object oriented wrapper for shell commands

README 文档

README

PHP Shell Wrapper is a high level object-oriented wrapper for accessing the program execution functions in PHP.

Its primary purpose is to abstract away low level program execution functions in your application, allowing you to mock PHP Shell Wrapper in your tests, making applications which call shell functions easily testable.

Installation

Using composer:

composer require adambrett/shell-wrapper '~1.0'

Basic Usage

Hello World

Import the required classes into your namespace:

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

Instantiate a new shell runner:

$shell = new Exec();

Create the command:

$command = new Command('echo');

Add some parameters:

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

Now run the command:

$shell->run($command);

Which would run the command:

echo 'Hello World'

Command Builder

Whilst this library is highly object-oriented behind the scenes, you may not want to use it that way, what's where the Command Builder comes in. The command builder constructs a Command object behind the scenes, and then constructs the correct class for each method called, so you don't have to worry about it.

The Command Builder also has a fluent interface for extra syntactical sugar. Here's the above example re-written using the Command Builder:

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);

And here's a slightly less trivial example:

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);

Which would run:

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

and another:

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);

Which would run:

/usr/bin/jekyll serve --watch

Runners

Runners are paths directly in to the PHP program execution functions, and map to them by name exactly. Runners should all implement \AdamBrett\ShellWrapper\Runners\Runner, which means you can type hint on that whenever you need to use a shell and they should then all be interchangeable.

Some runners will also implement \AdamBrett\ShellWrapper\Runners\ReturnValue, but only where that is appropriate to the low level function.

Some runners (marked *) only emulate command running. This feature useful for testing.

Runner Returns Flush getOutput getReturnValue
Exec Last Line x x
Passthru x x
ShellExec Full Output or null
System Last Line or false x x
Dry* Exit code x x
Fake* Exit code x x

You can use FakeRunner in your unit tests to emulate running a command. You can use DryRunner for debugging purposes, or when your application uses a --dry-run type argument and you want to echo the command rather than run it.

SubCommands

Usage

use AdamBrett\ShellWrapper\Command\SubCommand;

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

Sub commands will not be escaped or modified in anyway, they are intended for use like so:

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

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

Which would run the command jekyll build.

Arguments

Usage

use AdamBrett\ShellWrapper\Command\Argument;

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

$value will be automatically escaped behind the scenes, but $name will not, so make sure you never have user input in $name, or if you do, escape it yourself.

If you want multiple arguments of the same name, then $value can be an array, like so:

use AdamBrett\ShellWrapper\Command\Argument;

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

Which would result in the following:

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

Flags

Usage

use AdamBrett\ShellWrapper\Command\Flag;

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

$flag will not be escaped, but can be a string rather than a single character, so new Flag('lla') is perfectly valid.

Params

Usage

use AdamBrett\ShellWrapper\Command\Param;

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

$param will be automatically escaped behind the scenes, but will otherwise be un-altered.

Requirements

  • PHP >= 8.1

Contributing

Pull Requests

  1. Fork the php-shell-wrapper repository
  2. Create a new branch for each feature or improvement
  3. Send a pull request from each feature branch to the master branch

Style Guide

This package is compliant with PSR-4 and PSR-12. If you notice compliance oversights, please send a patch via pull request.

Tests

The library is developed using test driven development. All pull requests should be accompanied by passing unit tests with 100% coverage. phpunit is used for testing and mockery is used for mocks.

adambrett/shell-wrapper 适用场景与选型建议

adambrett/shell-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 263.17k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2013 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 adambrett/shell-wrapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adambrett/shell-wrapper 我们能提供哪些服务?
定制开发 / 二次开发

基于 adambrett/shell-wrapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 263.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 31
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-11-26