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