Annotations
3 years ago
EntityTraits
2 years ago
EventListeners
2 years ago
Types
3 years ago
Validator
3 years ago
ArrayCache.php
3 years ago
CacheOnlyMappingDriver.php
3 years ago
ConfigurationFactory.php
3 years ago
ConnectionFactory.php
3 years ago
EntityManagerFactory.php
2 years ago
MetadataCache.php
2 years ago
PSRArrayCache.php
3 years ago
PSRCacheInvalidArgumentException.php
3 years ago
PSRCacheItem.php
3 years ago
PSRMetadataCache.php
3 years ago
ProxyClassNameResolver.php
3 years ago
Repository.php
2 years ago
SerializableConnection.php
3 years ago
TablePrefixMetadataFactory.php
2 years ago
index.php
3 years ago
ConnectionFactory.php
103 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Doctrine; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Doctrine\Types\BigIntType; |
| 10 | use MailPoet\Doctrine\Types\DateTimeTzToStringType; |
| 11 | use MailPoet\Doctrine\Types\JsonOrSerializedType; |
| 12 | use MailPoet\Doctrine\Types\JsonType; |
| 13 | use MailPoet\Doctrine\Types\SerializedArrayType; |
| 14 | use MailPoetVendor\Doctrine\DBAL\Driver\PDO\MySQL\Driver; |
| 15 | use MailPoetVendor\Doctrine\DBAL\DriverManager; |
| 16 | use MailPoetVendor\Doctrine\DBAL\Platforms\MySqlPlatform; |
| 17 | use MailPoetVendor\Doctrine\DBAL\Types\Type; |
| 18 | use PDO; |
| 19 | |
| 20 | class ConnectionFactory { |
| 21 | const DRIVER = 'pdo_mysql'; |
| 22 | const PLATFORM_CLASS = MySqlPlatform::class; |
| 23 | |
| 24 | private $minWaitTimeout = 60; |
| 25 | |
| 26 | private $types = [ |
| 27 | BigIntType::NAME => BigIntType::class, |
| 28 | DateTimeTzToStringType::NAME => DateTimeTzToStringType::class, |
| 29 | JsonType::NAME => JsonType::class, |
| 30 | JsonOrSerializedType::NAME => JsonOrSerializedType::class, |
| 31 | SerializedArrayType::NAME => SerializedArrayType::class, |
| 32 | ]; |
| 33 | |
| 34 | public function createConnection() { |
| 35 | global $wpdb; |
| 36 | $platformClass = self::PLATFORM_CLASS; |
| 37 | $connectionParams = [ |
| 38 | 'wrapperClass' => SerializableConnection::class, |
| 39 | 'driver' => self::DRIVER, |
| 40 | 'driverClass' => Driver::class, |
| 41 | 'platform' => new $platformClass, |
| 42 | 'user' => Env::$dbUsername, |
| 43 | 'password' => Env::$dbPassword, |
| 44 | 'dbname' => Env::$dbName, |
| 45 | 'driverOptions' => $this->getDriverOptions(Env::$dbTimezoneOffset, Env::$dbCharset, Env::$dbCollation), |
| 46 | ]; |
| 47 | |
| 48 | if (!empty(Env::$dbCharset)) { |
| 49 | $connectionParams['charset'] = Env::$dbCharset; |
| 50 | } |
| 51 | |
| 52 | if (!empty(Env::$dbSocket)) { |
| 53 | $connectionParams['unix_socket'] = Env::$dbSocket; |
| 54 | } else { |
| 55 | $connectionParams['host'] = Env::$dbIsIpv6 ? ('[' . Env::$dbHost . ']') : Env::$dbHost; |
| 56 | if (!empty(Env::$dbPort)) { |
| 57 | $connectionParams['port'] = Env::$dbPort; |
| 58 | } else { |
| 59 | $connectionParams['port'] = $wpdb->get_var('SELECT @@port'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $this->setupTypes(); |
| 64 | return DriverManager::getConnection($connectionParams); |
| 65 | } |
| 66 | |
| 67 | private function getDriverOptions($timezoneOffset, $charset, $collation) { |
| 68 | $driverOptions = [ |
| 69 | "@@session.time_zone = '$timezoneOffset'", |
| 70 | "@@session.sql_mode = REPLACE( |
| 71 | REPLACE( |
| 72 | REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''), |
| 73 | '_QUOTES', '' |
| 74 | ), 'ANSI', '')", // This is needed because ONLY_FULL_GROUP_BY mode in MariaDB is much more restrictive than in MySQL |
| 75 | // We need to use CONVERT for MySQL 8, Maria DB bug which triggers #1232 - Incorrect argument type to variable 'wait_timeout` |
| 76 | // https://stackoverflow.com/questions/35187378/mariadb-type-error-when-setting-session-variable |
| 77 | "@@session.wait_timeout = GREATEST(CONVERT(COALESCE(@@wait_timeout, 0), SIGNED), $this->minWaitTimeout)", |
| 78 | ]; |
| 79 | |
| 80 | if (!empty(Env::$dbCharset)) { |
| 81 | $driverOptions[] = "NAMES $charset" . (empty($collation) ? '' : " COLLATE $collation"); |
| 82 | } |
| 83 | |
| 84 | return [ |
| 85 | PDO::MYSQL_ATTR_INIT_COMMAND => 'SET ' . implode(', ', $driverOptions), |
| 86 | // In PHP 8.1 was changed MySQL behavior that numbers are returned as native PHP types instead of strings. https://www.php.net/manual/en/migration81.incompatible.php |
| 87 | // We force the previous state due to the backward compatibility of the Idiorm. |
| 88 | // This can be removed when we drop Idiorm. |
| 89 | PDO::ATTR_STRINGIFY_FETCHES => true, |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | private function setupTypes() { |
| 94 | foreach ($this->types as $name => $class) { |
| 95 | if (Type::hasType($name)) { |
| 96 | Type::overrideType($name, $class); |
| 97 | } else { |
| 98 | Type::addType($name, $class); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |