1
|
|
|
<?php namespace Nord\Lumen\Doctrine\ORM; |
2
|
|
|
|
3
|
|
|
use Doctrine\Common\Cache\Cache; |
4
|
|
|
use Doctrine\Common\EventManager; |
5
|
|
|
use Doctrine\DBAL\Types\Type; |
6
|
|
|
use Doctrine\ORM\Configuration; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Doctrine\ORM\Tools\Setup; |
10
|
|
|
use Exception; |
11
|
|
|
use Illuminate\Config\Repository as ConfigRepository; |
12
|
|
|
use Illuminate\Contracts\Container\Container; |
13
|
|
|
use Illuminate\Support\ServiceProvider; |
14
|
|
|
use Nord\Lumen\Doctrine\ORM\Configuration\ConnectionConfiguration; |
15
|
|
|
use Nord\Lumen\Doctrine\ORM\Configuration\SqlAdapter; |
16
|
|
|
use Nord\Lumen\Doctrine\ORM\Configuration\SqliteAdapter; |
17
|
|
|
use Nord\Lumen\Doctrine\ORM\Contracts\ConfigurationAdapter; |
18
|
|
|
|
19
|
|
|
class DoctrineServiceProvider extends ServiceProvider |
20
|
|
|
{ |
21
|
|
|
const CONFIG_KEY = 'doctrine'; |
22
|
|
|
|
23
|
|
|
const METADATA_ANNOTATIONS = 'annotations'; |
24
|
|
|
const METADATA_XML = 'xml'; |
25
|
|
|
const METADATA_YAML = 'yaml'; |
26
|
|
|
|
27
|
|
|
const DRIVER_MYSQL = 'mysql'; |
28
|
|
|
const DRIVER_PGSQL = 'pgsql'; |
29
|
|
|
const DRIVER_SQLSRV = 'sqlsrv'; |
30
|
|
|
const DRIVER_SQLITE = 'sqlite'; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function register() |
37
|
|
|
{ |
38
|
|
|
$this->app->configure(self::CONFIG_KEY); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->registerBindings($this->app, $this->app['config']); |
41
|
|
|
$this->registerFacades(); |
42
|
|
|
$this->registerCommands(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Registers container bindings. |
48
|
|
|
* |
49
|
|
|
* @param Container $container |
50
|
|
|
* @param ConfigRepository $config |
51
|
|
|
*/ |
52
|
|
|
protected function registerBindings(Container $container, ConfigRepository $config) |
53
|
|
|
{ |
54
|
|
|
$container->singleton('Doctrine\ORM\EntityManager', function () use ($config) { |
55
|
|
|
return $this->createEntityManager($config); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$container->alias('Doctrine\ORM\EntityManager', 'Doctrine\ORM\EntityManagerInterface'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Registers facades. |
64
|
|
|
*/ |
65
|
|
|
protected function registerFacades() |
66
|
|
|
{ |
67
|
|
|
if (!class_exists('EntityManager')) { |
68
|
|
|
class_alias('Nord\Lumen\Doctrine\ORM\Facades\EntityManager', 'EntityManager'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Registers console commands. |
75
|
|
|
*/ |
76
|
|
|
protected function registerCommands() |
77
|
|
|
{ |
78
|
|
|
$this->commands([ |
79
|
|
|
'Nord\Lumen\Doctrine\ORM\Console\GenerateProxiesCommand', |
80
|
|
|
'Nord\Lumen\Doctrine\ORM\Console\SchemaCreateCommand', |
81
|
|
|
'Nord\Lumen\Doctrine\ORM\Console\SchemaDropCommand', |
82
|
|
|
'Nord\Lumen\Doctrine\ORM\Console\SchemaUpdateCommand', |
83
|
|
|
'Nord\Lumen\Doctrine\ORM\Console\FixturesLoadCommand', |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates the Doctrine entity manager instance. |
90
|
|
|
* |
91
|
|
|
* @param ConfigRepository $config |
92
|
|
|
* |
93
|
|
|
* @return EntityManager |
94
|
|
|
* @throws Exception |
95
|
|
|
* @throws \Doctrine\ORM\ORMException |
96
|
|
|
*/ |
97
|
|
|
protected function createEntityManager(ConfigRepository $config) |
98
|
|
|
{ |
99
|
|
|
if (!isset($config['doctrine'])) { |
100
|
|
|
throw new Exception('Doctrine configuration not registered.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!isset($config['database'])) { |
104
|
|
|
throw new Exception('Database configuration not registered.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$doctrineConfig = $config['doctrine']; |
108
|
|
|
$databaseConfig = $config['database']; |
109
|
|
|
|
110
|
|
|
$connectionConfig = $this->createConnectionConfig($doctrineConfig, $databaseConfig); |
111
|
|
|
|
112
|
|
|
$type = array_get($doctrineConfig, 'mapping', self::METADATA_ANNOTATIONS); |
113
|
|
|
$paths = array_get($doctrineConfig, 'paths', [base_path('app/Entities')]); |
114
|
|
|
$debug = $config['app.debug']; |
115
|
|
|
$proxyDir = array_get($doctrineConfig, 'proxy.directory'); |
116
|
|
|
$simpleAnnotations = array_get($doctrineConfig, 'simple_annotations', false); |
117
|
|
|
|
118
|
|
|
$metadataConfiguration = $this->createMetadataConfiguration($type, $paths, $debug, $proxyDir, null, |
|
|
|
|
119
|
|
|
$simpleAnnotations); |
120
|
|
|
|
121
|
|
|
$this->configureMetadataConfiguration($metadataConfiguration, $doctrineConfig); |
122
|
|
|
|
123
|
|
|
$eventManager = new EventManager; |
124
|
|
|
|
125
|
|
|
$this->configureEventManager($doctrineConfig, $eventManager); |
126
|
|
|
|
127
|
|
|
$entityManager = EntityManager::create($connectionConfig, $metadataConfiguration, $eventManager); |
128
|
|
|
|
129
|
|
|
$this->configureEntityManager($doctrineConfig, $entityManager); |
130
|
|
|
|
131
|
|
|
return $entityManager; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Creates the Doctrine connection configuration. |
137
|
|
|
* |
138
|
|
|
* @param array $doctrineConfig |
139
|
|
|
* @param array $databaseConfig |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
|
|
protected function createConnectionConfig(array $doctrineConfig, array $databaseConfig) |
145
|
|
|
{ |
146
|
|
|
$connectionName = array_get($doctrineConfig, 'connection', $databaseConfig['default']); |
147
|
|
|
$connectionConfig = array_get($databaseConfig['connections'], $connectionName); |
148
|
|
|
|
149
|
|
|
if ($connectionConfig === null) { |
150
|
|
|
throw new Exception("Configuration for connection '$connectionName' not found."); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->normalizeConnectionConfig($connectionConfig); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Normalizes the connection config to a format Doctrine can use. |
159
|
|
|
* |
160
|
|
|
* @param array $config |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
* @throws \Exception |
164
|
|
|
*/ |
165
|
|
|
protected function normalizeConnectionConfig(array $config) |
166
|
|
|
{ |
167
|
|
|
$adapter = $this->createConfigurationAdapter($config['driver']); |
168
|
|
|
|
169
|
|
|
$configuration = new ConnectionConfiguration($adapter); |
170
|
|
|
|
171
|
|
|
return $configuration->map($config); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $driver |
177
|
|
|
* |
178
|
|
|
* @return ConfigurationAdapter |
179
|
|
|
* @throws Exception |
180
|
|
|
*/ |
181
|
|
|
protected function createConfigurationAdapter($driver) |
182
|
|
|
{ |
183
|
|
|
switch ($driver) { |
184
|
|
|
case self::DRIVER_MYSQL: |
185
|
|
|
case self::DRIVER_PGSQL: |
186
|
|
|
case self::DRIVER_SQLSRV: |
187
|
|
|
return new SqlAdapter(); |
188
|
|
|
case self::DRIVER_SQLITE: |
189
|
|
|
return new SqliteAdapter(); |
190
|
|
|
default: |
191
|
|
|
throw new Exception("Driver '{$driver}' is not supported."); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Creates the metadata configuration instance. |
198
|
|
|
* |
199
|
|
|
* @param string $type |
200
|
|
|
* @param array $paths |
201
|
|
|
* @param bool $isDevMode |
202
|
|
|
* @param string $proxyDir |
203
|
|
|
* @param Cache $cache |
204
|
|
|
* @param bool $useSimpleAnnotationReader |
205
|
|
|
* |
206
|
|
|
* @return Configuration |
207
|
|
|
* @throws \Exception |
208
|
|
|
*/ |
209
|
|
|
protected function createMetadataConfiguration( |
210
|
|
|
$type, |
211
|
|
|
$paths, |
212
|
|
|
$isDevMode, |
213
|
|
|
$proxyDir, |
214
|
|
|
$cache, |
215
|
|
|
$useSimpleAnnotationReader = true |
216
|
|
|
) { |
217
|
|
|
switch ($type) { |
218
|
|
|
case self::METADATA_ANNOTATIONS: |
219
|
|
|
return Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, $proxyDir, $cache, |
220
|
|
|
$useSimpleAnnotationReader); |
221
|
|
|
case self::METADATA_XML: |
222
|
|
|
return Setup::createXMLMetadataConfiguration($paths, $isDevMode, $proxyDir, $cache); |
223
|
|
|
case self::METADATA_YAML: |
224
|
|
|
return Setup::createYAMLMetadataConfiguration($paths, $isDevMode, $proxyDir, $cache); |
225
|
|
|
default: |
226
|
|
|
throw new Exception("Metadata type '$type' is not supported."); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Configures the metadata configuration instance. |
233
|
|
|
* |
234
|
|
|
* @param Configuration $configuration |
235
|
|
|
* @param array $doctrineConfig |
236
|
|
|
* |
237
|
|
|
* @throws ORMException |
238
|
|
|
*/ |
239
|
|
|
protected function configureMetadataConfiguration( |
240
|
|
|
Configuration $configuration, |
241
|
|
|
array $doctrineConfig |
242
|
|
|
) { |
243
|
|
|
if (isset($doctrineConfig['filters'])) { |
244
|
|
|
foreach ($doctrineConfig['filters'] as $name => $filter) { |
245
|
|
|
$configuration->addFilter($name, $filter['class']); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
if (isset($doctrineConfig['logger'])) { |
249
|
|
|
$configuration->setSQLLogger($doctrineConfig['logger']); |
250
|
|
|
} |
251
|
|
|
if (isset($doctrineConfig['proxy']) && isset($doctrineConfig['proxy']['auto_generate'])) { |
252
|
|
|
$configuration->setAutoGenerateProxyClasses($doctrineConfig['proxy']['auto_generate']); |
253
|
|
|
} |
254
|
|
|
if (isset($doctrineConfig['proxy']) && isset($doctrineConfig['proxy']['namespace'])) { |
255
|
|
|
$configuration->setProxyNamespace($doctrineConfig['proxy']['namespace']); |
256
|
|
|
} |
257
|
|
|
if (isset($doctrineConfig['repository'])) { |
258
|
|
|
$configuration->setDefaultRepositoryClassName($doctrineConfig['repository']); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$namingStrategy = array_get($doctrineConfig, 'naming_strategy', 'Nord\Lumen\Doctrine\ORM\NamingStrategy'); |
262
|
|
|
$configuration->setNamingStrategy(new $namingStrategy); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Configures the Doctrine event manager instance. |
268
|
|
|
* |
269
|
|
|
* @param array $doctrineConfig |
270
|
|
|
* @param EventManager $eventManager |
271
|
|
|
*/ |
272
|
|
|
protected function configureEventManager(array $doctrineConfig, EventManager $eventManager) |
273
|
|
|
{ |
274
|
|
|
if (isset($doctrineConfig['event_listeners'])) { |
275
|
|
|
foreach ($doctrineConfig['event_listeners'] as $name => $listener) { |
276
|
|
|
$eventManager->addEventListener($listener['events'], new $listener['class']); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Configures the Doctrine entity manager instance. |
284
|
|
|
* |
285
|
|
|
* @param array $doctrineConfig |
286
|
|
|
* @param EntityManager $entityManager |
287
|
|
|
*/ |
288
|
|
|
protected function configureEntityManager(array $doctrineConfig, EntityManager $entityManager) |
289
|
|
|
{ |
290
|
|
|
if (isset($doctrineConfig['filters'])) { |
291
|
|
|
foreach ($doctrineConfig['filters'] as $name => $filter) { |
292
|
|
|
if (!array_get($filter, 'enabled', false)) { |
293
|
|
|
continue; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$entityManager->getFilters()->enable($name); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if (isset($doctrineConfig['types'])) { |
301
|
|
|
$databasePlatform = $entityManager->getConnection()->getDatabasePlatform(); |
302
|
|
|
|
303
|
|
|
foreach ($doctrineConfig['types'] as $name => $className) { |
304
|
|
|
Type::addType($name, $className); |
305
|
|
|
$databasePlatform->registerDoctrineTypeMapping($name, $name); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.