|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\EntityManager; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
|
8
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver; |
|
9
|
|
|
use Doctrine\DBAL\DBALException; |
|
10
|
|
|
use Doctrine\DBAL\Types\Type; |
|
11
|
|
|
use Doctrine\ORM\Configuration; |
|
12
|
|
|
use Doctrine\ORM\EntityManager; |
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use Doctrine\ORM\ORMException; |
|
15
|
|
|
use Doctrine\ORM\Tools; |
|
16
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\ConfigInterface; |
|
17
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
|
18
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\Decorator\EntityFactoryManagerDecorator; |
|
19
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\Mapping\ClassMetadataFactoryWithEntityFactories; |
|
20
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\RetryConnection\PingingAndReconnectingConnection; |
|
21
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\RetryConnection\ShouldConnectionByRetried; |
|
|
|
|
|
|
22
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
23
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
24
|
|
|
use Exception; |
|
25
|
|
|
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType; |
|
26
|
|
|
use Ramsey\Uuid\Doctrine\UuidBinaryType; |
|
27
|
|
|
use Ramsey\Uuid\Doctrine\UuidType; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class EntityManagerFactory |
|
31
|
|
|
* |
|
32
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\EntityManager |
|
33
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
34
|
|
|
*/ |
|
35
|
|
|
class EntityManagerFactory implements EntityManagerFactoryInterface |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Cache |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $cache; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var EntityFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $entityFactory; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(Cache $cache, EntityFactoryInterface $entityFactory) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->cache = $cache; |
|
50
|
|
|
$this->entityFactory = $entityFactory; |
|
51
|
|
|
$this->registerUuidDbalType(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws DBALException |
|
56
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
57
|
|
|
*/ |
|
58
|
|
|
private function registerUuidDbalType(): void |
|
59
|
|
|
{ |
|
60
|
|
|
if (!Type::hasType(MappingHelper::TYPE_UUID)) { |
|
61
|
|
|
Type::addType(MappingHelper::TYPE_UUID, UuidBinaryOrderedTimeType::class); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!Type::hasType(MappingHelper::TYPE_NON_BINARY_UUID)) { |
|
65
|
|
|
Type::addType(MappingHelper::TYPE_NON_BINARY_UUID, UuidType::class); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!Type::hasType(MappingHelper::TYPE_NON_ORDERED_BINARY_UUID)) { |
|
69
|
|
|
Type::addType(MappingHelper::TYPE_NON_ORDERED_BINARY_UUID, UuidBinaryType::class); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* This is used to create a new instance of the entity manager. Each of the steps involved need to take place, |
|
75
|
|
|
* however you may wish to make modifications to individual ones. There for the method is final, but the child |
|
76
|
|
|
* steps are public and can be overwritten if you extend the class |
|
77
|
|
|
* |
|
78
|
|
|
* @param ConfigInterface $config |
|
79
|
|
|
* |
|
80
|
|
|
* @return EntityManagerInterface |
|
81
|
|
|
* @throws DoctrineStaticMetaException |
|
82
|
|
|
*/ |
|
83
|
|
|
final public function getEntityManager(ConfigInterface $config): EntityManagerInterface |
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
|
|
$dbParams = $this->getDbConnectionInfo($config); |
|
87
|
|
|
$doctrineConfig = $this->getDoctrineConfig($config); |
|
88
|
|
|
$this->addDsmParamsToConfig($doctrineConfig, $config); |
|
89
|
|
|
$entityManager = $this->createEntityManager($dbParams, $doctrineConfig); |
|
90
|
|
|
$this->addEntityFactories($entityManager); |
|
91
|
|
|
$this->setDebuggingInfo($config, $entityManager); |
|
92
|
|
|
|
|
93
|
|
|
return $entityManager; |
|
94
|
|
|
} catch (Exception $e) { |
|
95
|
|
|
$message = 'Exception in ' . __METHOD__ . ': ' . $e->getMessage(); |
|
96
|
|
|
|
|
97
|
|
|
throw new DoctrineStaticMetaException($message, $e->getCode(), $e); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* This is used to get the connection information for doctrine. By default this pulls the information out of the |
|
104
|
|
|
* configuration interface, however if you connection information is in a different format you can override this |
|
105
|
|
|
* method and set it |
|
106
|
|
|
* |
|
107
|
|
|
* @param ConfigInterface $config |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getDbConnectionInfo(ConfigInterface $config): array |
|
112
|
|
|
{ |
|
113
|
|
|
$dbUser = $config->get(ConfigInterface::PARAM_DB_USER); |
|
114
|
|
|
$dbPass = $config->get(ConfigInterface::PARAM_DB_PASS); |
|
115
|
|
|
$dbHost = $config->get(ConfigInterface::PARAM_DB_HOST); |
|
116
|
|
|
$dbName = $config->get(ConfigInterface::PARAM_DB_NAME); |
|
117
|
|
|
$useRetryConnection = $config->get(ConfigInterface::PARAM_USE_RETRY_CONNECTION); |
|
118
|
|
|
|
|
119
|
|
|
$return = [ |
|
120
|
|
|
'driver' => 'pdo_mysql', |
|
121
|
|
|
'user' => $dbUser, |
|
122
|
|
|
'password' => $dbPass, |
|
123
|
|
|
'dbname' => $dbName, |
|
124
|
|
|
'host' => $dbHost, |
|
125
|
|
|
'charset' => 'utf8mb4', |
|
126
|
|
|
|
|
127
|
|
|
]; |
|
128
|
|
|
if (true === $useRetryConnection) { |
|
129
|
|
|
$return['wrapperClass'] = PingingAndReconnectingConnection::class; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* This is used to get the doctrine configuration object. By default this creates a new instance, however if you |
|
137
|
|
|
* already have an object configured you can override this method and inject it |
|
138
|
|
|
* |
|
139
|
|
|
* @param ConfigInterface $config |
|
140
|
|
|
* |
|
141
|
|
|
* @return Configuration |
|
142
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getDoctrineConfig(ConfigInterface $config): Configuration |
|
145
|
|
|
{ |
|
146
|
|
|
$isDevMode = (bool)$config->get(ConfigInterface::PARAM_DEVMODE); |
|
147
|
|
|
$proxyDir = $config->get(ConfigInterface::PARAM_DOCTRINE_PROXY_DIR); |
|
148
|
|
|
$cache = $isDevMode ? null : $this->cache; |
|
149
|
|
|
|
|
150
|
|
|
return Tools\Setup::createConfiguration($isDevMode, $proxyDir, $cache); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* This is used to add the DSM specific configuration to doctrine Configuration object. You shouldn't need to |
|
155
|
|
|
* override this, but if you do you can |
|
156
|
|
|
* |
|
157
|
|
|
* @param Configuration $doctrineConfig |
|
158
|
|
|
* @param ConfigInterface $config |
|
159
|
|
|
*/ |
|
160
|
|
|
public function addDsmParamsToConfig(Configuration $doctrineConfig, ConfigInterface $config): void |
|
161
|
|
|
{ |
|
162
|
|
|
$paths = $this->getPathInformation($config); |
|
163
|
|
|
$namingStrategy = $config->get(ConfigInterface::PARAM_DOCTRINE_NAMING_STRATEGY); |
|
164
|
|
|
$driver = new StaticPHPDriver($paths); |
|
165
|
|
|
$doctrineConfig->setMetadataDriverImpl($driver); |
|
166
|
|
|
$doctrineConfig->setNamingStrategy($namingStrategy); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* By default we only return a single path to the entities, however if you have your entities in multiple places you |
|
171
|
|
|
* can override this method and include them all |
|
172
|
|
|
* |
|
173
|
|
|
* @param ConfigInterface $config |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getPathInformation(ConfigInterface $config): array |
|
178
|
|
|
{ |
|
179
|
|
|
$dbEntitiesPath = $config->get(ConfigInterface::PARAM_ENTITIES_PATH); |
|
180
|
|
|
|
|
181
|
|
|
return [ |
|
182
|
|
|
$dbEntitiesPath, |
|
183
|
|
|
]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* This is used to create the Entity manager. You can override this if there are any calls you wish to make on it |
|
188
|
|
|
* after it has been created |
|
189
|
|
|
* |
|
190
|
|
|
* @param array $dbParams |
|
191
|
|
|
* @param Configuration $doctrineConfig |
|
192
|
|
|
* |
|
193
|
|
|
* @return EntityManagerInterface |
|
194
|
|
|
* @throws ORMException |
|
195
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
196
|
|
|
*/ |
|
197
|
|
|
public function createEntityManager(array $dbParams, Configuration $doctrineConfig): EntityManagerInterface |
|
198
|
|
|
{ |
|
199
|
|
|
$entityManager = EntityManager::create($dbParams, $doctrineConfig); |
|
200
|
|
|
|
|
201
|
|
|
return new EntityFactoryManagerDecorator($entityManager); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param EntityManagerInterface $entityManager |
|
206
|
|
|
*/ |
|
207
|
|
|
public function addEntityFactories(EntityManagerInterface $entityManager): void |
|
208
|
|
|
{ |
|
209
|
|
|
if (!$entityManager instanceof EntityFactoryManagerDecorator) { |
|
210
|
|
|
return; |
|
211
|
|
|
} |
|
212
|
|
|
$this->entityFactory->setEntityManager($entityManager); |
|
213
|
|
|
$entityManager->addGenericFactory($this->entityFactory); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* This is used to set any debugging information, by default it enables MySql logging and clears the log table. |
|
218
|
|
|
* Override this method if there is anything else that you need to do |
|
219
|
|
|
* |
|
220
|
|
|
* @param ConfigInterface $config |
|
221
|
|
|
* @param EntityManagerInterface $entityManager |
|
222
|
|
|
* |
|
223
|
|
|
* @throws DBALException |
|
224
|
|
|
*/ |
|
225
|
|
|
public function setDebuggingInfo(ConfigInterface $config, EntityManagerInterface $entityManager): void |
|
226
|
|
|
{ |
|
227
|
|
|
$isDbDebug = (bool)$config->get(ConfigInterface::PARAM_DB_DEBUG); |
|
228
|
|
|
if (false === $isDbDebug) { |
|
229
|
|
|
return; |
|
230
|
|
|
} |
|
231
|
|
|
$connection = $entityManager->getConnection(); |
|
232
|
|
|
$connection->query( |
|
233
|
|
|
" |
|
234
|
|
|
set global general_log = 1; |
|
235
|
|
|
set global log_output = 'table'; |
|
236
|
|
|
truncate general_log; |
|
237
|
|
|
" |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths