Support the ongoing development of Laravel.io →
Article Hero Image

Launching PHP GitHub Sponsors

28 Aug, 2021 3 min read

Photo by Alexander Mils on Unsplash

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:

GH_SPONSORS_TOKEN=ghp_xxx

This access token should have the user:read and organization:read scopes.

Then, you can start using the package:

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');

You can also retrieve the client from the IoC container and perform calls on it:

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');
});

Sponsorable

Additionally, you can add the GitHub\Sponsors\Concerns\Sponsorable trait to an Eloquent model like the User model to make it sponsorable:

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;
}

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:

$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');

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:

Conclusion

This package couldn't have happened without some awesome people. I'd really like to thank Claudio Dekker for all his feedback and help while developing the package. I'd also like to thank Sarah Vessels for all her help with providing feedback on the GitHub GraphQL API. And lastly I want to thank Tom Witkowski for joining me as a co-maintainer and helping out with developing and improving the package.

I hope you'll like this one and if you do end up using the package definitely let me know on Twitter. Enjoy!

Last updated 3 weeks ago.

driesvints, joedixon, alexanderfalkenberg liked this article

3
Like this article? Let the author know and give them a clap!
driesvints (Dries Vints) I work for Laravel and maintain Laravel.io. Creator of Eventy.

Other articles you might like

Article Hero Image November 18th 2024

Laravel Custom Query Builders Over Scopes

Hello 👋 Alright, let's talk about Query Scopes. They're awesome, they make queries much easier to r...

Read article
Article Hero Image November 19th 2024

Access Laravel before and after running Pest tests

How to access the Laravel ecosystem by simulating the beforeAll and afterAll methods in a Pest test....

Read article
Article Hero Image November 11th 2024

🍣 Sushi — Your Eloquent model driver for other data sources

In Laravel projects, we usually store data in databases, create tables, and run migrations. But not...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.