icyboy / shellwrap
将任何命令行工具用作PHP函数。
0.4.2
2016-05-31 07:04 UTC
Requires
- php: >=5.3.0
- d11wtq/boris: 1.0.*
This package is not auto-updated.
Last update: 2024-09-23 13:31:34 UTC
README
这是什么?
这是一个使用强大的Linux/Unix工具在PHP中的美丽方式。轻松且逻辑地组合命令,捕获错误作为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的启发