我很高兴地说,我和 Tom 最终发布了 PHP GitHub Sponsors,一个用于 PHP 的软件包,用于与 GitHub Sponsors GraphQL API 交互。
目前,PHP GitHub Sponsors 允许您执行基于 ACL 的检查,以查看一个 GitHub 帐户是否赞助了另一个帐户。虽然该软件包是框架无关的,因此它可以在任何 PHP 应用程序中使用,但它也与 Laravel 框架集成。
以下是如何使用它的示例。首先,在您的 .env
文件中 设置个人访问令牌
GH_SPONSORS_TOKEN=ghp_xxx
此访问令牌应具有 user:read
和 organization:read
范围。
然后,您可以开始使用该软件包
use GitHub\Sponsors\Facades\GitHubSponsors; // Check if driesvints is being sponsored by nunomaduro...GitHubSponsors::login('driesvints')->isSponsoredBy('nunomaduro'); // Check if the blade-ui-kit organization is sponsored by nunomaduro...GitHubSponsors::login('nunomaduro')->isSponsoring('blade-ui-kit'); // Check if the authenticated user is sponsored by Gummibeer...GitHubSponsors::viewer()->isSponsoredBy('Gummibeer'); // Check if the authenticated user is sponsoring driesvints...GitHubSponsors::viewer()->isSponsoring('driesvints');
您也可以从 IoC 容器中检索客户端并在其上执行调用
use GitHub\Sponsors\Client;use Illuminate\Http\Request;use Illuminate\Support\Facades\Route; Route::get('/home', function (Request $request, Client $sponsors) { $isSponsoring = $sponsors->viewer()->isSponsoredBy( $request->get('github_username') ); if ($isSponsoring) { return redirect('/secret-homepage'); } return view('welcome');});
可赞助
此外,您可以将 GitHub\Sponsors\Concerns\Sponsorable
特性添加到 Eloquent 模型(如 User
模型)中,使其可赞助
use GitHub\Sponsors\Concerns\Sponsorable;use GitHub\Sponsors\Contracts\Sponsorable as SponsorableContract;use Illuminate\Database\Eloquent\Model; class User extends Model implements SponsorableContract{ use Sponsorable;}
然后,用户将使用其 github
列的值作为其用户名。用户还可以将他们的个人访问令牌添加到 github_token
列中,让他们检查他们的私人赞助关系。
现在您可以按如下方式使用该模型
$user = User::where('github', 'driesvints')->first(); // Check if driesvints is being sponsored by nunomaduro...$user->isSponsoredBy('nunomaduro'); // Check if driesvints is sponsoring nunomaduro...$user->isSponsoring('driesvints');
有关更多用法和详细信息,请 查看文档。
路线图
最初,我想在该软件包中提供更多功能。但由于在过去几个月里我只间歇地开发该软件包,我感觉它进展不够快。这就是为什么我决定尽早发布它,并使用最小的功能集,然后继续公开进行开发。
我们仍然计划为该软件包提供一些功能。以下是我们路线图中的一些功能
结论
如果没有一些很棒的人,这个软件包不可能出现。我要感谢 Claudio Dekker 在开发该软件包期间提供的所有反馈和帮助。我还想感谢 Sarah Vessels 在提供 GitHub GraphQL API 反馈方面提供的帮助。最后,我要感谢 Tom Witkowski 加入我成为共同维护者,并在开发和改进该软件包方面提供帮助。
我希望你会喜欢这个软件包,如果你最终使用了它,请务必 在 Twitter 上告诉我。享受!
Dries 是 Laravel 的开发者,负责维护开源项目。他也是 Laravel.io 的维护者,Laravel 社区门户网站。他与人共同组织 Full Stack Europe 大会以及 Full Stack Belgium 聚会小组。他是 Blade UI Kit 的创建者。