2016-08-06 05:40:31 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-03-26 02:56:06 -05:00
|
|
|
use Doctrine\ORM\Events;
|
2021-02-26 13:01:16 -06:00
|
|
|
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
|
2022-01-20 13:16:37 -06:00
|
|
|
use Shlinkio\Shlink\Core\Config\EnvVars;
|
2024-04-01 03:22:51 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Listener\OrphanVisitsCountTracker;
|
2024-03-26 17:26:44 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Listener\ShortUrlVisitsCountTracker;
|
2024-03-26 03:42:25 -05:00
|
|
|
|
2023-11-30 11:09:15 -06:00
|
|
|
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
2021-08-07 04:05:20 -05:00
|
|
|
|
|
|
|
return (static function (): array {
|
2022-04-23 05:44:17 -05:00
|
|
|
$driver = EnvVars::DB_DRIVER->loadFromEnv();
|
2023-11-30 02:13:29 -06:00
|
|
|
$isMysqlCompatible = contains($driver, ['maria', 'mysql']);
|
2016-08-06 05:40:31 -05:00
|
|
|
|
2021-08-07 04:05:20 -05:00
|
|
|
$resolveDriver = static fn () => match ($driver) {
|
|
|
|
'postgres' => 'pdo_pgsql',
|
|
|
|
'mssql' => 'pdo_sqlsrv',
|
|
|
|
default => 'pdo_mysql',
|
|
|
|
};
|
2024-03-15 08:27:02 -05:00
|
|
|
$readCredentialAsString = static function (EnvVars $envVar): string|null {
|
|
|
|
$value = $envVar->loadFromEnv();
|
|
|
|
return $value === null ? null : (string) $value;
|
|
|
|
};
|
2022-01-10 06:04:16 -06:00
|
|
|
$resolveCharset = static fn () => match ($driver) {
|
|
|
|
// This does not determine charsets or collations in tables or columns, but the charset used in the data
|
|
|
|
// flowing in the connection, so it has to match what has been set in the database.
|
|
|
|
'maria', 'mysql' => 'utf8mb4',
|
|
|
|
'postgres' => 'utf8',
|
|
|
|
default => null,
|
|
|
|
};
|
2024-03-15 08:27:02 -05:00
|
|
|
|
2022-01-01 11:40:48 -06:00
|
|
|
$resolveConnection = static fn () => match ($driver) {
|
|
|
|
null, 'sqlite' => [
|
2021-08-07 04:05:20 -05:00
|
|
|
'driver' => 'pdo_sqlite',
|
|
|
|
'path' => 'data/database.sqlite',
|
2016-08-06 05:40:31 -05:00
|
|
|
],
|
2021-08-07 04:05:20 -05:00
|
|
|
default => [
|
|
|
|
'driver' => $resolveDriver(),
|
2024-10-13 05:24:59 -05:00
|
|
|
'dbname' => EnvVars::DB_NAME->loadFromEnv(),
|
2024-03-15 08:27:02 -05:00
|
|
|
'user' => $readCredentialAsString(EnvVars::DB_USER),
|
|
|
|
'password' => $readCredentialAsString(EnvVars::DB_PASSWORD),
|
2024-10-13 05:24:59 -05:00
|
|
|
'host' => EnvVars::DB_HOST->loadFromEnv(),
|
|
|
|
'port' => EnvVars::DB_PORT->loadFromEnv(),
|
2022-04-23 05:44:17 -05:00
|
|
|
'unix_socket' => $isMysqlCompatible ? EnvVars::DB_UNIX_SOCKET->loadFromEnv() : null,
|
2022-01-10 06:04:16 -06:00
|
|
|
'charset' => $resolveCharset(),
|
2022-12-10 12:40:33 -06:00
|
|
|
'driverOptions' => $driver !== 'mssql' ? [] : [
|
|
|
|
'TrustServerCertificate' => 'true',
|
|
|
|
],
|
2016-08-06 05:40:31 -05:00
|
|
|
],
|
2021-08-07 04:05:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
'entity_manager' => [
|
|
|
|
'orm' => [
|
|
|
|
'proxies_dir' => 'data/proxies',
|
|
|
|
'load_mappings_using_functional_style' => true,
|
|
|
|
'default_repository_classname' => EntitySpecificationRepository::class,
|
2024-03-26 02:56:06 -05:00
|
|
|
'listeners' => [
|
2024-04-01 03:22:51 -05:00
|
|
|
Events::onFlush => [ShortUrlVisitsCountTracker::class, OrphanVisitsCountTracker::class],
|
|
|
|
Events::postFlush => [ShortUrlVisitsCountTracker::class, OrphanVisitsCountTracker::class],
|
2024-03-26 02:56:06 -05:00
|
|
|
],
|
2021-08-07 04:05:20 -05:00
|
|
|
],
|
|
|
|
'connection' => $resolveConnection(),
|
|
|
|
],
|
2016-08-06 05:40:31 -05:00
|
|
|
|
2021-08-07 04:05:20 -05:00
|
|
|
];
|
|
|
|
})();
|