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
TablePrefixMetadataFactory.php
92 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 MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 10 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadataFactory; |
| 11 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadataInfo; |
| 12 | |
| 13 | // Taken from Doctrine docs (see link bellow) but implemented in metadata factory instead of an event |
| 14 | // because we need to add prefix at runtime, not at metadata dump (which is included in builds). |
| 15 | // @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.5/cookbook/sql-table-prefixes.html |
| 16 | class TablePrefixMetadataFactory extends ClassMetadataFactory { |
| 17 | // WordPress tables that are used by MailPoet via Doctrine |
| 18 | const WP_TABLES = [ |
| 19 | 'posts', |
| 20 | ]; |
| 21 | |
| 22 | /** @var string */ |
| 23 | private $prefix; |
| 24 | |
| 25 | /** @var string */ |
| 26 | private $wpDbPrefix; |
| 27 | |
| 28 | /** @var array */ |
| 29 | private $prefixedMap = []; |
| 30 | |
| 31 | public function __construct() { |
| 32 | if (Env::$dbPrefix === null) { |
| 33 | throw new \RuntimeException('DB table prefix not initialized'); |
| 34 | } |
| 35 | $this->prefix = Env::$dbPrefix; |
| 36 | $this->wpDbPrefix = Env::$wpDbPrefix; |
| 37 | $this->setProxyClassNameResolver(new ProxyClassNameResolver()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return ClassMetadata<object> |
| 42 | */ |
| 43 | public function getMetadataFor($className) { |
| 44 | $classMetadata = parent::getMetadataFor($className); |
| 45 | if (isset($this->prefixedMap[$classMetadata->getName()])) { |
| 46 | return $classMetadata; |
| 47 | } |
| 48 | |
| 49 | // prefix tables only after they are saved to cache so the prefix does not get included in cache |
| 50 | // (getMetadataFor can call itself recursively but it saves to cache only after the recursive calls) |
| 51 | $cacheKey = $this->getCacheKey($classMetadata->getName()); |
| 52 | $isCached = ($cache = $this->getCache()) ? $cache->hasItem($cacheKey) : false; |
| 53 | if ($classMetadata instanceof ClassMetadata && $isCached) { |
| 54 | $this->addPrefix($classMetadata); |
| 55 | $this->prefixedMap[$classMetadata->getName()] = true; |
| 56 | } |
| 57 | return $classMetadata; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param ClassMetadata<object> $classMetadata |
| 62 | */ |
| 63 | public function addPrefix(ClassMetadata $classMetadata) { |
| 64 | if (!$classMetadata->isInheritanceTypeSingleTable() || $classMetadata->getName() === $classMetadata->rootEntityName) { |
| 65 | $classMetadata->setPrimaryTable([ |
| 66 | 'name' => $this->createPrefixedName($classMetadata->getTableName()), |
| 67 | ]); |
| 68 | } |
| 69 | |
| 70 | foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { |
| 71 | if ($mapping['type'] === ClassMetadataInfo::MANY_TO_MANY && $mapping['isOwningSide']) { |
| 72 | /** @var string $mappedTableName */ |
| 73 | $mappedTableName = $mapping['joinTable']['name']; |
| 74 | $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->createPrefixedName($mappedTableName); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * MailPoet tables are prefixed by WP prefix + plugin prefix. |
| 81 | * For entities for WP tables we use WP prefix only. |
| 82 | */ |
| 83 | private function createPrefixedName(string $tableName): string { |
| 84 | // Use WP prefix for WP tables |
| 85 | if (in_array($tableName, self::WP_TABLES, true)) { |
| 86 | return $this->wpDbPrefix . $tableName; |
| 87 | } |
| 88 | // Use WP + plugin prefix for MailPoet tables |
| 89 | return $this->prefix . $tableName; |
| 90 | } |
| 91 | } |
| 92 |