Files
piratepoet/mailpoet/lib/Doctrine/ConnectionFactory.php
Rostislav Wolny 9f32c59e49 Use DBAL Driver middleware to handle the correct connection timezone
I looked into using DBAL events for this but found that DBAL events
are deprecated so I went with middleware as they are recommended
as a replacement for the DBAL events.
[MAILPOET-6142]
2024-08-19 15:29:42 +02:00

63 lines
2.0 KiB
PHP

<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
namespace MailPoet\Doctrine;
use MailPoet\Doctrine\Middlewares\PostConnectMiddleware;
use MailPoet\Doctrine\Types\BigIntType;
use MailPoet\Doctrine\Types\DateTimeTzToStringType;
use MailPoet\Doctrine\Types\JsonOrSerializedType;
use MailPoet\Doctrine\Types\JsonType;
use MailPoet\Doctrine\Types\SerializedArrayType;
use MailPoet\Doctrine\WPDB\Driver as WPDBDriver;
use MailPoetVendor\Doctrine\DBAL\Configuration;
use MailPoetVendor\Doctrine\DBAL\Driver;
use MailPoetVendor\Doctrine\DBAL\Driver\Middleware;
use MailPoetVendor\Doctrine\DBAL\DriverManager;
use MailPoetVendor\Doctrine\DBAL\Platforms\MySQLPlatform;
use MailPoetVendor\Doctrine\DBAL\Types\Type;
class ConnectionFactory {
const DRIVER_CLASS = WPDBDriver::class;
const PLATFORM_CLASS = MySQLPlatform::class;
private $types = [
BigIntType::NAME => BigIntType::class,
DateTimeTzToStringType::NAME => DateTimeTzToStringType::class,
JsonType::NAME => JsonType::class,
JsonOrSerializedType::NAME => JsonOrSerializedType::class,
SerializedArrayType::NAME => SerializedArrayType::class,
];
public function createConnection() {
$platformClass = self::PLATFORM_CLASS;
$connectionParams = [
'driverClass' => self::DRIVER_CLASS,
'platform' => new $platformClass,
];
$this->setupTypes();
return DriverManager::getConnection($connectionParams, $this->getConfiguration());
}
private function setupTypes() {
foreach ($this->types as $name => $class) {
if (Type::hasType($name)) {
Type::overrideType($name, $class);
} else {
Type::addType($name, $class);
}
}
}
private function getConfiguration(): Configuration {
$config = new Configuration();
$driverMiddleware = new class implements Middleware {
public function wrap(Driver $driver): Driver {
return new PostConnectMiddleware($driver);
}
};
$config->setMiddlewares([$driverMiddleware]);
return $config;
}
}