graham-campbell/github
Laravel bridge to the KnpLabs PHP GitHub API. Provides a configurable GitHub client via a manager, with Laravel-friendly service container integration, facades, and multi-connection support for GitHub authentication and requests.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require graham-campbell/github:^13.1
Register the service provider in config/app.php (if not using auto-discovery):
'providers' => [
GrahamCampbell\GitHub\GitHubServiceProvider::class,
],
Publish the config:
php artisan vendor:publish --provider="GrahamCampbell\GitHub\GitHubServiceProvider"
Configure GitHub Connection:
Edit config/github.php to define your connection (e.g., main or alternative) with authentication method (token, application, jwt, private, or none). Example:
'connections' => [
'main' => [
'auth' => [
'method' => 'token',
'token' => env('GITHUB_TOKEN'),
],
],
],
First Use Case: Fetch the authenticated user’s organizations:
use GrahamCampbell\GitHub\Facades\GitHub;
$organizations = GitHub::me()->organizations();
Facade-Based Usage:
Leverage the GitHub facade for concise syntax:
// Fetch a repo
$repo = GitHub::repo()->show('owner', 'repo-name');
// List issues
$issues = GitHub::issues()->all('owner', 'repo-name');
Connection Switching: Use named connections dynamically:
// Use 'alternative' connection
$emails = GitHub::connection('alternative')->me()->emails()->all();
Dependency Injection:
Inject GitHubManager into services for type safety:
use GrahamCampbell\GitHub\GitHubManager;
class GitHubService {
public function __construct(private GitHubManager $github) {}
public function fetchIssues() {
return $this->github->issues()->all('owner', 'repo-name');
}
}
Caching Responses:
Enable HTTP caching in config/github.php:
'cache' => [
'enabled' => true,
'driver' => 'illuminate',
'prefix' => 'github_',
],
Cache TTLs are managed automatically (default: 9m59s for JWT tokens).
Environment Variables:
Store sensitive credentials (e.g., GITHUB_TOKEN) in .env:
GITHUB_TOKEN=your_github_token_here
Error Handling: Wrap API calls in try-catch blocks:
try {
$repo = GitHub::repo()->show('owner', 'repo-name');
} catch (\Github\Exception\RuntimeException $e) {
Log::error('GitHub API error: ' . $e->getMessage());
}
Pagination:
Use the all() method for paginated results:
$allIssues = GitHub::issues()->all('owner', 'repo-name');
Webhooks:
Combine with Laravel’s Http client for webhook validation:
$payload = request()->json()->all();
$event = GitHub::webhook()->validate($payload, request()->header('X-Hub-Signature-256'));
Authentication Methods:
none or application for production (use token or private key).Case-Sensitive Filesystems:
Ensure filesystem paths in config/filesystems.php match the case of your actual storage.
Cache Quirks:
config/github.php:
'cache' => [
'ttl' => 3600, // 1 hour
],
illuminate cache driver is officially supported.Rate Limiting:
GitHub enforces rate limits. Handle 403 errors gracefully:
if ($e->getCode() === 403) {
// Retry or notify admin
}
Enable Debugging:
Set GITHUB_DEBUG=true in .env to log API requests/responses.
Inspect Raw Responses:
Use the underlying Github\Client methods:
$response = GitHub::connection()->raw('GET', '/user');
Common Errors:
InvalidArgumentException: Check connection config (e.g., missing token).RuntimeException: Validate API endpoint syntax (e.g., repo()->show() requires owner/repo).Custom Authenticators:
Extend GrahamCampbell\GitHub\Auth\Authenticator for custom OAuth flows.
Middleware:
Add HTTP middleware to the Github\Client via config/github.php:
'connections' => [
'main' => [
'middleware' => [
\App\Http\Middleware\GitHubLogging::class,
],
],
],
Event Listeners: Listen for GitHub webhook events:
GitHub::webhook()->listen('push', function ($payload) {
// Handle push event
});
Testing:
Use GitHub::shouldReceive() in PHPUnit:
GitHub::shouldReceive('repo()->show')
->once()
->andReturn(new \Github\Result\Repository());
---
How can I help you explore Laravel packages today?