|
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 LGPL. For more information, see |
|
17 * <http://www.doctrine-project.org>. |
|
18 */ |
|
19 |
|
20 namespace Doctrine\ORM; |
|
21 |
|
22 use Doctrine\Common\Cache\Cache, |
|
23 Doctrine\Common\Cache\ArrayCache, |
|
24 Doctrine\Common\Annotations\AnnotationRegistry, |
|
25 Doctrine\Common\Annotations\AnnotationReader, |
|
26 Doctrine\ORM\Mapping\Driver\Driver, |
|
27 Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
|
28 |
|
29 /** |
|
30 * Configuration container for all configuration options of Doctrine. |
|
31 * It combines all configuration options from DBAL & ORM. |
|
32 * |
|
33 * @since 2.0 |
|
34 * @internal When adding a new configuration option just write a getter/setter pair. |
|
35 * @author Benjamin Eberlei <kontakt@beberlei.de> |
|
36 * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
|
37 * @author Jonathan Wage <jonwage@gmail.com> |
|
38 * @author Roman Borschel <roman@code-factory.org> |
|
39 */ |
|
40 class Configuration extends \Doctrine\DBAL\Configuration |
|
41 { |
|
42 /** |
|
43 * Sets the directory where Doctrine generates any necessary proxy class files. |
|
44 * |
|
45 * @param string $dir |
|
46 */ |
|
47 public function setProxyDir($dir) |
|
48 { |
|
49 $this->_attributes['proxyDir'] = $dir; |
|
50 } |
|
51 |
|
52 /** |
|
53 * Gets the directory where Doctrine generates any necessary proxy class files. |
|
54 * |
|
55 * @return string |
|
56 */ |
|
57 public function getProxyDir() |
|
58 { |
|
59 return isset($this->_attributes['proxyDir']) ? |
|
60 $this->_attributes['proxyDir'] : null; |
|
61 } |
|
62 |
|
63 /** |
|
64 * Gets a boolean flag that indicates whether proxy classes should always be regenerated |
|
65 * during each script execution. |
|
66 * |
|
67 * @return boolean |
|
68 */ |
|
69 public function getAutoGenerateProxyClasses() |
|
70 { |
|
71 return isset($this->_attributes['autoGenerateProxyClasses']) ? |
|
72 $this->_attributes['autoGenerateProxyClasses'] : true; |
|
73 } |
|
74 |
|
75 /** |
|
76 * Sets a boolean flag that indicates whether proxy classes should always be regenerated |
|
77 * during each script execution. |
|
78 * |
|
79 * @param boolean $bool |
|
80 */ |
|
81 public function setAutoGenerateProxyClasses($bool) |
|
82 { |
|
83 $this->_attributes['autoGenerateProxyClasses'] = $bool; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Gets the namespace where proxy classes reside. |
|
88 * |
|
89 * @return string |
|
90 */ |
|
91 public function getProxyNamespace() |
|
92 { |
|
93 return isset($this->_attributes['proxyNamespace']) ? |
|
94 $this->_attributes['proxyNamespace'] : null; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Sets the namespace where proxy classes reside. |
|
99 * |
|
100 * @param string $ns |
|
101 */ |
|
102 public function setProxyNamespace($ns) |
|
103 { |
|
104 $this->_attributes['proxyNamespace'] = $ns; |
|
105 } |
|
106 |
|
107 /** |
|
108 * Sets the cache driver implementation that is used for metadata caching. |
|
109 * |
|
110 * @param Driver $driverImpl |
|
111 * @todo Force parameter to be a Closure to ensure lazy evaluation |
|
112 * (as soon as a metadata cache is in effect, the driver never needs to initialize). |
|
113 */ |
|
114 public function setMetadataDriverImpl(Driver $driverImpl) |
|
115 { |
|
116 $this->_attributes['metadataDriverImpl'] = $driverImpl; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Add a new default annotation driver with a correctly configured annotation reader. |
|
121 * |
|
122 * @param array $paths |
|
123 * @return Mapping\Driver\AnnotationDriver |
|
124 */ |
|
125 public function newDefaultAnnotationDriver($paths = array()) |
|
126 { |
|
127 if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0-DEV', '>=')) { |
|
128 // Register the ORM Annotations in the AnnotationRegistry |
|
129 AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php'); |
|
130 |
|
131 $reader = new AnnotationReader(); |
|
132 $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache()); |
|
133 } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-DEV', '>=')) { |
|
134 // Register the ORM Annotations in the AnnotationRegistry |
|
135 AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php'); |
|
136 |
|
137 $reader = new AnnotationReader(); |
|
138 $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); |
|
139 $reader->setIgnoreNotImportedAnnotations(true); |
|
140 $reader->setEnableParsePhpImports(false); |
|
141 $reader = new \Doctrine\Common\Annotations\CachedReader( |
|
142 new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache() |
|
143 ); |
|
144 } else { |
|
145 $reader = new AnnotationReader(); |
|
146 $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); |
|
147 } |
|
148 return new AnnotationDriver($reader, (array)$paths); |
|
149 } |
|
150 |
|
151 /** |
|
152 * Adds a namespace under a certain alias. |
|
153 * |
|
154 * @param string $alias |
|
155 * @param string $namespace |
|
156 */ |
|
157 public function addEntityNamespace($alias, $namespace) |
|
158 { |
|
159 $this->_attributes['entityNamespaces'][$alias] = $namespace; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Resolves a registered namespace alias to the full namespace. |
|
164 * |
|
165 * @param string $entityNamespaceAlias |
|
166 * @return string |
|
167 * @throws MappingException |
|
168 */ |
|
169 public function getEntityNamespace($entityNamespaceAlias) |
|
170 { |
|
171 if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) { |
|
172 throw ORMException::unknownEntityNamespace($entityNamespaceAlias); |
|
173 } |
|
174 |
|
175 return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\'); |
|
176 } |
|
177 |
|
178 /** |
|
179 * Set the entity alias map |
|
180 * |
|
181 * @param array $entityAliasMap |
|
182 * @return void |
|
183 */ |
|
184 public function setEntityNamespaces(array $entityNamespaces) |
|
185 { |
|
186 $this->_attributes['entityNamespaces'] = $entityNamespaces; |
|
187 } |
|
188 |
|
189 /** |
|
190 * Retrieves the list of registered entity namespace aliases. |
|
191 * |
|
192 * @return array |
|
193 */ |
|
194 public function getEntityNamespaces() |
|
195 { |
|
196 return $this->_attributes['entityNamespaces']; |
|
197 } |
|
198 |
|
199 /** |
|
200 * Gets the cache driver implementation that is used for the mapping metadata. |
|
201 * |
|
202 * @throws ORMException |
|
203 * @return Mapping\Driver\Driver |
|
204 */ |
|
205 public function getMetadataDriverImpl() |
|
206 { |
|
207 return isset($this->_attributes['metadataDriverImpl']) ? |
|
208 $this->_attributes['metadataDriverImpl'] : null; |
|
209 } |
|
210 |
|
211 /** |
|
212 * Gets the cache driver implementation that is used for query result caching. |
|
213 * |
|
214 * @return \Doctrine\Common\Cache\Cache |
|
215 */ |
|
216 public function getResultCacheImpl() |
|
217 { |
|
218 return isset($this->_attributes['resultCacheImpl']) ? |
|
219 $this->_attributes['resultCacheImpl'] : null; |
|
220 } |
|
221 |
|
222 /** |
|
223 * Sets the cache driver implementation that is used for query result caching. |
|
224 * |
|
225 * @param \Doctrine\Common\Cache\Cache $cacheImpl |
|
226 */ |
|
227 public function setResultCacheImpl(Cache $cacheImpl) |
|
228 { |
|
229 $this->_attributes['resultCacheImpl'] = $cacheImpl; |
|
230 } |
|
231 |
|
232 /** |
|
233 * Gets the cache driver implementation that is used for the query cache (SQL cache). |
|
234 * |
|
235 * @return \Doctrine\Common\Cache\Cache |
|
236 */ |
|
237 public function getQueryCacheImpl() |
|
238 { |
|
239 return isset($this->_attributes['queryCacheImpl']) ? |
|
240 $this->_attributes['queryCacheImpl'] : null; |
|
241 } |
|
242 |
|
243 /** |
|
244 * Sets the cache driver implementation that is used for the query cache (SQL cache). |
|
245 * |
|
246 * @param \Doctrine\Common\Cache\Cache $cacheImpl |
|
247 */ |
|
248 public function setQueryCacheImpl(Cache $cacheImpl) |
|
249 { |
|
250 $this->_attributes['queryCacheImpl'] = $cacheImpl; |
|
251 } |
|
252 |
|
253 /** |
|
254 * Gets the cache driver implementation that is used for metadata caching. |
|
255 * |
|
256 * @return \Doctrine\Common\Cache\Cache |
|
257 */ |
|
258 public function getMetadataCacheImpl() |
|
259 { |
|
260 return isset($this->_attributes['metadataCacheImpl']) ? |
|
261 $this->_attributes['metadataCacheImpl'] : null; |
|
262 } |
|
263 |
|
264 /** |
|
265 * Sets the cache driver implementation that is used for metadata caching. |
|
266 * |
|
267 * @param \Doctrine\Common\Cache\Cache $cacheImpl |
|
268 */ |
|
269 public function setMetadataCacheImpl(Cache $cacheImpl) |
|
270 { |
|
271 $this->_attributes['metadataCacheImpl'] = $cacheImpl; |
|
272 } |
|
273 |
|
274 /** |
|
275 * Adds a named DQL query to the configuration. |
|
276 * |
|
277 * @param string $name The name of the query. |
|
278 * @param string $dql The DQL query string. |
|
279 */ |
|
280 public function addNamedQuery($name, $dql) |
|
281 { |
|
282 $this->_attributes['namedQueries'][$name] = $dql; |
|
283 } |
|
284 |
|
285 /** |
|
286 * Gets a previously registered named DQL query. |
|
287 * |
|
288 * @param string $name The name of the query. |
|
289 * @return string The DQL query. |
|
290 */ |
|
291 public function getNamedQuery($name) |
|
292 { |
|
293 if ( ! isset($this->_attributes['namedQueries'][$name])) { |
|
294 throw ORMException::namedQueryNotFound($name); |
|
295 } |
|
296 return $this->_attributes['namedQueries'][$name]; |
|
297 } |
|
298 |
|
299 /** |
|
300 * Adds a named native query to the configuration. |
|
301 * |
|
302 * @param string $name The name of the query. |
|
303 * @param string $sql The native SQL query string. |
|
304 * @param ResultSetMapping $rsm The ResultSetMapping used for the results of the SQL query. |
|
305 */ |
|
306 public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm) |
|
307 { |
|
308 $this->_attributes['namedNativeQueries'][$name] = array($sql, $rsm); |
|
309 } |
|
310 |
|
311 /** |
|
312 * Gets the components of a previously registered named native query. |
|
313 * |
|
314 * @param string $name The name of the query. |
|
315 * @return array A tuple with the first element being the SQL string and the second |
|
316 * element being the ResultSetMapping. |
|
317 */ |
|
318 public function getNamedNativeQuery($name) |
|
319 { |
|
320 if ( ! isset($this->_attributes['namedNativeQueries'][$name])) { |
|
321 throw ORMException::namedNativeQueryNotFound($name); |
|
322 } |
|
323 return $this->_attributes['namedNativeQueries'][$name]; |
|
324 } |
|
325 |
|
326 /** |
|
327 * Ensures that this Configuration instance contains settings that are |
|
328 * suitable for a production environment. |
|
329 * |
|
330 * @throws ORMException If a configuration setting has a value that is not |
|
331 * suitable for a production environment. |
|
332 */ |
|
333 public function ensureProductionSettings() |
|
334 { |
|
335 if ( !$this->getQueryCacheImpl()) { |
|
336 throw ORMException::queryCacheNotConfigured(); |
|
337 } |
|
338 if ( !$this->getMetadataCacheImpl()) { |
|
339 throw ORMException::metadataCacheNotConfigured(); |
|
340 } |
|
341 if ($this->getAutoGenerateProxyClasses()) { |
|
342 throw ORMException::proxyClassesAlwaysRegenerating(); |
|
343 } |
|
344 } |
|
345 |
|
346 /** |
|
347 * Registers a custom DQL function that produces a string value. |
|
348 * Such a function can then be used in any DQL statement in any place where string |
|
349 * functions are allowed. |
|
350 * |
|
351 * DQL function names are case-insensitive. |
|
352 * |
|
353 * @param string $name |
|
354 * @param string $className |
|
355 */ |
|
356 public function addCustomStringFunction($name, $className) |
|
357 { |
|
358 $this->_attributes['customStringFunctions'][strtolower($name)] = $className; |
|
359 } |
|
360 |
|
361 /** |
|
362 * Gets the implementation class name of a registered custom string DQL function. |
|
363 * |
|
364 * @param string $name |
|
365 * @return string |
|
366 */ |
|
367 public function getCustomStringFunction($name) |
|
368 { |
|
369 $name = strtolower($name); |
|
370 return isset($this->_attributes['customStringFunctions'][$name]) ? |
|
371 $this->_attributes['customStringFunctions'][$name] : null; |
|
372 } |
|
373 |
|
374 /** |
|
375 * Sets a map of custom DQL string functions. |
|
376 * |
|
377 * Keys must be function names and values the FQCN of the implementing class. |
|
378 * The function names will be case-insensitive in DQL. |
|
379 * |
|
380 * Any previously added string functions are discarded. |
|
381 * |
|
382 * @param array $functions The map of custom DQL string functions. |
|
383 */ |
|
384 public function setCustomStringFunctions(array $functions) |
|
385 { |
|
386 $this->_attributes['customStringFunctions'] = array_change_key_case($functions); |
|
387 } |
|
388 |
|
389 /** |
|
390 * Registers a custom DQL function that produces a numeric value. |
|
391 * Such a function can then be used in any DQL statement in any place where numeric |
|
392 * functions are allowed. |
|
393 * |
|
394 * DQL function names are case-insensitive. |
|
395 * |
|
396 * @param string $name |
|
397 * @param string $className |
|
398 */ |
|
399 public function addCustomNumericFunction($name, $className) |
|
400 { |
|
401 $this->_attributes['customNumericFunctions'][strtolower($name)] = $className; |
|
402 } |
|
403 |
|
404 /** |
|
405 * Gets the implementation class name of a registered custom numeric DQL function. |
|
406 * |
|
407 * @param string $name |
|
408 * @return string |
|
409 */ |
|
410 public function getCustomNumericFunction($name) |
|
411 { |
|
412 $name = strtolower($name); |
|
413 return isset($this->_attributes['customNumericFunctions'][$name]) ? |
|
414 $this->_attributes['customNumericFunctions'][$name] : null; |
|
415 } |
|
416 |
|
417 /** |
|
418 * Sets a map of custom DQL numeric functions. |
|
419 * |
|
420 * Keys must be function names and values the FQCN of the implementing class. |
|
421 * The function names will be case-insensitive in DQL. |
|
422 * |
|
423 * Any previously added numeric functions are discarded. |
|
424 * |
|
425 * @param array $functions The map of custom DQL numeric functions. |
|
426 */ |
|
427 public function setCustomNumericFunctions(array $functions) |
|
428 { |
|
429 $this->_attributes['customNumericFunctions'] = array_change_key_case($functions); |
|
430 } |
|
431 |
|
432 /** |
|
433 * Registers a custom DQL function that produces a date/time value. |
|
434 * Such a function can then be used in any DQL statement in any place where date/time |
|
435 * functions are allowed. |
|
436 * |
|
437 * DQL function names are case-insensitive. |
|
438 * |
|
439 * @param string $name |
|
440 * @param string $className |
|
441 */ |
|
442 public function addCustomDatetimeFunction($name, $className) |
|
443 { |
|
444 $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className; |
|
445 } |
|
446 |
|
447 /** |
|
448 * Gets the implementation class name of a registered custom date/time DQL function. |
|
449 * |
|
450 * @param string $name |
|
451 * @return string |
|
452 */ |
|
453 public function getCustomDatetimeFunction($name) |
|
454 { |
|
455 $name = strtolower($name); |
|
456 return isset($this->_attributes['customDatetimeFunctions'][$name]) ? |
|
457 $this->_attributes['customDatetimeFunctions'][$name] : null; |
|
458 } |
|
459 |
|
460 /** |
|
461 * Sets a map of custom DQL date/time functions. |
|
462 * |
|
463 * Keys must be function names and values the FQCN of the implementing class. |
|
464 * The function names will be case-insensitive in DQL. |
|
465 * |
|
466 * Any previously added date/time functions are discarded. |
|
467 * |
|
468 * @param array $functions The map of custom DQL date/time functions. |
|
469 */ |
|
470 public function setCustomDatetimeFunctions(array $functions) |
|
471 { |
|
472 $this->_attributes['customDatetimeFunctions'] = array_change_key_case($functions); |
|
473 } |
|
474 |
|
475 /** |
|
476 * Get the hydrator class for the given hydration mode name. |
|
477 * |
|
478 * @param string $modeName The hydration mode name. |
|
479 * @return string $hydrator The hydrator class name. |
|
480 */ |
|
481 public function getCustomHydrationMode($modeName) |
|
482 { |
|
483 return isset($this->_attributes['customHydrationModes'][$modeName]) ? |
|
484 $this->_attributes['customHydrationModes'][$modeName] : null; |
|
485 } |
|
486 |
|
487 /** |
|
488 * Add a custom hydration mode. |
|
489 * |
|
490 * @param string $modeName The hydration mode name. |
|
491 * @param string $hydrator The hydrator class name. |
|
492 */ |
|
493 public function addCustomHydrationMode($modeName, $hydrator) |
|
494 { |
|
495 $this->_attributes['customHydrationModes'][$modeName] = $hydrator; |
|
496 } |
|
497 |
|
498 /** |
|
499 * Set a class metadata factory. |
|
500 * |
|
501 * @param string $cmf |
|
502 */ |
|
503 public function setClassMetadataFactoryName($cmfName) |
|
504 { |
|
505 $this->_attributes['classMetadataFactoryName'] = $cmfName; |
|
506 } |
|
507 |
|
508 /** |
|
509 * @return string |
|
510 */ |
|
511 public function getClassMetadataFactoryName() |
|
512 { |
|
513 if (!isset($this->_attributes['classMetadataFactoryName'])) { |
|
514 $this->_attributes['classMetadataFactoryName'] = 'Doctrine\ORM\Mapping\ClassMetadataFactory'; |
|
515 } |
|
516 return $this->_attributes['classMetadataFactoryName']; |
|
517 } |
|
518 } |