Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/DSN/DSN.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class DSN
protected ?string $password;

/**
* @var string
* @var ?string
*/
protected string $host;
protected ?string $host;

/**
* @var ?string
Expand Down Expand Up @@ -55,6 +55,19 @@ public function __construct(string $dsn)
{
$parts = \parse_url($dsn);

// Handle scheme-only DSNs like "local://" which parse_url returns false for
if (!$parts && \str_ends_with($dsn, '://') && \strlen($dsn) > 3) {
$this->scheme = \substr($dsn, 0, -3);
$this->user = null;
$this->password = null;
$this->host = null;
$this->port = null;
$this->path = '';
$this->query = null;

return;
}

if (!$parts) {
throw new \InvalidArgumentException("Unable to parse DSN: $dsn");
}
Expand All @@ -63,14 +76,10 @@ public function __construct(string $dsn)
throw new \InvalidArgumentException('Unable to parse DSN: scheme is required');
}

if (empty($parts['host'])) {
throw new \InvalidArgumentException('Unable to parse DSN: host is required');
}

$this->scheme = $parts['scheme'];
$this->user = isset($parts['user']) ? \urldecode($parts['user']) : null;
$this->password = isset($parts['pass']) ? \urldecode($parts['pass']) : null;
$this->host = $parts['host'];
$this->host = $parts['host'] ?? null;
$this->port = $parts['port'] ?? null;
$this->path = isset($parts['path']) ? ltrim((string) $parts['path'], '/') : '';
$this->query = $parts['query'] ?? null;
Expand Down Expand Up @@ -109,9 +118,9 @@ public function getPassword(): ?string
/**
* Return the host
*
* @return string
* @return ?string
*/
public function getHost(): string
public function getHost(): ?string
{
return $this->host;
}
Expand Down
32 changes: 31 additions & 1 deletion tests/DSN/DSNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,39 @@ public function testGetParam(): void
$this->assertEquals('us-east-2', $dsn->getParam('region', 'us-east-2'));
}

public function testSchemeOnly(): void
{
$dsn = new DSN('local://');
$this->assertEquals('local', $dsn->getScheme());
$this->assertNull($dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertNull($dsn->getHost());
$this->assertNull($dsn->getPort());
$this->assertEmpty($dsn->getPath());
$this->assertNull($dsn->getQuery());

$dsn = new DSN('memory://');
$this->assertEquals('memory', $dsn->getScheme());
$this->assertNull($dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertNull($dsn->getHost());
$this->assertNull($dsn->getPort());
$this->assertEmpty($dsn->getPath());
$this->assertNull($dsn->getQuery());

$dsn = new DSN('file://');
$this->assertEquals('file', $dsn->getScheme());
$this->assertNull($dsn->getUser());
$this->assertNull($dsn->getPassword());
$this->assertNull($dsn->getHost());
$this->assertNull($dsn->getPort());
$this->assertEmpty($dsn->getPath());
$this->assertNull($dsn->getQuery());
}

public function testFail(): void
{
$this->expectException(\InvalidArgumentException::class);
new DSN('mariadb://');
new DSN('invalid-dsn-without-scheme');
}
}