1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineORMModule\Service; |
21
|
|
|
|
22
|
|
|
use Doctrine\ORM\Cache\CacheConfiguration; |
23
|
|
|
use Doctrine\ORM\Cache\DefaultCacheFactory; |
24
|
|
|
use Doctrine\ORM\Cache\RegionsConfiguration; |
25
|
|
|
use Doctrine\ORM\Mapping\EntityListenerResolver; |
26
|
|
|
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration; |
27
|
|
|
use DoctrineORMModule\Service\DBALConfigurationFactory as DoctrineConfigurationFactory; |
28
|
|
|
use Interop\Container\ContainerInterface; |
29
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
30
|
|
|
use Zend\ServiceManager\Exception\InvalidArgumentException; |
31
|
|
|
use Doctrine\ORM\Configuration; |
32
|
|
|
|
33
|
|
|
class ConfigurationFactory extends DoctrineConfigurationFactory |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
* |
38
|
|
|
* @return Configuration |
39
|
|
|
*/ |
40
|
88 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
41
|
|
|
{ |
42
|
|
|
/** @var $options DoctrineORMModuleConfiguration */ |
43
|
88 |
|
$options = $this->getOptions($container); |
44
|
88 |
|
$config = new Configuration(); |
45
|
|
|
|
46
|
88 |
|
$config->setAutoGenerateProxyClasses($options->getGenerateProxies()); |
47
|
88 |
|
$config->setProxyDir($options->getProxyDir()); |
48
|
88 |
|
$config->setProxyNamespace($options->getProxyNamespace()); |
49
|
|
|
|
50
|
88 |
|
$config->setEntityNamespaces($options->getEntityNamespaces()); |
51
|
|
|
|
52
|
88 |
|
$config->setCustomDatetimeFunctions($options->getDatetimeFunctions()); |
53
|
88 |
|
$config->setCustomStringFunctions($options->getStringFunctions()); |
54
|
88 |
|
$config->setCustomNumericFunctions($options->getNumericFunctions()); |
55
|
|
|
|
56
|
88 |
|
$config->setClassMetadataFactoryName($options->getClassMetadataFactoryName()); |
57
|
|
|
|
58
|
88 |
|
foreach ($options->getNamedQueries() as $name => $query) { |
59
|
|
|
$config->addNamedQuery($name, $query); |
60
|
88 |
|
} |
61
|
|
|
|
62
|
88 |
|
foreach ($options->getNamedNativeQueries() as $name => $query) { |
63
|
|
|
$config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']); |
64
|
88 |
|
} |
65
|
|
|
|
66
|
88 |
|
foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) { |
67
|
|
|
$config->addCustomHydrationMode($modeName, $hydrator); |
68
|
88 |
|
} |
69
|
|
|
|
70
|
88 |
|
foreach ($options->getFilters() as $name => $class) { |
71
|
|
|
$config->addFilter($name, $class); |
72
|
88 |
|
} |
73
|
|
|
|
74
|
88 |
|
$config->setMetadataCacheImpl($container->get($options->getMetadataCache())); |
75
|
88 |
|
$config->setQueryCacheImpl($container->get($options->getQueryCache())); |
76
|
88 |
|
$config->setResultCacheImpl($container->get($options->getResultCache())); |
77
|
88 |
|
$config->setHydrationCacheImpl($container->get($options->getHydrationCache())); |
78
|
88 |
|
$config->setMetadataDriverImpl($container->get($options->getDriver())); |
79
|
|
|
|
80
|
88 |
|
if ($namingStrategy = $options->getNamingStrategy()) { |
81
|
87 |
|
if (is_string($namingStrategy)) { |
82
|
2 |
|
if (! $container->has($namingStrategy)) { |
83
|
87 |
|
throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy)); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$config->setNamingStrategy($container->get($namingStrategy)); |
87
|
1 |
|
} else { |
88
|
1 |
|
$config->setNamingStrategy($namingStrategy); |
89
|
86 |
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
87 |
|
if ($quoteStrategy = $options->getQuoteStrategy()) { |
93
|
3 |
|
if (is_string($quoteStrategy)) { |
94
|
2 |
|
if (! $container->has($quoteStrategy)) { |
95
|
1 |
|
throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy)); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$config->setQuoteStrategy($container->get($quoteStrategy)); |
99
|
1 |
|
} else { |
100
|
1 |
|
$config->setQuoteStrategy($quoteStrategy); |
101
|
|
|
} |
102
|
2 |
|
} |
103
|
|
|
|
104
|
86 |
|
if ($repositoryFactory = $options->getRepositoryFactory()) { |
105
|
|
|
if (is_string($repositoryFactory)) { |
106
|
|
|
if (! $container->has($repositoryFactory)) { |
107
|
|
|
throw new InvalidArgumentException( |
108
|
|
|
sprintf('Repository factory "%s" not found', $repositoryFactory) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$config->setRepositoryFactory($container->get($repositoryFactory)); |
113
|
|
|
} else { |
114
|
|
|
$config->setRepositoryFactory($repositoryFactory); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
86 |
|
if ($entityListenerResolver = $options->getEntityListenerResolver()) { |
119
|
2 |
|
if ($entityListenerResolver instanceof EntityListenerResolver) { |
120
|
2 |
|
$config->setEntityListenerResolver($entityListenerResolver); |
121
|
1 |
|
} else { |
122
|
1 |
|
$config->setEntityListenerResolver($container->get($entityListenerResolver)); |
123
|
|
|
} |
124
|
2 |
|
} |
125
|
|
|
|
126
|
86 |
|
$secondLevelCache = $options->getSecondLevelCache(); |
127
|
|
|
|
128
|
86 |
|
if ($secondLevelCache->isEnabled()) { |
129
|
1 |
|
$regionsConfig = new RegionsConfiguration( |
130
|
1 |
|
$secondLevelCache->getDefaultLifetime(), |
131
|
1 |
|
$secondLevelCache->getDefaultLockLifetime() |
132
|
1 |
|
); |
133
|
|
|
|
134
|
1 |
|
foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) { |
135
|
1 |
|
if (isset($regionConfig['lifetime'])) { |
136
|
1 |
|
$regionsConfig->setLifetime($regionName, $regionConfig['lifetime']); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
if (isset($regionConfig['lock_lifetime'])) { |
140
|
1 |
|
$regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']); |
141
|
1 |
|
} |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
// As Second Level Cache caches queries results, we reuse the result cache impl |
145
|
1 |
|
$cacheFactory = new DefaultCacheFactory($regionsConfig, $config->getResultCacheImpl()); |
|
|
|
|
146
|
1 |
|
$cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory()); |
147
|
|
|
|
148
|
1 |
|
$cacheConfiguration = new CacheConfiguration(); |
149
|
1 |
|
$cacheConfiguration->setCacheFactory($cacheFactory); |
150
|
1 |
|
$cacheConfiguration->setRegionsConfiguration($regionsConfig); |
151
|
|
|
|
152
|
1 |
|
$config->setSecondLevelCacheEnabled(); |
153
|
1 |
|
$config->setSecondLevelCacheConfiguration($cacheConfiguration); |
154
|
1 |
|
} |
155
|
|
|
|
156
|
86 |
|
if($schemaFilter = $options->getSchemaFilter()) { |
|
|
|
|
157
|
|
|
$config->setFilterSchemaAssetsExpression($schemaFilter); |
158
|
|
|
} |
159
|
|
|
|
160
|
86 |
|
if ($className = $options->getDefaultRepositoryClassName()) { |
161
|
73 |
|
$config->setDefaultRepositoryClassName($className); |
162
|
73 |
|
} |
163
|
|
|
|
164
|
86 |
|
$this->setupDBALConfiguration($container, $config); |
165
|
|
|
|
166
|
86 |
|
return $config; |
167
|
|
|
} |
168
|
|
|
|
169
|
88 |
|
public function createService(ServiceLocatorInterface $container) |
170
|
|
|
{ |
171
|
88 |
|
return $this($container, Configuration::class); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
88 |
|
protected function getOptionsClass() |
178
|
|
|
{ |
179
|
88 |
|
return DoctrineORMModuleConfiguration::class; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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: