Skip to content

Commit 07f05ab

Browse files
committed
added Factory::createFromParameters() & createFromDsn() [WIP]
merging Connection & Explorer classes into one (BC break) [WIP]
1 parent 04a0477 commit 07f05ab

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

Diff for: src/Bridges/DatabaseDI/DatabaseExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function setupDatabase(\stdClass $config, string $name): void
9999
$cache = new Statement(Nette\Caching\Cache::class, [1 => $cacheId]);
100100

101101
$explorer = $builder->addDefinition($this->prefix($name))
102-
->setFactory(Nette\Database\Explorer::class, [$config->dsn, $config->user, $config->password, $config->options])
102+
->setFactory([Nette\Database\Factory::class, 'createFromDsn'], [$config->dsn, $config->user, $config->password, $config->options])
103103
->addSetup('setCache', [$cache])
104104
->setAutowired($config->autowired);
105105

Diff for: src/Database/Explorer.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public function __construct(
4343
if (is_string($driver)) { // compatibility with version 3.x
4444
[$dsn, $user, $password, $options] = func_get_args() + [null, null, null, []];
4545
unset($options['lazy']);
46-
Factory::configure($this, $options);
47-
$driver = Factory::createDriverFromDsn($dsn, $user, $password, $options);
46+
$explorer = Factory::createFromDsn($dsn, $user, $password, $options);
47+
$driver = $explorer->driver;
48+
$this->typeConverter = $explorer->typeConverter;
4849
}
4950

5051
$this->driver = $driver;

Diff for: src/Database/Factory.php

+59-12
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,88 @@ final class Factory
2828
private const TypeConverterOptions = ['convertBoolean', 'convertDateTime', 'convertDecimal', 'newDateTime'];
2929

3030

31-
/** @internal */
32-
public static function createDriverFromDsn(
31+
public static function createFromParameters(
32+
#[\SensitiveParameter]
33+
...$params,
34+
): Explorer
35+
{
36+
$params = count($params) === 1 && is_array($params[0] ?? null) ? $params[0] : $params;
37+
$driver = self::createDriverFromParameters($params);
38+
return self::createExplorer($driver, $params);
39+
}
40+
41+
42+
public static function createFromDsn(
3343
string $dsn,
3444
?string $username = null,
3545
#[\SensitiveParameter]
3646
?string $password = null,
3747
array $options = [],
48+
): Explorer
49+
{
50+
$driver = self::createDriverFromDsn($dsn, $username, $password, $options);
51+
return self::createExplorer($driver, $options);
52+
}
53+
54+
55+
private static function createDriverFromParameters(
56+
#[\SensitiveParameter]
57+
array $params,
3858
): Drivers\Driver
3959
{
4060
if ($class = $params['driverClass'] ?? null) {
4161
if (!is_subclass_of($class, Drivers\Driver::class)) {
4262
throw new \LogicException("Driver class '$class' is not subclass of " . Drivers\Driver::class);
4363
}
64+
unset($params['driverClass']);
4465

45-
} else {
46-
$driver = explode(':', $dsn)[0];
47-
$class = self::Drivers['pdo-' . $driver] ?? null;
66+
} elseif ($driver = $params['driver'] ?? null) {
67+
$class = self::Drivers[$driver] ?? null;
4868
if (!$class) {
49-
throw new \LogicException("Unknown PDO driver '$driver'.");
69+
throw new \LogicException("Unknown driver '$driver'.");
5070
}
71+
unset($params['driver']);
72+
73+
} elseif ($params['dsn'] ?? null) {
74+
return self::createDriverFromDsn(...$params);
75+
76+
} else {
77+
throw new \LogicException("Missing options 'driver' or 'driverClass'.");
5178
}
5279

80+
$params = array_diff_key($params, array_flip(self::TypeConverterOptions));
81+
return new $class($params);
82+
}
83+
84+
85+
private static function createDriverFromDsn(
86+
string $dsn,
87+
?string $username,
88+
#[\SensitiveParameter]
89+
?string $password,
90+
array $options = [],
91+
): Drivers\Driver
92+
{
93+
$options = array_diff_key($options, array_flip(self::TypeConverterOptions));
94+
$driver = explode(':', $dsn)[0];
95+
$class = self::Drivers['pdo-' . $driver] ?? null;
96+
if (!$class) {
97+
throw new \LogicException("Unknown PDO driver '$driver'.");
98+
}
5399
return new $class(['dsn' => $dsn, 'username' => $username, 'password' => $password, 'options' => $options]);
54100
}
55101

56102

57-
/** @internal */
58-
public static function configure(Explorer $explorer, array $options): void
103+
private static function createExplorer(Drivers\Driver $driver, array $options): Explorer
59104
{
105+
$explorer = new Explorer($driver);
60106
$converter = $explorer->getTypeConverter();
61-
foreach (self::TypeConverterOptions as $opt) {
62-
if (isset($options[$opt])) {
63-
$converter->$opt = (bool) $options[$opt];
64-
unset($options[$opt]);
107+
foreach (self::TypeConverterOptions as $key) {
108+
if (isset($options[$key])) {
109+
$converter->$key = (bool) $options[$key];
110+
unset($options[$key]);
65111
}
66112
}
113+
return $explorer;
67114
}
68115
}

0 commit comments

Comments
 (0)