-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathConfiguration.php
596 lines (513 loc) · 19 KB
/
Configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
declare(strict_types=1);
namespace Doctrine\ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache as CacheDriver;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\DBAL\Configuration as DBALConfiguration;
use Doctrine\ORM\Cache\CacheConfiguration;
use Doctrine\ORM\Cache\Exception\MetadataCacheNotConfigured;
use Doctrine\ORM\Cache\Exception\MetadataCacheUsesNonPersistentCache;
use Doctrine\ORM\Cache\Exception\QueryCacheNotConfigured;
use Doctrine\ORM\Cache\Exception\QueryCacheUsesNonPersistentCache;
use Doctrine\ORM\Exception\InvalidEntityRepository;
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\MappingDriver;
use Doctrine\ORM\Mapping\EntityListenerResolver;
use Doctrine\ORM\Mapping\Factory\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\Factory\NamingStrategy;
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
use ProxyManager\Configuration as ProxyManagerConfiguration;
use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\FileLocator\FileLocator;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use ReflectionClass;
use function strtolower;
/**
* Configuration container for all configuration options of Doctrine.
* It combines all configuration options from DBAL & ORM.
*
* {@internal When adding a new configuration option just write a getter/setter pair. }}
*/
class Configuration extends DBALConfiguration
{
/** @var ProxyManagerConfiguration|null */
private $proxyManagerConfiguration;
/** @var MappingDriver|null */
private $metadataDriver;
/** @var CacheDriver|null */
private $queryCache;
/** @var CacheDriver|null */
private $hydrationCache;
/** @var CacheDriver|null */
private $metadataCache;
/** @var string[][]|ResultSetMapping[][] tuples of [$sqlString, $resultSetMapping] indexed by query name */
private $customStringFunctions = [];
/** @var string[][]|ResultSetMapping[][] tuples of [$sqlString, $resultSetMapping] indexed by query name */
private $customNumericFunctions = [];
/** @var string[][]|ResultSetMapping[][] tuples of [$sqlString, $resultSetMapping] indexed by query name */
private $customDatetimeFunctions = [];
/** @var string[] of hydrator class names, indexed by mode name */
private $customHydrationModes = [];
/** @var string */
private $classMetadataFactoryClassName = ClassMetadataFactory::class;
/** @var string[] of filter class names, indexed by filter name */
private $filters;
/** @var string */
private $defaultRepositoryClassName = EntityRepository::class;
/** @var NamingStrategy|null */
private $namingStrategy;
/** @var EntityListenerResolver|null */
private $entityListenerResolver;
/** @var RepositoryFactory|null */
private $repositoryFactory;
/** @var bool */
private $isSecondLevelCacheEnabled = false;
/** @var CacheConfiguration|null */
private $secondLevelCacheConfiguration;
/** @var mixed[] indexed by hint name */
private $defaultQueryHints = [];
/**
* Sets the directory where Doctrine generates any necessary proxy class files.
*/
public function setProxyDir(string $directory) : void
{
$this->getProxyManagerConfiguration()->setProxiesTargetDir($directory);
$this->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
}
/**
* Sets the strategy for automatically generating proxy classes.
*
* @param bool|int $autoGenerate Possible values are constants of Doctrine\ORM\Proxy\Factory\ProxyFactory.
* True is converted to AUTOGENERATE_ALWAYS, false to AUTOGENERATE_NEVER.
*/
public function setAutoGenerateProxyClasses($autoGenerate) : void
{
$proxyManagerConfig = $this->getProxyManagerConfiguration();
switch ((int) $autoGenerate) {
case ProxyFactory::AUTOGENERATE_ALWAYS:
case ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS:
$proxyManagerConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy(
new FileLocator($proxyManagerConfig->getProxiesTargetDir())
));
return;
case ProxyFactory::AUTOGENERATE_NEVER:
case ProxyFactory::AUTOGENERATE_EVAL:
default:
$proxyManagerConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
return;
}
}
/**
* Sets the namespace where proxy classes reside.
*/
public function setProxyNamespace(string $namespace) : void
{
$this->getProxyManagerConfiguration()->setProxiesNamespace($namespace);
}
/**
* Sets the cache driver implementation that is used for metadata caching.
*
* @todo Force parameter to be a Closure to ensure lazy evaluation
* (as soon as a metadata cache is in effect, the driver never needs to initialize).
*/
public function setMetadataDriverImpl(MappingDriver $metadataDriver) : void
{
$this->metadataDriver = $metadataDriver;
}
/**
* Adds a new default annotation driver with a correctly configured annotation reader.
*
* @param string[] $paths
*/
public function newDefaultAnnotationDriver(array $paths = []) : AnnotationDriver
{
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/DoctrineAnnotations.php');
$reader = new CachedReader(new AnnotationReader(), new ArrayCache());
return new AnnotationDriver($reader, $paths);
}
/**
* Gets the cache driver implementation that is used for the mapping metadata.
*/
public function getMetadataDriverImpl() : ?MappingDriver
{
return $this->metadataDriver;
}
/**
* Gets the cache driver implementation that is used for the query cache (SQL cache).
*/
public function getQueryCacheImpl() : ?CacheDriver
{
return $this->queryCache;
}
/**
* Sets the cache driver implementation that is used for the query cache (SQL cache).
*/
public function setQueryCacheImpl(CacheDriver $queryCache) : void
{
$this->queryCache = $queryCache;
}
/**
* Gets the cache driver implementation that is used for the hydration cache (SQL cache).
*/
public function getHydrationCacheImpl() : ?CacheDriver
{
return $this->hydrationCache;
}
/**
* Sets the cache driver implementation that is used for the hydration cache (SQL cache).
*/
public function setHydrationCacheImpl(CacheDriver $hydrationCache) : void
{
$this->hydrationCache = $hydrationCache;
}
/**
* Gets the cache driver implementation that is used for metadata caching.
*/
public function getMetadataCacheImpl() : ?CacheDriver
{
return $this->metadataCache;
}
/**
* Sets the cache driver implementation that is used for metadata caching.
*/
public function setMetadataCacheImpl(CacheDriver $metadataCache) : void
{
$this->metadataCache = $metadataCache;
}
/**
* Ensures that this Configuration instance contains settings that are
* suitable for a production environment.
*
* @throws ORMException If a configuration setting has a value that is not
* suitable for a production environment.
*/
public function ensureProductionSettings() : void
{
$queryCacheImpl = $this->getQueryCacheImpl();
if (! $queryCacheImpl) {
throw QueryCacheNotConfigured::create();
}
if ($queryCacheImpl instanceof ArrayCache) {
throw QueryCacheUsesNonPersistentCache::fromDriver($queryCacheImpl);
}
$metadataCacheImpl = $this->getMetadataCacheImpl();
if (! $metadataCacheImpl) {
throw MetadataCacheNotConfigured::create();
}
if ($metadataCacheImpl instanceof ArrayCache) {
throw MetadataCacheUsesNonPersistentCache::fromDriver($metadataCacheImpl);
}
if ($this->getProxyManagerConfiguration()->getGeneratorStrategy() instanceof EvaluatingGeneratorStrategy) {
throw ProxyClassesAlwaysRegenerating::create();
}
}
/**
* Registers a custom DQL function that produces a string value.
* Such a function can then be used in any DQL statement in any place where string
* functions are allowed.
*
* DQL function names are case-insensitive.
*
* @param string|callable $classNameOrFactory Class name or a callable that returns the function.
*/
public function addCustomStringFunction(string $functionName, $classNameOrFactory) : void
{
$this->customStringFunctions[strtolower($functionName)] = $classNameOrFactory;
}
/**
* Gets the implementation class name of a registered custom string DQL function.
*
* @return string|callable|null
*/
public function getCustomStringFunction(string $functionName)
{
return $this->customStringFunctions[strtolower($functionName)] ?? null;
}
/**
* Sets a map of custom DQL string functions.
*
* Keys must be function names and values the FQCN of the implementing class.
* The function names will be case-insensitive in DQL.
*
* Any previously added string functions are discarded.
*
* @param string[]|callable[] $functions The map of custom DQL string functions.
*/
public function setCustomStringFunctions(array $functions) : void
{
foreach ($functions as $name => $className) {
$this->addCustomStringFunction($name, $className);
}
}
/**
* Registers a custom DQL function that produces a numeric value.
* Such a function can then be used in any DQL statement in any place where numeric
* functions are allowed.
*
* DQL function names are case-insensitive.
*
* @param string|callable $classNameOrFactory Class name or a callable that returns the function.
*/
public function addCustomNumericFunction(string $functionName, $classNameOrFactory) : void
{
$this->customNumericFunctions[strtolower($functionName)] = $classNameOrFactory;
}
/**
* Gets the implementation class name of a registered custom numeric DQL function.
*
* @return string|callable|null
*/
public function getCustomNumericFunction(string $functionName)
{
return $this->customNumericFunctions[strtolower($functionName)] ?? null;
}
/**
* Sets a map of custom DQL numeric functions.
*
* Keys must be function names and values the FQCN of the implementing class.
* The function names will be case-insensitive in DQL.
*
* Any previously added numeric functions are discarded.
*
* @param string[]|callable[] $functions The map of custom DQL numeric functions.
*/
public function setCustomNumericFunctions(array $functions) : void
{
foreach ($functions as $name => $className) {
$this->addCustomNumericFunction($name, $className);
}
}
/**
* Registers a custom DQL function that produces a date/time value.
* Such a function can then be used in any DQL statement in any place where date/time
* functions are allowed.
*
* DQL function names are case-insensitive.
*
* @param string|callable $classNameOrFactory Class name or a callable that returns the function.
*/
public function addCustomDatetimeFunction(string $functionName, $classNameOrFactory)
{
$this->customDatetimeFunctions[strtolower($functionName)] = $classNameOrFactory;
}
/**
* Gets the implementation class name of a registered custom date/time DQL function.
*
* @return string|callable|null
*/
public function getCustomDatetimeFunction(string $functionName)
{
return $this->customDatetimeFunctions[strtolower($functionName)] ?? null;
}
/**
* Sets a map of custom DQL date/time functions.
*
* Keys must be function names and values the FQCN of the implementing class.
* The function names will be case-insensitive in DQL.
*
* Any previously added date/time functions are discarded.
*
* @param iterable|string[] $functions The map of custom DQL date/time functions.
*/
public function setCustomDatetimeFunctions(array $functions) : void
{
foreach ($functions as $name => $className) {
$this->addCustomDatetimeFunction($name, $className);
}
}
/**
* Sets the custom hydrator modes in one pass.
*
* @param iterable|string[] $modes An iterable of string $modeName => string $hydratorClassName
*/
public function setCustomHydrationModes(iterable $modes) : void
{
$this->customHydrationModes = [];
foreach ($modes as $modeName => $hydrator) {
$this->addCustomHydrationMode($modeName, $hydrator);
}
}
/**
* Gets the hydrator class for the given hydration mode name.
*
* @return string|null The hydrator class name.
*/
public function getCustomHydrationMode(string $modeName) : ?string
{
return $this->customHydrationModes[$modeName] ?? null;
}
/**
* Adds a custom hydration mode.
*/
public function addCustomHydrationMode(string $modeName, string $hydratorClassName) : void
{
$this->customHydrationModes[$modeName] = $hydratorClassName;
}
/**
* Sets a class metadata factory.
*/
public function setClassMetadataFactoryName(string $classMetadataFactoryClassName) : void
{
$this->classMetadataFactoryClassName = $classMetadataFactoryClassName;
}
public function getClassMetadataFactoryName() : string
{
return $this->classMetadataFactoryClassName;
}
/**
* Adds a filter to the list of possible filters.
*/
public function addFilter(string $filterName, string $filterClassName) : void
{
$this->filters[$filterName] = $filterClassName;
}
/**
* Gets the class name for a given filter name.
*
* @return string|null The class name of the filter
*/
public function getFilterClassName(string $filterName) : ?string
{
return $this->filters[$filterName] ?? null;
}
/**
* Sets default repository class.
*
* @throws ORMException If not is a \Doctrine\Common\Persistence\ObjectRepository implementation.
*/
public function setDefaultRepositoryClassName(string $repositoryClassName) : void
{
$reflectionClass = new ReflectionClass($repositoryClassName);
if (! $reflectionClass->implementsInterface(ObjectRepository::class)) {
throw InvalidEntityRepository::fromClassName($repositoryClassName);
}
$this->defaultRepositoryClassName = $repositoryClassName;
}
/**
* Get default repository class.
*/
public function getDefaultRepositoryClassName() : string
{
return $this->defaultRepositoryClassName;
}
/**
* Sets naming strategy.
*/
public function setNamingStrategy(NamingStrategy $namingStrategy) : void
{
$this->namingStrategy = $namingStrategy;
}
/**
* Gets naming strategy..
*/
public function getNamingStrategy() : ?NamingStrategy
{
return $this->namingStrategy
?? $this->namingStrategy = new DefaultNamingStrategy();
}
/**
* Set the entity listener resolver.
*/
public function setEntityListenerResolver(EntityListenerResolver $resolver) : void
{
$this->entityListenerResolver = $resolver;
}
/**
* Get the entity listener resolver.
*/
public function getEntityListenerResolver() : EntityListenerResolver
{
return $this->entityListenerResolver
?? $this->entityListenerResolver = new DefaultEntityListenerResolver();
}
/**
* Set the entity repository factory.
*/
public function setRepositoryFactory(RepositoryFactory $repositoryFactory) : void
{
$this->repositoryFactory = $repositoryFactory;
}
/**
* Get the entity repository factory.
*/
public function getRepositoryFactory() : RepositoryFactory
{
return $this->repositoryFactory
?? $this->repositoryFactory = new DefaultRepositoryFactory();
}
public function isSecondLevelCacheEnabled() : bool
{
return $this->isSecondLevelCacheEnabled;
}
public function setSecondLevelCacheEnabled(bool $flag = true) : void
{
$this->isSecondLevelCacheEnabled = $flag;
}
public function setSecondLevelCacheConfiguration(CacheConfiguration $cacheConfig) : void
{
$this->secondLevelCacheConfiguration = $cacheConfig;
}
public function getSecondLevelCacheConfiguration() : ?CacheConfiguration
{
if ($this->isSecondLevelCacheEnabled && ! $this->secondLevelCacheConfiguration) {
$this->secondLevelCacheConfiguration = new CacheConfiguration();
}
return $this->secondLevelCacheConfiguration;
}
/**
* Returns query hints, which will be applied to every query in application
*
* @return mixed[]
*/
public function getDefaultQueryHints() : array
{
return $this->defaultQueryHints;
}
/**
* Sets array of query hints, which will be applied to every query in application
*
* @param mixed[] $defaultQueryHints
*/
public function setDefaultQueryHints(array $defaultQueryHints) : void
{
$this->defaultQueryHints = $defaultQueryHints;
}
/**
* Gets the value of a default query hint. If the hint name is not recognized, FALSE is returned.
*
* @return mixed The value of the hint or FALSE, if the hint name is not recognized.
*/
public function getDefaultQueryHint(string $hintName)
{
return $this->defaultQueryHints[$hintName] ?? false;
}
/**
* Sets a default query hint. If the hint name is not recognized, it is silently ignored.
*
* @param mixed $value The value of the hint.
*/
public function setDefaultQueryHint(string $hintName, $value) : void
{
$this->defaultQueryHints[$hintName] = $value;
}
public function buildGhostObjectFactory() : LazyLoadingGhostFactory
{
return new LazyLoadingGhostFactory(clone $this->getProxyManagerConfiguration());
}
public function getProxyManagerConfiguration() : ProxyManagerConfiguration
{
return $this->proxyManagerConfiguration
?? $this->proxyManagerConfiguration = new ProxyManagerConfiguration();
}
}