Laravel GitHub

repository·13.1·Indexed 20 days ago

https://github.com/grahamcampbell/laravel-github

A PHP bridge for the GitHub API designed for Laravel applications. It provides a seamless way to interact with GitHub using Laravel's Manager pattern, Facades, and dependency injection. The package supports multiple authentication methods (application, jwt, none, private, and token), HTTP caching via the illuminate driver, and configurable retry logic with backoff. It is compatible with PHP 8.1-8.5 and Laravel 10-13.

Tokens
2.2K
Snippets
11
Records
11
Agent score
70%

What's inside laravel-github

  1. Manage multiple GitHub connections

    13.1

    You can use the connection method to call specific connections defined in your configuration. If you use the default connection, you can omit the connection() call or use connection() with no arguments.

    use GrahamCampbell\GitHub\Facades\GitHub;
    
    // Using a specific connection named 'alternative'
    GitHub::connection('alternative')->me()->emails()->add('foo@bar.com');
    
    // The following are all equivalent if 'main' is the default connection:
    GitHub::connection('main')->issues()->show('GrahamCampbell', 'Laravel-GitHub', 2);
    GitHub::issues()->show('GrahamCampbell', 'Laravel-GitHub', 2);
    GitHub::connection()->issues()->show('GrahamCampbell', 'Laravel-GitHub', 2);
    
    // You can also change the default connection at runtime
    GitHub::setDefaultConnection('alternative');
  2. Configure Laravel GitHub

    13.1

    To configure connections and caching, publish the vendor assets to create a config/github.php file:

    $ php artisan vendor:publish

    The configuration file contains three main sections:

    1. Default Connection Name ('default'): Specifies which connection to use by default. The default value is 'main'.
    2. GitHub Connections ('connections'): Defines your GitHub connections. Supported authentication methods are: "application", "jwt", "none", "private", and "token".
    3. HTTP Cache ('cache'): Configures caching for your application. The "illuminate" driver is provided out of the box.
  3. Install Laravel GitHub

    13.1

    Install the package using Composer. This version requires PHP 8.1-8.5 and supports Laravel 10-13.

    If you are not using automatic package discovery, register the GrahamCampbell\GitHub\GitHubServiceProvider in your config/app.php. You can also optionally alias the facade:

    $ composer require "graham-campbell/github:^13.1"
    // In config/app.php
    'GitHub' => GrahamCampbell\GitHub\Facades\GitHub::class,
  4. Publish the GitHub configuration file

    13.1

    To customize the package behavior, you can publish the configuration file to your application's config directory. If you are using Laravel, run the following Artisan command:

    php artisan vendor:publish --provider="GrahamCampbell\GitHub\GitHubServiceProvider"

    This will place a github.php file in your config/ directory.

  5. Use the GitHub Facade

    13.1

    The GrahamCampbell\GitHub\Facades\GitHub facade dynamically passes static method calls to the GitHubManager instance in the IoC container. It allows you to interact with the GitHub API using a simple syntax. The underlying connection class returned is always an instance of Github\Client.

    use GrahamCampbell\GitHub\Facades\GitHub;
    
    // Get current user's organizations
    GitHub::me()->organizations();
    
    // Show repository details
    GitHub::repo()->show('GrahamCampbell', 'Laravel-GitHub');
  6. Inject GitHubManager via Dependency Injection

    13.1

    Instead of using facades, you can inject the GrahamCampbell\GitHub\GitHubManager class directly into your classes.

    use GrahamCampbell\GitHub\GitHubManager;
    
    class Foo
    {
        public function __construct(
            private readonly GitHubManager $github,
        ) {
        }
    
        public function bar()
        {
            $this->github->issues()->show('GrahamCampbell', 'Laravel-GitHub', 2);
        }
    }
    
    app(Foo::class)->bar();
  7. Configure HTTP caching

    13.1

    The GitHubFactory supports adding a cache layer to the underlying HTTP client. To enable this, provide an array under the cache key in your configuration.

    The factory uses the provided configuration to create a cache connection via the ConnectionFactory, which is then wrapped in a Psr16Adapter and added to the HTTP builder. The cache lifetime is automatically set based on the maximum lifetime of the provided cache connection.

    $config = [
        'method' => 'token',
        'token' => '...', 
        'cache' => [
            // Configuration options for the ConnectionFactory
        ],
    ];
  8. Configure retry logic with backoff

    13.1

    You can configure automatic retries for HTTP requests by providing a backoff key in the configuration array passed to make().

    • If backoff is set to true, the client will perform 2 retries.
    • If backoff is set to an integer, that integer will be used as the number of retries.

    This utilizes the RetryPlugin to handle request failures.

    // 2 retries (default for true)
    $config = ['method' => 'none', 'backoff' => true];
    
    // 5 retries
    $config = ['method' => 'none', 'backoff' => 5];
  9. Create a GitHub client with make()

    13.1

    The GitHubFactory::make(array $config) method is the primary entry point for creating a configured Github\Client instance. It uses a configuration array to set up the HTTP builder, API version, enterprise settings, and authentication.

    Configuration Keys:

    • method: (Required) The authentication method to use (e.g., token, user_password, or none). If none is provided, no authentication is applied.
    • version: The GitHub API version to use.
    • enterprise: Boolean or configuration to enable GitHub Enterprise support.
    • backoff: Configures retry logic. Can be a boolean true (defaults to 2 retries) or an integer specifying the number of retries.
    • cache: An array configuration used to set up a PSR-16 cache adapter for the HTTP client.

    Throws an InvalidArgumentException if the method key is missing from the configuration.

    // Example of creating a client with token authentication and retries
    $config = [
        'method' => 'token',
        'token' => 'your-github-token',
        'version' => 'v3',
        'backoff' => 3,
    ];
    
    $client = $githubFactory->make($config);
  10. Access the GitHub Client via Service Container bindings

    13.1

    The package registers several key components in the Laravel/Lumen service container. You can resolve these using their class names (via aliases) or their specific string keys.

    Primary Bindings

    ServiceClass AliasContainer Key
    GitHub ManagerGitHubManagergithub
    GitHub ClientClient (from Github\Client)github.connection
    GitHub FactoryGitHubFactorygithub.factory
    HTTP Client FactoryBuilderFactorygithub.httpclientfactory
    Auth FactoryAuthenticatorFactorygithub.authfactory
    Cache FactoryConnectionFactorygithub.cachefactory

    Usage Example

    To get the main GitHub client (the connection) directly from the container:

    $client = app(Client::class);
    // or
    $client = app('github.connection');
    $client = app(Client::class);
  11. Access the GitHub API via the GitHub Facade

    13.1

    The GitHub facade provides a convenient way to access the underlying GitHub API client within your Laravel application. By using this facade, you can call methods on the GitHub client without needing to manually resolve it from the service container.

    To use it, ensure the package is installed and configured, then call the GitHub facade directly in your code.

    use GrahamCampbell//GitHub\
    Facades\GitHub;
    
    // Example usage (actual methods depend on the underlying client)
    $repos = GitHub::repositories();