b2pweb/bdf-dsn
Simple PHP DSN parser for database connection strings. Parse URL-style (mysql://user:pass@host/db?timeout=3) and PDO DSN format (mysql:host=...;dbname=...;timeout=...) into an easy-to-use object/array.
parseDSN() by offering:
mysql://user:pass@host and pdo_mysql:host=host;dbname=db formats for consistency.pdo_mysql:host=...) and URL-style DSNs (mysql://user:pass@host).sqlite:, sqlsrv:) and advanced options (e.g., SSL, Unix sockets). Laravel’s native parser handles these natively.Connection configuration.parseDSN()).pgsql:, oci:). Test thoroughly for your use case.null or incomplete data for malformed DSNs. Requires wrapper logic for robust error handling.parseDSN() is sufficient for most use cases. Justify this package only for pre-validation or custom logic.parseDSN() fail in scenarios this package addresses?charset, unix_socket, sslmode) unsupported by this package?sslmode=require)?try-catch?parseDSN()?parseDSN() (built-in, no dependencies).dsn-parser (more mature, supports more drivers).php-dsn (alternative with broader driver support).pass:word).mysql://@localhost, mysql://user:@localhost).mysql://user%3Apass@host).config/database.mysql:// strings to Laravel’s pdo_mysql: format).FormRequest or middleware).config/database.php or Laravel’s parseDSN()).Phase 1: Proof of Concept (1-2 Days)
composer require b2pweb/bdf-dsn.parseDSN():
// Laravel's native parser
$laravelParsed = \Illuminate\Database\Connectors\ConnectionFactory::parseDSN('mysql://user:pass@host/db');
// This package
$thisPackageParsed = \Bdf\Dsn\Dsn::parse('mysql://user:pass@host/db')->toArray();
Phase 2: Wrapper Layer (2-3 Days)
app/Services/DsnParserService.php):
namespace App\Services;
use Bdf\Dsn\Dsn;
use Illuminate\Support\Facades\Log;
class DsnParserService
{
public function parse(string $dsn): array
{
try {
$parsed = Dsn::parse($dsn)->toArray();
// Add custom validation (e.g., required fields, driver support)
$this->validateParsedDsn($parsed);
return $parsed;
} catch (\Exception $e) {
Log::error("DSN parsing failed: {$e->getMessage()}");
// Fall back to Laravel's parser or throw custom exception
return \Illuminate\Database\Connectors\ConnectionFactory::parseDSN($dsn);
}
}
protected function validateParsedDsn(array $dsn): void
{
// Example: Ensure 'host' exists and is non-empty
if (empty($dsn['host'])) {
throw new \InvalidArgumentException('DSN must include a host.');
}
}
}
AppServiceProvider:
public function register()
{
$this->app->singleton(DsnParserService::class);
}
Phase 3: Integration (3-5 Days)
How can I help you explore Laravel packages today?