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