net_bazzline / php_component_command
作为自由软件的 php_component_command 是一个轻量级的通用包装器,旨在简化使用系统(shell)命令的操作
2.0.0
2018-01-13 19:29 UTC
Requires
- php: ~7.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~6.0
README
这个自由软件项目旨在提供一个易于使用的 php 命令组件。
当前 master 分支的构建状态由 Travis CI 跟踪:
请访问 openhub.net。
当前更改日志可在 此处 找到。
使用方法
您可以在 此处 找到许多示例。
class Zip extends Command { /** * @param string $archiveName * @param array $items * @return array * @throws RuntimeException */ public function __invoke($archiveName, array $items) { return $this->zip($archiveName, $items); } /** * @param string $archiveName * @param array $items * @return array * @throws RuntimeException * @todo implement parameter validation */ public function zip($archiveName, array $items) { $command = '/usr/bin/zip -r ' . $archiveName . ' ' . implode(' ' , $items); return $this->execute($command); } /** * @param string $pathToArchive * @param null|string $outputPath * @return array * @throws RuntimeException * @todo implement parameter validation */ public function unzip($pathToArchive, $outputPath = null) { if (!is_null($outputPath)) { $command = '/usr/bin/unzip ' . $pathToArchive . ' -d ' . $outputPath; } else { $command = '/usr/bin/unzip ' . $pathToArchive; } return $this->execute($command); } /** * @param string $pathToArchive * @return array * @throws RuntimeException * @todo implement parameter validation */ public function listContent($pathToArchive) { $command = '/usr/bin/unzip -l ' . $pathToArchive; return $this->execute($command); } /** * @throws InvalidSystemEnvironmentException */ public function validateSystemEnvironment() { if (!is_executable('/usr/bin/zip')) { throw new InvalidSystemEnvironmentException( '/usr/bin/zip is mandatory' ); } if (!is_executable('/usr/bin/unzip')) { throw new InvalidSystemEnvironmentException( '/usr/bin/unzip is mandatory' ); } } } $zip = new Zip(); $pathToZipArchive = '/tmp/my.zip'; $zip->validateSystemEnvironment(); echo 'list archive content' . PHP_EOL; $lines = $zip->listContent($pathToZipArchive); foreach ($lines as $line) { echo $line . PHP_EOL; } echo 'unzip archive' . PHP_EOL; $zip->unzip($pathToZipArchive, '/tmp/my_directory'); echo 'zip directory' . PHP_EOL; //also valid call since we implemented __invoke //$zip($pathToZipArchive, array('/tmp/my_directory')); $zip->zip($pathToZipArchive, array('/tmp/my_directory'));
安装
手动
mkdir -p vendor/net_bazzline/php_component_command
cd vendor/net_bazzline/php_component_command
git clone https://github.com/bazzline/php_component_command .
使用 Composer
composer require net_bazzline/php_component_command:dev-master
优势
- 简单且健壮地处理系统命令的方式
- 使用异常进行返回值验证
- 希望是系统命令之间最薄的一层
API
API 可在 bazzline.net 上找到。
结束语
如果喜欢它,请给它加星标 :-). 如果需要,请添加问题。如果喜欢,请拉取补丁。如果您使用了它,请写一篇博客文章 :-D。