technodelight/shell-exec

更轻松地使用PHP在shell中组装和执行命令

1.0 2018-06-20 15:57 UTC

This package is not auto-updated.

Last update: 2024-09-18 13:21:47 UTC


README

更轻松地使用PHP在shell中组装和执行命令

安装

通过composer安装。

composer require technodelight/shell-exec

用法

<?php

use Technodelight\ShellExec\Command;
use Technodelight\ShellExec\Exec;

$shell = new Exec('which');
$output = $shell->exec(
    Command::create()
        ->withArgument('php')
        ->withStdErrToStdOut()
        ->withStdOutTo('/dev/null') // command will be assembled as 'which php 2>'
);

var_dump($output); // will be ["/usr/bin/php"]

当发生异常时,会抛出一个ShellCommandException实例。 ShellCommandException仍然包含命令结果。

<?php

use Technodelight\ShellExec\Command;
use Technodelight\ShellExec\Exec;
use Technodelight\ShellExec\ShellCommandException;

try {
    $shell = new Exec;
    $shell->exec(
        Command::create('which')
            ->withArgument('nope') // command will be assembled as 'which nope'
    );
} catch(ShellCommandException $e) {
    // $e->getCode() will be the shell return code for the executed command.
    var_dump($e->getResult());
}

有多种驱动程序可供选择

  • Exec
  • Passthru

还有一个特别的驱动程序

  • TestShell,可用于行为测试。对于behat,请参考ShellExec\FixtureContext

许可证

MIT许可证 (MIT)

版权所有 © 2015-2018 Zsolt Gál

特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的人士免费使用权,不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许将软件提供给其他人,以便他们可以这样做,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和不受侵犯的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任是基于合同、侵权或其他法律依据,无论该责任产生于、源于或与软件或其使用或其他方式有关。⏎