Laravel DigitalOcean

repository·11.1·Indexed 19 days ago

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

A bridge between the DigitalOcean PHP API Client and the Laravel framework. It enables the management of DigitalOcean resources—such as droplets, regions, and sizes—within Laravel applications using the DigitalOceanManager and DigitalOcean facade. Supports PHP 8.1-8.5 and Laravel 10-13.

Tokens
2.5K
Snippets
10
Records
11
Agent score
68%

What's inside laravel-digitalocean

  1. Manage multiple DigitalOcean connections

    11.1

    You can switch between different configured connections using the connection() method. If you use the default connection, you can omit the connection() call.

    Key methods for managing connections:

    • connection('name'): Selects a specific connection.
    • setDefaultConnection('name'): Changes the default connection for subsequent calls.
    • getDefaultConnection(): Returns the name of the current default connection.
    use GrahamCampbell\DigitalOcean\Facades
    DigitalOcean;
    
    // Using a specific connection
    DigitalOcean::connection('your_connection_name')->droplet()->getById(12345);
    
    // These are equivalent if 'main' is the default:
    DigitalOcean::connection('main')->region()->getAll();
    DigitalOcean::region()->getAll();
    DigitalOcean::connection()->region()->getAll();
    
    // Changing the default connection
    DigitalOcean::setDefaultConnection('alternative');
  2. Install Laravel DigitalOcean via Composer

    11.1

    To install the latest version of Laravel DigitalOcean, use Composer to require the package. This version requires PHP 8.1-8.5 and supports Laravel 10-13.

    If you are not using automatic package discovery, you must manually register the GrahamCampbell\DigitalOcean\DigitalOceanServiceProvider in your config/app.php file. You can also optionally alias the facade:

    $ composer require "graham-campbell/digitalocean:^11.1"
    'DigitalOcean' => GrahamCampbell\DigitalOcean\Facades\DigitalOcean::class,
  3. Configure Laravel DigitalOcean

    11.1

    Laravel DigitalOcean requires connection configuration. To publish the configuration file to config/digitalocean.php, run the following artisan command:

    There are two main configuration options:

    1. default: Specifies which connection from the connections array is used by default (defaults to 'main').
    2. connections: An array defining your DigitalOcean connections. Supported authentication methods are "none" and "token".
    $ php artisan vendor:publish
  4. Inject DigitalOceanManager via Dependency Injection

    11.1

    Instead of using facades, you can inject the DigitalOceanManager directly into your classes via the constructor.

    use GrahamCampbell\DigitalOcean\DigitalOceanManager;
    
    class Foo
    {
        public function __construct(
            private readonly DigitalOceanManager $digitalocean,
        ) {
        }
    
        public function bar()
        {
            $this->digitalocean->region()->getAll();
        }
    }
    
    app(Foo::class)->bar();
  5. Use the DigitalOcean Facade

    11.1

    The DigitalOcean facade provides a simple way to interact with DigitalOcean resources. It dynamically passes calls to the DigitalOceanManager instance in the IoC container. The returned connection objects are instances of DigitalOceanV2\Client.

    Basic usage examples:

    use GrahamCampbell\DigitalOcean\Facades\DigitalOcean;
    
    // Power on a droplet
    DigitalOcean::droplet()->powerOn(12345);
    
    // Get all sizes
    DigitalOcean::size()->getAll();
  6. Use the DigitalOcean facade for easy access

    11.1

    The DigitalOcean facade provides a convenient proxy to the underlying DigitalOcean manager. Instead of resolving the manager from the service container manually, you can call methods directly on the DigitalOcean facade. This facade is bound to the digitalocean service container key.

    use GrahamCampbell\​DigitalOcean\Facades\DigitalOcean;
    
    // Example usage (actual methods depend on the DigitalOcean manager implementation)
    DigitalOcean::someMethod();
  7. Manage DigitalOcean connections via DigitalOceanManager

    11.1

    The DigitalOceanManager class is the primary entry point for interacting with the DigitalOcean API through this package. It allows you to resolve multiple connections (configured in your Laravel application) and provides direct access to various DigitalOcean API resources via magic methods.

    Each resource method (e.g., droplet(), domain(), database()) returns an instance of the corresponding DigitalOcean API class, allowing you to perform operations on that specific resource type using the current connection.

    // Example: Accessing droplets via the manager
    $droplets = app(DigitalOceanManager::class)->droplet()->all();
    
    // Example: Accessing a specific named connection
    $client = app(DigitalOceanManager::class)->connection('secondary');
  8. Access the DigitalOcean Manager and Client via the Service Container

    11.1

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

    Most users will want to interact with the DigitalOceanManager to manage connections or resolve the DigitalOcean\Client directly to interact with the DigitalOcean API.

    // Resolve the Manager to manage multiple connections or configurations
    $manager = app(GrahamCampbell\DigitalOcean\DigitalOceanManager::class);
    
    // Resolve the DigitalOcean Client directly for the default connection
    $client = app(DigitalOceanV2\Client::class);
    
    // Alternatively, using the container strings
    $manager = app('digitalocean');
    $client = app('digitalocean.connection');
  9. Access DigitalOcean API resources

    11.1

    The DigitalOceanManager provides access to the following DigitalOcean API resource types through magic methods. Each method returns the appropriate API class instance for the resource:

    • account(): \DigitalOceanV2\Api\Account
    • action(): \DigitalOceanV2\Api\Action
    • app(): \DigitalOceanV2\Api\App
    • cdnEndpoint(): \DigitalOceanV2\Api\CdnEndpoint
    • certificate(): \DigitalOceanV2\Api\Certificate
    • database(): \DigitalOceanV2\Api\Database
    • domain(): \DigitalOceanV2\Api\Domain
    • domainRecord(): \DigitalOceanV2\Api\DomainRecord
    • droplet(): \DigitalOceanV2\Api\Droplet
    • firewall(): \DigitalOceanV2\Api\Firewall
    • floatingIp(): \DigitalOceanV2\Api\FloatingIp
    • image(): \DigitalOceanV2\Api\Image
    • key(): \DigitalOceanV2\Api\Key
    • loadBalancer(): \DigitalOceanV2\Api\LoadBalancer
    • monitoring(): \DigitalOceanV2\Api\Monitoring
    • projectResource(): \DigitalOceanV2\Api\ProjectResource
    • region(): \DigitalOceanV2\Api\Region
    • reservedIp(): \DigitalOceanV2\Api\ReservedIp
    • size(): \DigitalOceanV2\Api\Size
    • snapshot(): \DigitalOceanV2\Api\Snapshot
    • tag(): \DigitalOceanV2\Api\Tag
    • volume(): \DigitalOceanV2\Api\Volume
    • vpc(): \DigitalOceanV2\Api\Vpc
  10. Manage DigitalOcean client connections

    11.1

    Use the following methods on the DigitalOceanManager to manage the lifecycle and configuration of your DigitalOcean client instances:

    • connection(string|null $name = null): Retrieves a specific connection by name. If $name is null, it returns the default connection. Returns \DigitalOceanV2\Client.
    • reconnect(string|null $name = null): Reconnects a specific connection. Returns \DigitalOceanV2\Client.
    • disconnect(string|null $name = null): Disconnects a specific connection. Returns void.
    • getConnections(): Returns an array of all active connections, keyed by their name. Returns array<string, \DigitalOceanV2\Client>.
    $manager = app(DigitalOceanManager::class);
    
    // Get all connections
    $connections = $manager->getConnections();
    
    // Disconnect a specific connection
    $manager->disconnect('secondary');
  11. Available Service Container Bindings

    11.1

    The following services are registered in the container. You can use the class name (as an alias) or the string identifier to resolve them.

    String Identifier          | Class Alias / Type
    --------------------------|--------------------------------------
    digitalocean.httpclientfactory | GrahamCampbell\DigitalOcean\HttpClient\BuilderFactory
    digitalocean.authfactory       | GrahamCampbell\DigitalOcean\Auth\AuthenticatorFactory
    digitalocean.factory           | GrahamCampbell\DigitalOcean\DigitalOceanFactory
    digitalocean                    | GrahamCampbell\DigitalOcean\DigitalOceanManager
    digitalocean.connection        | DigitalOceanV2\Client