1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\Doctrine\EventListener\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\EventSubscriber; |
15
|
|
|
use Doctrine\ORM\EntityManager; |
16
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
17
|
|
|
use Doctrine\ORM\Events; |
18
|
|
|
use Doctrine\ORM\Id\SequenceGenerator; |
19
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
20
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
21
|
|
|
|
22
|
|
|
final class TablePrefixEventListener implements EventSubscriber |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
private $prefix; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string|null $prefix |
31
|
|
|
*/ |
32
|
|
|
public function __construct(?string $prefix) |
33
|
|
|
{ |
34
|
|
|
$this->prefix = $prefix; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function getSubscribedEvents(): array |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
Events::loadClassMetadata, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param LoadClassMetadataEventArgs $args |
49
|
|
|
*/ |
50
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $args): void |
51
|
|
|
{ |
52
|
|
|
if (null === $this->prefix) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$classMetadata = $args->getClassMetadata(); |
57
|
|
|
$entityManager = $args->getEntityManager(); |
58
|
|
|
|
59
|
|
|
$this->addTablePrefix($classMetadata); |
60
|
|
|
$this->addSequencePrefix($classMetadata, $entityManager); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ClassMetadata $classMetadata |
65
|
|
|
*/ |
66
|
|
|
private function addTablePrefix(ClassMetadata $classMetadata): void |
67
|
|
|
{ |
68
|
|
|
if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$tableName = $classMetadata->getTableName(); |
73
|
|
|
|
74
|
|
|
if (!$this->prefixExists($tableName)) { |
75
|
|
|
$classMetadata->setPrimaryTable([ |
76
|
|
|
'name' => $this->prefix.$tableName, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { |
81
|
|
|
if (ClassMetadataInfo::MANY_TO_MANY !== $mapping['type']) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])) { |
86
|
|
|
$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; |
87
|
|
|
|
88
|
|
|
if (!$this->prefixExists($mappedTableName)) { |
89
|
|
|
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix.$mappedTableName; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param ClassMetadata $classMetadata |
97
|
|
|
* @param EntityManager $em |
98
|
|
|
*/ |
99
|
|
|
private function addSequencePrefix(ClassMetadata $classMetadata, EntityManager $em): void |
100
|
|
|
{ |
101
|
|
|
if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!$classMetadata->isIdGeneratorSequence()) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$newDefinition = $classMetadata->sequenceGeneratorDefinition; |
110
|
|
|
|
111
|
|
|
$sequenceName = $newDefinition['sequenceName']; |
112
|
|
|
if (!$this->prefixExists($sequenceName)) { |
113
|
|
|
$newDefinition['sequenceName'] = $this->prefix.$sequenceName; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$classMetadata->setSequenceGeneratorDefinition($newDefinition); |
117
|
|
|
|
118
|
|
|
if (isset($classMetadata->idGenerator)) { |
119
|
|
|
$sequenceGenerator = new SequenceGenerator( |
120
|
|
|
$em->getConfiguration()->getQuoteStrategy()->getSequenceName( |
121
|
|
|
$newDefinition, |
122
|
|
|
$classMetadata, |
123
|
|
|
$em->getConnection()->getDatabasePlatform() |
124
|
|
|
), |
125
|
|
|
$newDefinition['allocationSize'] |
126
|
|
|
); |
127
|
|
|
$classMetadata->setIdGenerator($sequenceGenerator); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $name |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
private function prefixExists(string $name): bool |
137
|
|
|
{ |
138
|
|
|
return 0 === strpos($name, $this->prefix); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|