Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@

final readonly class ConnectionParameters
{
/**
* @param array<string, string> $options
*/
private function __construct(
public string $connectionString,
private string $host,
private int $port,
private string $database,
private ?string $user,
#[\SensitiveParameter]
private ?string $password,
private array $options,
) {
}

Expand All @@ -25,44 +34,212 @@ public static function fromParams(
?string $password = null,
array $options = [],
) : self {
$parts = [
\sprintf('host=%s', $host),
\sprintf('port=%d', $port),
\sprintf('dbname=%s', $database),
];
return new self(
host: $host,
port: $port,
database: $database,
user: $user,
password: $password,
options: $options,
);
}

if ($user !== null) {
$parts[] = \sprintf('user=%s', $user);
}
/**
* Create from a PostgreSQL libpq connection string.
*
* Parses connection strings in libpq format: key=value pairs separated by spaces.
* Supports quoted values for values containing spaces (e.g., password='my secret').
*
* @throws \InvalidArgumentException if dbname is missing
*/
public static function fromString(#[\SensitiveParameter] string $connectionString) : self
{
$parts = [];
$pattern = '/(\w+)=(?:\'([^\']*)\'|([^\s]*))/';
\preg_match_all($pattern, $connectionString, $matches, \PREG_SET_ORDER);

if ($password !== null) {
$parts[] = \sprintf('password=%s', $password);
foreach ($matches as $match) {
$key = $match[1];
$quotedValue = $match[2] ?? '';
$unquotedValue = $match[3] ?? '';
$value = $quotedValue !== '' ? $quotedValue : $unquotedValue;
$parts[$key] = $value;
}

foreach ($options as $key => $value) {
$parts[] = \sprintf('%s=%s', $key, $value);
if (!isset($parts['dbname'])) {
throw new \InvalidArgumentException('Missing dbname in connection string');
}

return new self(\implode(' ', $parts));
return new self(
host: $parts['host'] ?? 'localhost',
port: isset($parts['port']) ? (int) $parts['port'] : 5432,
database: $parts['dbname'],
user: $parts['user'] ?? null,
password: $parts['password'] ?? null,
options: \array_diff_key($parts, \array_flip(['host', 'port', 'dbname', 'user', 'password'])),
);
}

/**
* Create from a PostgreSQL connection string.
* Mask password in debug output to prevent accidental exposure.
*
* @return array<string, mixed>
*/
public static function fromString(#[\SensitiveParameter] string $connectionString) : self
public function __debugInfo() : array
{
return [
'host' => $this->host,
'port' => $this->port,
'database' => $this->database,
'user' => $this->user,
'password' => $this->password !== null ? '***' : null,
'options' => $this->options,
];
}

public function database() : string
{
return $this->database;
}

public function host() : string
{
return new self($connectionString);
return $this->host;
}

/**
* Mask password in debug output to prevent accidental exposure.
*
* @return array<string, string>
*/
public function __debugInfo() : array
public function options() : array
{
return [
'connectionString' => \preg_replace('/password=[^\s]+/', 'password=***', $this->connectionString) ?? $this->connectionString,
return $this->options;
}

public function password() : ?string
{
return $this->password;
}

public function port() : int
{
return $this->port;
}

/**
* Convert connection parameters to a libpq connection string.
*/
public function toString() : string
{
$parts = [
\sprintf('host=%s', $this->host),
\sprintf('port=%d', $this->port),
\sprintf('dbname=%s', $this->database),
];

if ($this->user !== null) {
$parts[] = \sprintf('user=%s', $this->user);
}

if ($this->password !== null) {
$parts[] = \sprintf('password=%s', $this->password);
}

foreach ($this->options as $key => $value) {
$parts[] = \sprintf('%s=%s', $key, $value);
}

return \implode(' ', $parts);
}

public function user() : ?string
{
return $this->user;
}

public function withDatabase(string $database) : self
{
return new self(
host: $this->host,
port: $this->port,
database: $database,
user: $this->user,
password: $this->password,
options: $this->options,
);
}

public function withHost(string $host) : self
{
return new self(
host: $host,
port: $this->port,
database: $this->database,
user: $this->user,
password: $this->password,
options: $this->options,
);
}

public function withOption(string $key, string $value) : self
{
return new self(
host: $this->host,
port: $this->port,
database: $this->database,
user: $this->user,
password: $this->password,
options: \array_merge($this->options, [$key => $value]),
);
}

/**
* @param array<string, string> $options
*/
public function withOptions(array $options) : self
{
return new self(
host: $this->host,
port: $this->port,
database: $this->database,
user: $this->user,
password: $this->password,
options: $options,
);
}

public function withPassword(#[\SensitiveParameter] ?string $password) : self
{
return new self(
host: $this->host,
port: $this->port,
database: $this->database,
user: $this->user,
password: $password,
options: $this->options,
);
}

public function withPort(int $port) : self
{
return new self(
host: $this->host,
port: $port,
database: $this->database,
user: $this->user,
password: $this->password,
options: $this->options,
);
}

public function withUser(?string $user) : self
{
return new self(
host: $this->host,
port: $this->port,
database: $this->database,
user: $user,
password: $this->password,
options: $this->options,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function connect(
}

\error_clear_last();
$connection = @\pg_connect($params->connectionString);
$connection = @\pg_connect($params->toString());

if ($connection === false) {
$error = \error_get_last();
Expand Down
Loading
Loading