nubs / 空缺
1.2.1
2013-07-16 15:22 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2021-01-12 13:53:23 UTC
README
一个用于执行带有可选超时的shell命令的PHP库。
已弃用:此库不再积极维护。 symfony/process 具有Hiatus的所有功能,并且更多。它被推荐作为替代方案。
要求
此库的唯一要求是PHP 5.4或更高版本。
安装
此软件包使用 composer,因此您只需将 nubs/hiatus
添加为依赖项到您的 composer.json
文件或执行以下命令
composer require nubs/hiatus
使用方法
Composer的自动加载器将自动包括用于在项目中使用的命名空间函数。
以下是一个执行简单命令的示例
<?php // Get the directory listing of the directory given by the user. // NOTE: This is probably not a good idea to let users run arbitrary directory // listings. list($exitStatus, $stdout, $stderr) = \Hiatus\exec('ls -l', [$_POST['dir']]); if ($exitStatus !== 0) { throw new Exception('Command failed.'); } echo $stdout;
包含超时非常简单
<?php // Download the url given by the user, but fail if it takes more than 10 // seconds. // NOTE: This is probably not a good idea to let users download arbitrary urls. list($exitStatus, $stdout, $stderr) = \Hiatus\exec( 'curl', [$_POST['url']], 10 ); if ($exitStatus !== 0) { throw new Exception('Command failed.'); } echo $stdout;
还包括一个生成异常的变体
<?php try { list($stdout, $stderr) = \Hiatus\execX('ls /foo'); } catch (Exception $e) { echo "Error occurred: {$e->getMessage()}\n"; exit(1); } echo $stdout;
both exec
和 execX
都可以提供一个字符串来传递到stdin
<?php list($exitStatus, $stdout, $stderr) = \Hiatus\exec( 'wc -c', [], null, 'stdin test' ); if ((int)$stdout !== 10) { echo "Well, this is awkward.\n"; }
贡献
任何更改、建议或错误报告都可以提交到github。鼓励提交拉取请求!
许可
本项目采用MIT许可证。