Files
KantSites/src/Infrastructure/ConnectionFactory.php
T
kantarci 41d87f62a4
CMS quality / php (8.2) (push) Canceled after 0s
CMS quality / php (8.4) (push) Canceled after 0s
CMS quality / browser (push) Canceled after 0s
CMS quality / mysql (push) Canceled after 0s
Ergänzt AJAX-Livesuche für Seiten, Beiträge und Mediathek
2026-09-14 11:09:24 +02:00

34 lines
1.4 KiB
PHP

<?php
namespace App\Infrastructure;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
final class ConnectionFactory
{
public static function create(string $url, ?QueryCounter $counter = null): Connection
{
$params = (new DsnParser(['sqlite' => 'pdo_sqlite', 'mysql' => 'pdo_mysql']))->parse($url);
$configuration = null;
if (null !== $counter) {
// Counting queries is a measuring instrument, not a feature: it is wired up outside
// the production environment, where the browser checks read the number.
$configuration = new Configuration();
$configuration->setMiddlewares([$counter]);
}
$connection = DriverManager::getConnection($params, $configuration);
if ('pdo_sqlite' === $params['driver']) {
// SQLite's built-in LOWER only handles ASCII: searching for "über" must also find
// "Über uns", just as on MySQL. No database rewrite or search-specific schema needed.
$connection->getNativeConnection()->sqliteCreateFunction('lower', static fn (?string $value): ?string => null === $value ? null : mb_strtolower($value, 'UTF-8'), 1, \PDO::SQLITE_DETERMINISTIC);
$connection->executeStatement('PRAGMA foreign_keys = ON');
$connection->executeStatement('PRAGMA busy_timeout = 5000');
}
return $connection;
}
}