Skip to content

Commit dfa1e86

Browse files
committed
Connection::__construct() now only accepts connector, use Factory::createFromDsn()
1 parent a33203f commit dfa1e86

2 files changed

Lines changed: 88 additions & 17 deletions

File tree

src/Database/Connection.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database;
1111

1212
use JetBrains\PhpStorm\Language;
13+
use Nette;
1314
use Nette\Utils\Arrays;
1415

1516

@@ -35,18 +36,20 @@ class Connection
3536

3637

3738
public function __construct(
38-
private readonly string $dsn,
39-
?string $user = null,
40-
#[\SensitiveParameter]
41-
?string $password = null,
42-
array $options = [],
39+
\Closure|string $connector,
4340
) {
44-
$lazy = $options['lazy'] ?? false;
45-
unset($options['newDateTime'], $options['lazy']);
41+
if (is_string($connector)) { // compatibility with version 3.x
42+
//trigger_error('Passing DSN as string is deprecated, use Nette\Database\Factory::createFromDsn() instead.', E_USER_DEPRECATED);
43+
[$dsn, $user, $password, $options] = func_get_args() + [null, null, null, []];
44+
$lazy = $options['lazy'] ?? false;
45+
unset($options['newDateTime'], $options['lazy']);
46+
$this->connector = (new Factory)->createConnectorFromDsn($dsn, $user, $password, $options);
47+
if (!$lazy) {
48+
$this->connect();
49+
}
4650

47-
$this->connector = (new Factory)->createConnectorFromDsn($dsn, $user, $password, $options);
48-
if (!$lazy) {
49-
$this->connect();
51+
} else {
52+
$this->connector = $connector;
5053
}
5154
}
5255

@@ -73,9 +76,11 @@ public function disconnect(): void
7376
}
7477

7578

79+
/** @deprecated */
7680
public function getDsn(): string
7781
{
78-
return $this->dsn;
82+
return ''; // TODO
83+
throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated.');
7984
}
8085

8186

src/Database/Factory.php

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,94 @@ final class Factory
2323
];
2424

2525

26-
/** @internal */
27-
public function createConnectorFromDsn(
26+
public function __construct(
27+
private bool $lazy = true,
28+
) {
29+
}
30+
31+
32+
public function createFromParameters(
33+
#[\SensitiveParameter]
34+
...$params,
35+
): Connection
36+
{
37+
$params = count($params) === 1 && is_array($params[0] ?? null) ? $params[0] : $params;
38+
$factory = $this->createConnectorFromParameters($params);
39+
return $this->createFromConnector($factory);
40+
}
41+
42+
43+
public function createFromDsn(
2844
string $dsn,
2945
?string $username = null,
3046
#[\SensitiveParameter]
3147
?string $password = null,
3248
array $options = [],
49+
): Connection
50+
{
51+
$factory = $this->createConnectorFromDsn($dsn, $username, $password, $options);
52+
return $this->createFromConnector($factory);
53+
}
54+
55+
56+
/**
57+
* @param \Closure(): Drivers\Connection $connector
58+
*/
59+
public function createFromConnector(\Closure $connector): Connection
60+
{
61+
$connection = new Connection($connector);
62+
if (!$this->lazy) {
63+
$connection->connect();
64+
}
65+
return $connection;
66+
67+
}
68+
69+
70+
/** @internal */
71+
public function createConnectorFromParameters(
72+
#[\SensitiveParameter]
73+
array $params,
3374
): \Closure
3475
{
3576
if ($class = $params['driverClass'] ?? null) {
3677
if (!is_subclass_of($class, Drivers\Connection::class)) {
3778
throw new \LogicException("Driver class '$class' is not subclass of " . Drivers\Connection::class);
3879
}
80+
unset($params['driverClass']);
3981

40-
} else {
41-
$driver = explode(':', $dsn)[0];
42-
$class = self::Drivers['pdo-' . $driver] ?? null;
82+
} elseif ($driver = $params['driver'] ?? null) {
83+
$class = self::Drivers[$driver] ?? null;
4384
if (!$class) {
44-
throw new \LogicException("Unknown PDO driver '$driver'.");
85+
throw new \LogicException("Unknown driver '$driver'.");
4586
}
87+
unset($params['driver']);
88+
89+
} elseif ($params['dsn'] ?? null) {
90+
return $this->createConnectorFromDsn(...$params);
91+
92+
} else {
93+
throw new \LogicException("Missing options 'driver' or 'driverClass'.");
4694
}
4795

96+
return fn() => new $class(...$params);
97+
}
98+
99+
100+
/** @internal */
101+
public function createConnectorFromDsn(
102+
string $dsn,
103+
?string $username,
104+
#[\SensitiveParameter]
105+
?string $password,
106+
array $options = [],
107+
): \Closure
108+
{
109+
$driver = explode(':', $dsn)[0];
110+
$class = self::Drivers['pdo-' . $driver] ?? null;
111+
if (!$class) {
112+
throw new \LogicException("Unknown PDO driver '$driver'.");
113+
}
48114
return fn() => new $class($dsn, $username, $password, $options);
49115
}
50116
}

0 commit comments

Comments
 (0)