I'm very happy to say that Tom and I finally launched PHP GitHub Sponsors, a package for PHP to interact with the GitHub Sponsors GraphQL API.
Currently, PHP GitHub Sponsors allows you to do ACL based checks to see if one GitHub account is sponsoring another. While the package is framework agnostic so it can be used in any PHP application, it also comes with integration for the Laravel framework.
Here's an example how you'd use it. First, set up a personal access token in your .env
file:
1GH_SPONSORS_TOKEN=ghp_xxx
This access token should have the user:read
and organization:read
scopes.
Then, you can start using the package:
1use GitHub\Sponsors\Facades\GitHubSponsors; 2 3// Check if driesvints is being sponsored by nunomaduro... 4GitHubSponsors::login('driesvints')->isSponsoredBy('nunomaduro'); 5 6// Check if the blade-ui-kit organization is sponsored by nunomaduro... 7GitHubSponsors::login('nunomaduro')->isSponsoring('blade-ui-kit'); 8 9// Check if the authenticated user is sponsored by Gummibeer...10GitHubSponsors::viewer()->isSponsoredBy('Gummibeer');11 12// Check if the authenticated user is sponsoring driesvints...13GitHubSponsors::viewer()->isSponsoring('driesvints');
You can also retrieve the client from the IoC container and perform calls on it:
1use GitHub\Sponsors\Client; 2use Illuminate\Http\Request; 3use Illuminate\Support\Facades\Route; 4 5Route::get('/home', function (Request $request, Client $sponsors) { 6 $isSponsoring = $sponsors->viewer()->isSponsoredBy( 7 $request->get('github_username') 8 ); 9 10 if ($isSponsoring) {11 return redirect('/secret-homepage');12 }13 14 return view('welcome');15});
Sponsorable
Additionally, you can add the GitHub\Sponsors\Concerns\Sponsorable
trait to an Eloquent model like the User
model to make it sponsorable:
1use GitHub\Sponsors\Concerns\Sponsorable;2use GitHub\Sponsors\Contracts\Sponsorable as SponsorableContract;3use Illuminate\Database\Eloquent\Model;4 5class User extends Model implements SponsorableContract6{7 use Sponsorable;8}
Then, the user will use the value of its github
column as its username. A user can also add their personal access token on the github_token
column to let them check their private sponsorships.
Now you can use the model as follows:
1$user = User::where('github', 'driesvints')->first();2 3// Check if driesvints is being sponsored by nunomaduro...4$user->isSponsoredBy('nunomaduro');5 6// Check if driesvints is sponsoring nunomaduro...7$user->isSponsoring('driesvints');
For more usage and details, check out the documentation.
Roadmap
Initially I wanted to provide much more features in the package. But because I only managed to work on the package sporadically over the past few months I felt that it wasn't coming along fast enough. That's why I decided to release it early with a minimum feature set and continue to work on it in the open.
We still have quite a bit of planned for the package. Here's some of the features on our roadmap:
- Caching
- Retrieve sponsorships
- Check sponsorship tiers
- Create new Sponsorships
- Automatically grant and revoke perks
- Sync sponsorships through GitHub webhooks
- Track sponsored amounts
0 comments:
Post a Comment
Thanks