rummykhan / github-reader
Github 仓库阅读器。
0.0.2
2017-10-30 08:59 UTC
Requires
- php: >=5.5.9
- graham-campbell/github: ^6.1
- laravel/framework: 5.2.*
This package is not auto-updated.
Last update: 2024-09-19 05:29:04 UTC
README
此包帮助阅读完整的 Github 仓库并检索您想要的任何文件。此包是围绕 GrahamCampbell/Laravel-GitHub 的包装,但此包专门用于使用 Github 官方 API 读取仓库的适当格式。
Github 官方格式
name: "LICENSE" path: "LICENSE" sha: "c8a38eeec1767ff114eaf7caf5cda6d0a7f8f33d" size: 1110 url: "https://api.github.com/repos/rummykhan/github-reader/contents/LICENSE?ref=master" html_url: "https://github.com/rummykhan/github-reader/blob/master/LICENSE" git_url: "https://api.github.com/repos/rummykhan/github-reader/git/blobs/c8a38eeec1767ff114eaf7caf5cda6d0a7f8f33d" download_url: "https://raw.githubusercontent.com/rummykhan/github-reader/master/LICENSE" type: "file"
只有类型在 directory / symlink 中更改。
安装
使用 composer 安装
composer require rummykhan/github-reader php-http/guzzle6-adapter
添加服务提供者
将 ServiceProvider 添加到 config/app.php 提供者数组中。
\GithubReader\GithubReaderServiceProvider::class,
添加外观
要使用外观,请添加
'GithubReader' => \GithubReader\Facades\GithubReader::class,
发布配置(github.php)
发布配置
php artisan vendor:publish
这将发布 github.php 到 config/ 目录。
更新配置
由于 Github 已经更改了 API 速率限制,您可能会达到每小时速率限制并出现异常。然后您需要 注册 Github 应用 并将凭据添加到 config/github.php。
'app' => [ 'clientId' => 'xxx**************xxx', 'clientSecret' => 'xxx**************xxx', 'method' => 'application', // 'backoff' => false, // 'cache' => false, // 'version' => 'v3', // 'enterprise' => false, ],
读取仓库
读取仓库非常直接。
$repository = app('github-reader')->read('rummykhan', 'github-reader'); dd($repository);
获取文件内容
$repository = app('github-reader')->read('rummykhan', 'github-reader'); $files = $repository->getFiles(); // This method will retrieve file from github $file = $files->first()->retrieve(); dd($file->getContent());
查询文件
由于文件和目录都是 Illuminate\Support\Collection 的实例,因此您可以像查询 Illuminate\Support\Collection 一样查询文件或目录。
您有两种查询文件的方式。
$repository = app('github-reader') ->read('rummykhan', 'github-reader'); $file = $repository->getFiles()->where('name', 'LICENSE')->first(); dd($file);
或者
要仅在文件中查询,请将 InFiles 添加到所有集合方法中。
$repository = app('github-reader') ->read('rummykhan', 'github-reader'); $file = $repository->whereInFiles('name', 'LICENSE')->first(); dd($file);
查询目录
$repository = app('github-reader') ->read('rummykhan', 'github-reader'); $dictionary = $repository->getDirectories()->where('name', 'src')->first(); dd($dictionary);
或者
要仅在目录中查询,请将 InDictionaries 添加到所有集合方法中。
$repository = app('github-reader') ->read('rummykhan', 'github-reader'); $dictionary = $repository->whereInDictionaries('name', 'src')->first(); dd($dictionary);
在仓库中查找文件
由于仓库中每个项的结构(文件/目录)如下。
name: "LICENSE" path: "LICENSE" sha: "c8a38eeec1767ff114eaf7caf5cda6d0a7f8f33d" size: 1110 url: "https://api.github.com/repos/rummykhan/github-reader/contents/LICENSE?ref=master" html_url: "https://github.com/rummykhan/github-reader/blob/master/LICENSE" git_url: "https://api.github.com/repos/rummykhan/github-reader/git/blobs/c8a38eeec1767ff114eaf7caf5cda6d0a7f8f33d" download_url: "https://raw.githubusercontent.com/rummykhan/github-reader/master/LICENSE" type: "file"
我们可以递归地找到所有匹配的目录/文件。
$repository = app('github-reader') ->read('rummykhan', 'github-reader'); $found = $repository->find('type', 'file'); dd($found);
find 的第三个参数用于递归查找
$repository = app('github-reader') ->read('rummykhan', 'github-reader'); $found = $repository->find('name', 'File.php', true); dd($found);
此 find 方法将返回一个集合。
可用方法
1. GithubReader\RepositoryReader
$reader = app('github-reader')
仅用于读取路径。
$directory = app('github-reader') ->setOrganization('rummykhan') ->setRepositoryName('github-reader') ->readPath('src');
2. GithubReader\Github\Directory 或 Repository
$repository = app('github-reader')->read('rummykhan','github-reader');
3. GithubReader\Github\File
$repository = app('github-reader')->read('rummykhan','github-reader'); $file = $repository->getFiles()->first();
4. GithubReader\Github\FileContent
$repository = app('github-reader')->read('rummykhan','github-reader'); $file = $repository->getFiles()->first(); $fileContent = $file->retrieve();