1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineORMModule\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Cache\CacheConfiguration; |
6
|
|
|
use Doctrine\ORM\Cache\DefaultCacheFactory; |
7
|
|
|
use Doctrine\ORM\Cache\RegionsConfiguration; |
8
|
|
|
use Doctrine\ORM\Mapping\EntityListenerResolver; |
9
|
|
|
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration; |
10
|
|
|
use DoctrineORMModule\Service\DBALConfigurationFactory as DoctrineConfigurationFactory; |
11
|
|
|
use Interop\Container\ContainerInterface; |
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
13
|
|
|
use Zend\ServiceManager\Exception\InvalidArgumentException; |
14
|
|
|
use Doctrine\ORM\Configuration; |
15
|
|
|
|
16
|
|
|
class ConfigurationFactory extends DoctrineConfigurationFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
* |
21
|
|
|
* @return Configuration |
22
|
|
|
*/ |
23
|
88 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
24
|
|
|
{ |
25
|
|
|
/** @var $options DoctrineORMModuleConfiguration */ |
26
|
88 |
|
$options = $this->getOptions($container); |
27
|
88 |
|
$config = new Configuration(); |
28
|
|
|
|
29
|
88 |
|
$config->setAutoGenerateProxyClasses($options->getGenerateProxies()); |
30
|
88 |
|
$config->setProxyDir($options->getProxyDir()); |
31
|
88 |
|
$config->setProxyNamespace($options->getProxyNamespace()); |
32
|
|
|
|
33
|
88 |
|
$config->setEntityNamespaces($options->getEntityNamespaces()); |
34
|
|
|
|
35
|
88 |
|
$config->setCustomDatetimeFunctions($options->getDatetimeFunctions()); |
36
|
88 |
|
$config->setCustomStringFunctions($options->getStringFunctions()); |
37
|
88 |
|
$config->setCustomNumericFunctions($options->getNumericFunctions()); |
38
|
|
|
|
39
|
88 |
|
$config->setClassMetadataFactoryName($options->getClassMetadataFactoryName()); |
40
|
|
|
|
41
|
88 |
|
foreach ($options->getNamedQueries() as $name => $query) { |
42
|
|
|
$config->addNamedQuery($name, $query); |
43
|
88 |
|
} |
44
|
|
|
|
45
|
88 |
|
foreach ($options->getNamedNativeQueries() as $name => $query) { |
46
|
|
|
$config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']); |
47
|
88 |
|
} |
48
|
|
|
|
49
|
88 |
|
foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) { |
50
|
|
|
$config->addCustomHydrationMode($modeName, $hydrator); |
51
|
88 |
|
} |
52
|
|
|
|
53
|
88 |
|
foreach ($options->getFilters() as $name => $class) { |
54
|
|
|
$config->addFilter($name, $class); |
55
|
88 |
|
} |
56
|
|
|
|
57
|
88 |
|
$config->setMetadataCacheImpl($container->get($options->getMetadataCache())); |
58
|
88 |
|
$config->setQueryCacheImpl($container->get($options->getQueryCache())); |
59
|
88 |
|
$config->setResultCacheImpl($container->get($options->getResultCache())); |
60
|
88 |
|
$config->setHydrationCacheImpl($container->get($options->getHydrationCache())); |
61
|
88 |
|
$config->setMetadataDriverImpl($container->get($options->getDriver())); |
62
|
|
|
|
63
|
88 |
|
if ($namingStrategy = $options->getNamingStrategy()) { |
64
|
87 |
|
if (is_string($namingStrategy)) { |
65
|
2 |
|
if (! $container->has($namingStrategy)) { |
66
|
87 |
|
throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy)); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$config->setNamingStrategy($container->get($namingStrategy)); |
70
|
1 |
|
} else { |
71
|
1 |
|
$config->setNamingStrategy($namingStrategy); |
72
|
86 |
|
} |
73
|
2 |
|
} |
74
|
|
|
|
75
|
87 |
|
if ($quoteStrategy = $options->getQuoteStrategy()) { |
76
|
3 |
|
if (is_string($quoteStrategy)) { |
77
|
2 |
|
if (! $container->has($quoteStrategy)) { |
78
|
1 |
|
throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy)); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$config->setQuoteStrategy($container->get($quoteStrategy)); |
82
|
1 |
|
} else { |
83
|
1 |
|
$config->setQuoteStrategy($quoteStrategy); |
84
|
|
|
} |
85
|
2 |
|
} |
86
|
|
|
|
87
|
86 |
|
if ($repositoryFactory = $options->getRepositoryFactory()) { |
88
|
|
|
if (is_string($repositoryFactory)) { |
89
|
|
|
if (! $container->has($repositoryFactory)) { |
90
|
|
|
throw new InvalidArgumentException( |
91
|
|
|
sprintf('Repository factory "%s" not found', $repositoryFactory) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$config->setRepositoryFactory($container->get($repositoryFactory)); |
96
|
|
|
} else { |
97
|
|
|
$config->setRepositoryFactory($repositoryFactory); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
86 |
|
if ($entityListenerResolver = $options->getEntityListenerResolver()) { |
102
|
2 |
|
if ($entityListenerResolver instanceof EntityListenerResolver) { |
103
|
2 |
|
$config->setEntityListenerResolver($entityListenerResolver); |
104
|
1 |
|
} else { |
105
|
1 |
|
$config->setEntityListenerResolver($container->get($entityListenerResolver)); |
106
|
|
|
} |
107
|
2 |
|
} |
108
|
|
|
|
109
|
86 |
|
$secondLevelCache = $options->getSecondLevelCache(); |
110
|
|
|
|
111
|
86 |
|
if ($secondLevelCache->isEnabled()) { |
112
|
1 |
|
$regionsConfig = new RegionsConfiguration( |
113
|
1 |
|
$secondLevelCache->getDefaultLifetime(), |
114
|
1 |
|
$secondLevelCache->getDefaultLockLifetime() |
115
|
1 |
|
); |
116
|
|
|
|
117
|
1 |
|
foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) { |
118
|
1 |
|
if (isset($regionConfig['lifetime'])) { |
119
|
1 |
|
$regionsConfig->setLifetime($regionName, $regionConfig['lifetime']); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
if (isset($regionConfig['lock_lifetime'])) { |
123
|
1 |
|
$regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']); |
124
|
1 |
|
} |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
// As Second Level Cache caches queries results, we reuse the result cache impl |
128
|
1 |
|
$cacheFactory = new DefaultCacheFactory($regionsConfig, $config->getResultCacheImpl()); |
|
|
|
|
129
|
1 |
|
$cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory()); |
130
|
|
|
|
131
|
1 |
|
$cacheConfiguration = new CacheConfiguration(); |
132
|
1 |
|
$cacheConfiguration->setCacheFactory($cacheFactory); |
133
|
1 |
|
$cacheConfiguration->setRegionsConfiguration($regionsConfig); |
134
|
|
|
|
135
|
1 |
|
$config->setSecondLevelCacheEnabled(); |
136
|
1 |
|
$config->setSecondLevelCacheConfiguration($cacheConfiguration); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
86 |
|
if($filterSchemaAssetsExpression = $options->getFilterSchemaAssetsExpression()) { |
|
|
|
|
140
|
|
|
$config->setFilterSchemaAssetsExpression($filterSchemaAssetsExpression); |
141
|
|
|
} |
142
|
|
|
|
143
|
86 |
|
if ($className = $options->getDefaultRepositoryClassName()) { |
144
|
73 |
|
$config->setDefaultRepositoryClassName($className); |
145
|
73 |
|
} |
146
|
|
|
|
147
|
86 |
|
$this->setupDBALConfiguration($container, $config); |
148
|
|
|
|
149
|
86 |
|
return $config; |
150
|
|
|
} |
151
|
|
|
|
152
|
88 |
|
public function createService(ServiceLocatorInterface $container) |
153
|
|
|
{ |
154
|
88 |
|
return $this($container, Configuration::class); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
88 |
|
protected function getOptionsClass() |
161
|
|
|
{ |
162
|
88 |
|
return DoctrineORMModuleConfiguration::class; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: