单旋律/挂人

执行命令的包装器,包括对干运行的支持(例如在单元测试期间)。

0.1.0 2019-12-30 08:38 UTC

This package is auto-updated.

Last update: 2024-08-29 05:36:54 UTC


README

执行命令的包装器,包括对干运行的支持(例如在单元测试期间)。

安装

Composer(推荐)

$ composer require monomelodies/hangman

用法

挂人提供了两个“执行者”:实际的ExecutionerFakeExecutioner,后者将简单地收集命令以供进一步检查

<?php

use Monomelodies\Hangman\{ Executioner, FakeExecutioner, Executable };

function setExecutioner() : Executable
{
    if (shouldActuallyRunCommands()) {
        $executioner = new Executioner;
    } else {
        $executioner = new FakeExecutioner;
    }
    return $executioner;
}

$executioner = setExecutioner();
$executioner->exec('some/command');

如你所见,Executable接口可用于类型提示。

方法

实际的和假的执行者都支持5个方法

  • exec
  • passthru
  • shellExec
  • system
  • chdir

这些方法与PHP的对应方法几乎相同,但它们可以被链式调用

<?php

$executioner
    ->exec('ls')
    ->passthru('ls')
    ->chdir('some/path')
    ->system('ls')
    ->chdir();

假的执行者

假的执行者(用于测试)支持一些额外的方法

  • succeeds - 强制命令成功(默认行为)
  • fails([int $error = 1]) - 强制命令失败
  • getCommands - 返回“已执行”命令的列表
  • flush - 类似于getCommands,但还将列表重置为空数组

由假的执行者“执行”的命令被包装在Command类中。