Skip to content

Commit

Permalink
added Factory::createFromParameters() & createFromDsn() [WIP]
Browse files Browse the repository at this point in the history
merging Connection & Explorer classes into one (BC break) [WIP]
  • Loading branch information
dg committed Aug 29, 2024
1 parent 0084e73 commit 6ca4231
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
9 changes: 8 additions & 1 deletion src/Bridges/DatabaseDI/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,18 @@ private function setupDatabase(\stdClass $config, string $name): void
$cacheId = 'Nette.Database.' . hash('xxh128', $name . $config->dsn);
$cache = new Statement(Nette\Caching\Cache::class, [1 => $cacheId]);

$lazy = $config->options['lazy'] ?? null;
unset($config->options['lazy']);

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

if ($lazy === false) {
$explorer->addSetup('connect');
}

if (!$config->conventions || $config->conventions === 'discovered') {

} elseif (is_string($config->conventions)) {
Expand Down
6 changes: 4 additions & 2 deletions src/Database/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public function __construct(
$lazy = $options['lazy'] ?? false;
unset($options['lazy']);

Factory::configure($this, $options);
$this->driver = Factory::createDriverFromDsn($dsn, $user, $password, $options);
$explorer = Factory::createFromDsn($dsn, $user, $password, $options);
$this->driver = $explorer->driver;
$this->typeConverter = $explorer->typeConverter;

if (!$lazy) {
$this->connect();
}
Expand Down
71 changes: 59 additions & 12 deletions src/Database/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,88 @@ final class Factory
private const TypeConverterOptions = ['convertBoolean', 'convertDateTime', 'convertDecimal', 'newDateTime'];


/** @internal */
public static function createDriverFromDsn(
public static function createFromParameters(
#[\SensitiveParameter]
...$params,
): Explorer
{
$params = count($params) === 1 && is_array($params[0] ?? null) ? $params[0] : $params;
$driver = self::createDriverFromParameters($params);
return self::createExplorer($driver, $params);
}


public static function createFromDsn(
string $dsn,
?string $username = null,
#[\SensitiveParameter]
?string $password = null,
array $options = [],
): Explorer
{
$driver = self::createDriverFromDsn($dsn, $username, $password, $options);
return self::createExplorer($driver, $options);
}


private static function createDriverFromParameters(
#[\SensitiveParameter]
array $params,
): Drivers\Driver
{
if ($class = $params['driverClass'] ?? null) {
if (!is_subclass_of($class, Drivers\Driver::class)) {
throw new \LogicException("Driver class '$class' is not subclass of " . Drivers\Driver::class);
}
unset($params['driverClass']);

} else {
$driver = explode(':', $dsn)[0];
$class = self::Drivers['pdo-' . $driver] ?? null;
} elseif ($driver = $params['driver'] ?? null) {
$class = self::Drivers[$driver] ?? null;
if (!$class) {
throw new \LogicException("Unknown PDO driver '$driver'.");
throw new \LogicException("Unknown driver '$driver'.");
}
unset($params['driver']);

} elseif ($params['dsn'] ?? null) {
return self::createDriverFromDsn(...$params);

} else {
throw new \LogicException("Missing options 'driver' or 'driverClass'.");
}

$params = array_diff_key($params, array_flip(self::TypeConverterOptions));
return new $class($params);
}


private static function createDriverFromDsn(
string $dsn,
?string $username,
#[\SensitiveParameter]
?string $password,
array $options = [],
): Drivers\Driver
{
$options = array_diff_key($options, array_flip(self::TypeConverterOptions));
$driver = explode(':', $dsn)[0];
$class = self::Drivers['pdo-' . $driver] ?? null;
if (!$class) {
throw new \LogicException("Unknown PDO driver '$driver'.");
}
return new $class(['dsn' => $dsn, 'username' => $username, 'password' => $password, 'options' => $options]);
}


/** @internal */
public static function configure(Explorer $explorer, array $options): void
private static function createExplorer(Drivers\Driver $driver, array $options): Explorer
{
$explorer = new Explorer($driver);
$converter = $explorer->getTypeConverter();
foreach (self::TypeConverterOptions as $opt) {
if (isset($options[$opt])) {
$converter->$opt = (bool) $options[$opt];
unset($options[$opt]);
foreach (self::TypeConverterOptions as $key) {
if (isset($options[$key])) {
$converter->$key = (bool) $options[$key];
unset($options[$key]);
}
}
return $explorer;
}
}

0 comments on commit 6ca4231

Please sign in to comment.