enqueue/dsn
Parse and work with Enqueue DSNs (connection strings) for message queue transports. Lightweight PHP utility to read DSN options and parameters, used across the Enqueue ecosystem. MIT licensed.
Installation
composer require enqueue/dsn
Add to composer.json if not using autoloading:
"autoload": {
"psr-4": {
"App\\": "app/",
"Enqueue\\Dsn\\": "vendor/enqueue/dsn/"
}
}
Run composer dump-autoload.
First Use Case
Parse a DSN string (e.g., amqp://guest:guest@localhost:5672/%2f):
use Enqueue\Dsn\AmqpDsn;
use Enqueue\Dsn\Dsn;
$dsn = new Dsn('amqp://guest:guest@localhost:5672/%2f');
$parsed = $dsn->parse(); // Returns an array of parsed components
Where to Look First
src/Enqueue/Dsn/ for core classes (AmqpDsn, Dsn, SqsDsn, etc.).tests/ for edge cases and validation.Parsing DSNs
$dsn = new Dsn('redis://user:pass@host:6379/0');
$parsed = $dsn->parse();
// Returns: ['scheme' => 'redis', 'user' => 'user', 'pass' => 'pass', ...]
Protocol-Specific Parsing Use protocol-specific classes for stricter validation:
$amqpDsn = new AmqpDsn('amqp://guest:guest@localhost:5672/%2f');
$parsed = $amqpDsn->parse(); // Validates AMQP-specific rules (e.g., vhost)
Integration with Enqueue Clients
Combine with php-enqueue/amqp-ext or php-enqueue/sqs for broker connections:
use Enqueue\AmqpExt\AmqpConnectionFactory;
use Enqueue\Client\Producer;
$dsn = new Dsn('amqp://guest:guest@localhost:5672/%2f');
$connectionFactory = new AmqpConnectionFactory($dsn->parse());
$producer = new Producer($connectionFactory->createConnection());
Dynamic DSN Handling
Parse DSNs from config (e.g., .env):
$dsnString = env('QUEUE_CONNECTION_DSN');
$dsn = new Dsn($dsnString);
$config = $dsn->parse(); // Use in Laravel's queue config
Validation and Error Handling Catch parsing errors gracefully:
try {
$dsn = new Dsn('invalid-dsn');
$parsed = $dsn->parse();
} catch (\InvalidArgumentException $e) {
Log::error("Invalid DSN: " . $e->getMessage());
}
Laravel Queue Configuration
Replace hardcoded DSNs in config/queue.php with dynamic parsing:
$dsn = new Dsn(env('QUEUE_CONNECTION_DSN'));
$config['connections']['amqp'] = [
'driver' => 'amqp',
'dsn' => $dsn->parse(),
];
Environment-Specific DSNs
Use different DSNs per environment (e.g., local, staging, production):
$env = env('APP_ENV');
$dsnString = config("queue.dsn.{$env}");
$dsn = new Dsn($dsnString);
Testing Mock DSN parsing in unit tests:
$mockDsn = $this->createMock(Dsn::class);
$mockDsn->method('parse')->willReturn(['scheme' => 'amqp', 'host' => 'localhost']);
CLI Tools Build CLI tools to validate DSNs before deployment:
php artisan queue:validate-dsn --dsn="amqp://user:pass@host:5672/vhost"
Leverage Laravel Service Providers Bind the DSN parser in a service provider:
$this->app->singleton(Dsn::class, function ($app) {
return new Dsn(env('QUEUE_CONNECTION_DSN'));
});
Extend for Custom Protocols
Create a custom DSN class by extending Enqueue\Dsn\Dsn:
class CustomDsn extends Dsn {
protected function parseSchemeSpecificPart($schemeSpecificPart) {
// Custom parsing logic
}
}
Combine with Laravel Queues Use parsed DSNs to configure Laravel's queue drivers dynamically:
$parsed = (new Dsn(env('QUEUE_CONNECTION_DSN')))->parse();
Queue::extend('custom', function () use ($parsed) {
return new CustomQueueDriver($parsed);
});
Logging and Monitoring Log parsed DSNs for debugging:
Log::debug('Parsed DSN', $dsn->parse());
Deprecated Protocol Support
URL Encoding Issues
%2f for /) may not parse correctly in all cases.rawurldecode():
$dsnString = rawurldecode($dsnString);
No Built-in Connection Testing
Enqueue\AmqpExt\AmqpConnectionFactory or php-amqplib to test connections post-parsing.Limited Error Messages
try {
$parsed = $dsn->parse();
} catch (\Exception $e) {
Log::error("DSN: {$dsn->getDsn()}, Error: " . $e->getMessage());
}
No Support for TLS/SSL Options
?tls or ?ssl query parameters natively.$query = parse_url($dsn->getDsn(), PHP_URL_QUERY);
parse_str($query, $queryParams);
if (isset($queryParams['tls'])) {
$parsed['tls'] = filter_var($queryParams['tls'], FILTER_VALIDATE_BOOLEAN);
}
Validate DSN Structure
Use parse_url() to inspect the DSN before parsing:
$parts = parse_url('amqp://user:pass@host:5672/vhost?heartbeat=30');
// Debug each part: $parts['scheme'], $parts['host'], etc.
Check for Typos Common typos in DSNs:
:// (e.g., amqp:user@host → invalid).host:5672 vs. host:5672/).if (!preg_match('/^[a-z]+:\/\/.+$/', $dsnString)) {
throw new \InvalidArgumentException("Invalid DSN format");
}
Protocol-Specific Quirks
%2f for /).redis://host defaults to 6379).sqs.us-east-1.amazonaws.com).user, pass, port).
$parsed = $dsn->parse();
$parsed['port'] = $parsed['port'] ?? (($parsed['scheme'] === 'redis') ?
How can I help you explore Laravel packages today?