mrrio / shellwrap
将任何命令行工具用作PHP函数。
0.4.1
2013-12-23 04:05 UTC
Requires
- php: >=5.3.0
- d11wtq/boris: 1.0.*
This package is not auto-updated.
Last update: 2024-09-20 21:05:59 UTC
README
这是什么?
这是一种在PHP中使用强大的Linux/Unix工具的优雅方式。轻松地、逻辑地将命令连接起来,捕获错误作为PHP异常,并使用简单而强大的语法。与任何命令行工具自动协同工作。
特性
- 灵活且吸引人的语法。
- 如果可执行程序返回错误,将抛出异常。
- 自动解析二进制文件的路径。
- 所有参数都得到适当的转义。
- 用于流式输出回调函数。
示例
<?php require_once 'vendor/autoload.php'; use MrRio\ShellWrap as sh; // List all files in current dir echo sh::ls(); // Checkout a branch in git sh::git('checkout', 'master'); // You can also pipe the output of one command, into another // This downloads example.com through cURL, follows location, then pipes through grep to // filter for 'html' echo sh::grep('html', sh::curl('http://example.com', array( 'location' => true ))); // Touch a file to create it sh::touch('file.html'); // Remove file sh::rm('file.html'); // Remove file again (this fails, and throws an exception because the file doesn't exist) try { sh::rm('file.html'); } catch (ShellWrapException $e) { echo 'Caught failing sh::rm() call'; } // This throws an exception, as 'invalidoption' is not a valid argument try { echo sh::ls(array('invalidoption' => true)); } catch (ShellWrapException $e) { echo 'Caught failing sh::ls() call'; } // Commands can be written multiple ways sh::git('reset', array('hard' => true), 'HEAD'); sh::git('reset', '--hard', 'HEAD'); sh::git(array('reset', '--hard', 'HEAD')); // Arguments passed in are automatically escaped, this expands to // date --date '2012-10-10 10:00:00' echo sh::date(array( 'date' => '2012-10-10 10:00:00' )); // If arg keys are one letter, is assumes one dash prefixing it // date -d '2012-10-10 10:00:00' echo sh::date(array( 'd' => '2012-10-10 10:00:00' )); ?>
示例:跟踪文件并添加输出时间戳
您可以将命令的输出流到回调函数中。例如
sh::tail('-f log', function($in) { echo "\033[32m" . date('Y-m-d H:i:s') . "\033[39m " . $in; });
确保文件 'log' 存在。这将输出时间戳和输入。尝试将输出写入日志文件。
转义码用于给终端添加一点颜色。
交互式Shell
ShellWrap还包含交互式Shell模式。您可以通过键入来访问此模式
./bin/shellwrap
警告
不要使用任何用户输入的数据来执行这些命令。即使经过非常偏执的过滤,您也无法了解您使用的每个命令的所有潜在风险。使用您的头脑。
致谢
灵感来自Python项目sh by Andrew Moffat