wesleywmd/element-shell

此包最新版本(1.0.0)没有提供许可信息。

用于从PHP运行shell命令的简单库

1.0.0 2019-01-07 04:51 UTC

This package is auto-updated.

Last update: 2024-09-07 17:49:26 UTC


README

运行shell命令的库。

如何安装库

使用composer安装库

composer install wesleywmd/element-shell

如何创建命令

只需一个字符串即可创建简单的命令。

$command = $commandFactory->create("ls -al");

命令可以有参数。通过工厂添加它们。

$command = $commandFactory->create("ls", ["-al"]);

或分离的方法。

$command = $commandFactory->create("ls");
$command->addArgument("-al");

您可以使用数组定义多个参数。

$command = $commandFactory->create("ls", ["-a", "-l"]);

$command = $commandFactory->create("ls");
$command->addArguments(["-a", "-l"]);

如何从cli获取结果对象

创建命令后,下一步是运行命令。

$result = $cli->execute($command);

使用 execute() 方法运行命令,然后返回一个结果对象。如果我们想与我们的命令交互呢?那么我们需要使用 interact() 方法。这将把stdin和stdout附加到运行命令的进程。

$result = $cli->interact($command);

如果您想在运行命令时指定目录,请将其指定为第二个参数。

$result = $cli->execute($command, "~/path/to/use");

如何使用结果对象

您从 execute()interact() 获取结果对象。您能够获取有关命令的大量信息。

$result->getOriginalCommand();
$result->getOriginalCwd();
$result->getResponse();
$result->getExitCode();
$result->getStdErr();
$result->getExecutionTime();