1 | <?php |
||
32 | class ConfigurationFactory extends DoctrineConfigurationFactory |
||
33 | { |
||
34 | /** |
||
35 | * {@inheritDoc} |
||
36 | * |
||
37 | * @return Configuration |
||
38 | */ |
||
39 | 72 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
40 | { |
||
41 | /** @var $options \DoctrineORMModule\Options\Configuration */ |
||
42 | 72 | $options = $this->getOptions($container); |
|
43 | 72 | $config = new Configuration(); |
|
44 | |||
45 | 72 | $config->setAutoGenerateProxyClasses($options->getGenerateProxies()); |
|
46 | 72 | $config->setProxyDir($options->getProxyDir()); |
|
47 | 72 | $config->setProxyNamespace($options->getProxyNamespace()); |
|
48 | |||
49 | 72 | $config->setEntityNamespaces($options->getEntityNamespaces()); |
|
50 | |||
51 | 72 | $config->setCustomDatetimeFunctions($options->getDatetimeFunctions()); |
|
52 | 72 | $config->setCustomStringFunctions($options->getStringFunctions()); |
|
53 | 72 | $config->setCustomNumericFunctions($options->getNumericFunctions()); |
|
54 | |||
55 | 72 | $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName()); |
|
56 | |||
57 | 72 | foreach ($options->getNamedQueries() as $name => $query) { |
|
58 | $config->addNamedQuery($name, $query); |
||
59 | 72 | } |
|
60 | |||
61 | 72 | foreach ($options->getNamedNativeQueries() as $name => $query) { |
|
62 | $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']); |
||
63 | 72 | } |
|
64 | |||
65 | 72 | foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) { |
|
66 | $config->addCustomHydrationMode($modeName, $hydrator); |
||
67 | 72 | } |
|
68 | |||
69 | 72 | foreach ($options->getFilters() as $name => $class) { |
|
70 | $config->addFilter($name, $class); |
||
71 | 72 | } |
|
72 | |||
73 | 72 | $config->setMetadataCacheImpl($container->get($options->getMetadataCache())); |
|
74 | 72 | $config->setQueryCacheImpl($container->get($options->getQueryCache())); |
|
75 | 72 | $config->setResultCacheImpl($container->get($options->getResultCache())); |
|
76 | 72 | $config->setHydrationCacheImpl($container->get($options->getHydrationCache())); |
|
77 | 72 | $config->setMetadataDriverImpl($container->get($options->getDriver())); |
|
78 | |||
79 | 72 | if ($namingStrategy = $options->getNamingStrategy()) { |
|
80 | 71 | if (is_string($namingStrategy)) { |
|
81 | 2 | if (!$container->has($namingStrategy)) { |
|
82 | 71 | throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy)); |
|
83 | } |
||
84 | |||
85 | 1 | $config->setNamingStrategy($container->get($namingStrategy)); |
|
86 | 1 | } else { |
|
87 | 1 | $config->setNamingStrategy($namingStrategy); |
|
88 | 70 | } |
|
89 | 2 | } |
|
90 | |||
91 | 71 | if ($quoteStrategy = $options->getQuoteStrategy()) { |
|
92 | 3 | if (is_string($quoteStrategy)) { |
|
93 | 2 | if (!$container->has($quoteStrategy)) { |
|
94 | 1 | throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy)); |
|
95 | } |
||
96 | |||
97 | 1 | $config->setQuoteStrategy($container->get($quoteStrategy)); |
|
98 | 1 | } else { |
|
99 | 1 | $config->setQuoteStrategy($quoteStrategy); |
|
100 | } |
||
101 | 2 | } |
|
102 | |||
103 | 70 | if ($repositoryFactory = $options->getRepositoryFactory()) { |
|
104 | if (is_string($repositoryFactory)) { |
||
105 | if (!$container->has($repositoryFactory)) { |
||
106 | throw new InvalidArgumentException( |
||
107 | sprintf('Repository factory "%s" not found', $repositoryFactory) |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | $config->setRepositoryFactory($container->get($repositoryFactory)); |
||
112 | } else { |
||
113 | $config->setRepositoryFactory($repositoryFactory); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | 70 | if ($entityListenerResolver = $options->getEntityListenerResolver()) { |
|
118 | 2 | if ($entityListenerResolver instanceof EntityListenerResolver) { |
|
119 | 2 | $config->setEntityListenerResolver($entityListenerResolver); |
|
120 | 1 | } else { |
|
121 | 1 | $config->setEntityListenerResolver($container->get($entityListenerResolver)); |
|
122 | } |
||
123 | 2 | } |
|
124 | |||
125 | 70 | $secondLevelCache = $options->getSecondLevelCache(); |
|
126 | |||
127 | 70 | if ($secondLevelCache->isEnabled()) { |
|
128 | 1 | $regionsConfig = new RegionsConfiguration( |
|
129 | 1 | $secondLevelCache->getDefaultLifetime(), |
|
130 | 1 | $secondLevelCache->getDefaultLockLifetime() |
|
131 | 1 | ); |
|
132 | |||
133 | 1 | foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) { |
|
134 | 1 | if (isset($regionConfig['lifetime'])) { |
|
135 | 1 | $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']); |
|
136 | 1 | } |
|
137 | |||
138 | 1 | if (isset($regionConfig['lock_lifetime'])) { |
|
139 | 1 | $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']); |
|
140 | 1 | } |
|
141 | 1 | } |
|
142 | |||
143 | // As Second Level Cache caches queries results, we reuse the result cache impl |
||
144 | 1 | $cacheFactory = new DefaultCacheFactory($regionsConfig, $config->getResultCacheImpl()); |
|
|
|||
145 | 1 | $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory()); |
|
146 | |||
147 | 1 | $cacheConfiguration = new CacheConfiguration(); |
|
148 | 1 | $cacheConfiguration->setCacheFactory($cacheFactory); |
|
149 | 1 | $cacheConfiguration->setRegionsConfiguration($regionsConfig); |
|
150 | |||
151 | 1 | $config->setSecondLevelCacheEnabled(); |
|
152 | 1 | $config->setSecondLevelCacheConfiguration($cacheConfiguration); |
|
153 | 1 | } |
|
154 | |||
155 | 70 | if ($className = $options->getDefaultRepositoryClassName()) { |
|
156 | 57 | $config->setDefaultRepositoryClassName($className); |
|
157 | 57 | } |
|
158 | |||
159 | 70 | $this->setupDBALConfiguration($container, $config); |
|
160 | |||
161 | 70 | return $config; |
|
162 | } |
||
163 | |||
164 | 72 | public function createService(ServiceLocatorInterface $container) |
|
165 | { |
||
166 | 72 | return $this($container, Configuration::class); |
|
167 | } |
||
168 | |||
169 | 72 | protected function getOptionsClass() |
|
173 | } |
||
174 |
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: