Install the Bundle:
composer require dunglas/digital-ocean-bundle symfony/http-client nyholm/psr7 guzzlehttp/promises
For non-Flex projects, add the bundle to config/bundles.php:
Dunglas\DigitalOceanBundle\DunglasDigitalOceanBundle::class => ['all' => true],
Configure API Token:
Add your DigitalOcean token to config/packages/dunglas_digital_ocean.yaml:
dunglas_digital_ocean:
token: "%env(DIGITAL_OCEAN_TOKEN)%"
Or use the shorthand:
dunglas_digital_ocean: "%env(DIGITAL_OCEAN_TOKEN)%"
First Use Case:
Inject the Client into a controller or service and interact with DigitalOcean resources:
use DigitalOceanV2\Client;
class DropletController {
public function listDroplets(Client $client) {
$droplets = $client->droplet()->getAll();
// Process droplets...
}
}
Use the Client to interact with DigitalOcean resources (droplets, databases, etc.):
// Create a droplet
$droplet = $client->droplet()->create([
'name' => 'app-server',
'region' => 'nyc3',
'size' => 's-1vcpu-1gb',
'image' => 'ubuntu-22-04-x64',
]);
// List droplets with pagination
$droplets = $client->droplet()->getAll();
foreach ($droplets as $droplet) {
// Process each droplet
}
// Delete a droplet
$client->droplet()->delete($dropletId);
Configure multiple connections in config/packages/dunglas_digital_ocean.yaml:
dunglas_digital_ocean:
connections:
primary:
token: "%env(DO_PRIMARY_TOKEN)%"
secondary:
token: "%env(DO_SECONDARY_TOKEN)%"
default_connection: primary
Inject specific clients via autowiring aliases:
use DigitalOceanV2\Client;
class MultiClientService {
public function __construct(
private Client $primaryClient,
private Client $secondaryClient
) {}
}
ResultPagerHandle large datasets efficiently:
use DigitalOceanV2\ResultPager;
$pager = new ResultPager($client);
$allDroplets = $pager->fetchAll($client->droplet(), 'getAll');
Offload long-running tasks (e.g., droplet creation) to Symfony Messenger:
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
public function handleCreateDroplet(CreateDropletMessage $message) {
$client = $this->doClient;
$client->droplet()->create($message->getConfig());
}
Expose DigitalOcean resources as REST endpoints:
# config/api_platform/resources.yaml
resources:
App\Model\Droplet:
collectionOperations:
get: ~
itemOperations:
get: ~
Create a custom Droplet entity and hydrate it with data from the Client.
Store tokens in .env:
DIGITAL_OCEAN_TOKEN=your_token_here
Reference them in config:
dunglas_digital_ocean: "%env(DIGITAL_OCEAN_TOKEN)%"
Wrap API calls in try-catch blocks to handle DigitalOcean API errors:
try {
$droplet = $client->droplet()->create($config);
} catch (\DigitalOceanV2\Exception\ApiException $e) {
// Log or handle the error (e.g., retry or notify)
$this->logger->error('Failed to create droplet: ' . $e->getMessage());
}
Cache frequent API calls (e.g., listing droplets) using Symfony Cache:
use Symfony\Contracts\Cache\CacheInterface;
public function listDroplets(Client $client, CacheInterface $cache) {
$cacheKey = 'droplets_list';
$droplets = $cache->get($cacheKey, function() use ($client) {
return $client->droplet()->getAll();
});
}
Use Symfony’s HttpClient to listen to DigitalOcean webhooks:
use Symfony\Contracts\HttpClient\HttpClientInterface;
public function handleWebhook(HttpClientInterface $client, string $payload) {
$data = json_decode($payload, true);
// Process webhook event (e.g., droplet created/deleted)
}
Mock the Client in tests using PHPUnit:
use DigitalOceanV2\Client;
public function testListDroplets() {
$mockClient = $this->createMock(Client::class);
$mockClient->method('droplet')->willReturnSelf();
$mockClient->method('getAll')->willReturn([/* mock data */]);
$service = new MyService($mockClient);
$result = $service->listDroplets();
$this->assertEquals([/* expected */], $result);
}
429 Too Many Requests.HttpClient:
$client->withOptions([
'timeout' => 30,
'retries' => 3,
'delay' => 1000, // 1 second delay between retries
]);
401 Unauthorized errors gracefully.ResultPager is useful but may not handle all edge cases (e.g., malformed responses). Test with large datasets.$pager = new ResultPager($client);
$allItems = $pager->fetchAll($resource, 'getAll');
if (empty($allItems)) {
throw new \RuntimeException('Failed to fetch all items');
}
DigitalOceanPHP/Client library may evolve. Check for breaking changes in its changelog.composer.json if using an older Symfony version:
"dunglas/digital-ocean-bundle": "^1.1.0"
HttpClient to verify SSL certificates and validate webhook signatures.Configure Monolog to log DigitalOcean API requests/responses:
# config/packages/monolog.yaml
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["
How can I help you explore Laravel packages today?