Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Configuration.php
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 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\Common\Persistence\Mapping\Driver\MappingDriver,
27     Doctrine\ORM\Mapping\Driver\AnnotationDriver,
28     Doctrine\ORM\Mapping\QuoteStrategy,
29     Doctrine\ORM\Mapping\DefaultQuoteStrategy,
30     Doctrine\ORM\Mapping\NamingStrategy,
31     Doctrine\ORM\Mapping\DefaultNamingStrategy,
32     Doctrine\Common\Annotations\SimpleAnnotationReader,
33     Doctrine\Common\Annotations\CachedReader;
34
35 /**
36  * Configuration container for all configuration options of Doctrine.
37  * It combines all configuration options from DBAL & ORM.
38  *
39  * @since 2.0
40  * @internal When adding a new configuration option just write a getter/setter pair.
41  * @author  Benjamin Eberlei <kontakt@beberlei.de>
42  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
43  * @author  Jonathan Wage <jonwage@gmail.com>
44  * @author  Roman Borschel <roman@code-factory.org>
45  */
46 class Configuration extends \Doctrine\DBAL\Configuration
47 {
48     /**
49      * Sets the directory where Doctrine generates any necessary proxy class files.
50      *
51      * @param string $dir
52      */
53     public function setProxyDir($dir)
54     {
55         $this->_attributes['proxyDir'] = $dir;
56     }
57
58     /**
59      * Gets the directory where Doctrine generates any necessary proxy class files.
60      *
61      * @return string
62      */
63     public function getProxyDir()
64     {
65         return isset($this->_attributes['proxyDir'])
66             ? $this->_attributes['proxyDir']
67             : null;
68     }
69
70     /**
71      * Gets a boolean flag that indicates whether proxy classes should always be regenerated
72      * during each script execution.
73      *
74      * @return boolean
75      */
76     public function getAutoGenerateProxyClasses()
77     {
78         return isset($this->_attributes['autoGenerateProxyClasses'])
79             ? $this->_attributes['autoGenerateProxyClasses']
80             : true;
81     }
82
83     /**
84      * Sets a boolean flag that indicates whether proxy classes should always be regenerated
85      * during each script execution.
86      *
87      * @param boolean $bool
88      */
89     public function setAutoGenerateProxyClasses($bool)
90     {
91         $this->_attributes['autoGenerateProxyClasses'] = $bool;
92     }
93
94     /**
95      * Gets the namespace where proxy classes reside.
96      *
97      * @return string
98      */
99     public function getProxyNamespace()
100     {
101         return isset($this->_attributes['proxyNamespace'])
102             ? $this->_attributes['proxyNamespace']
103             : null;
104     }
105
106     /**
107      * Sets the namespace where proxy classes reside.
108      *
109      * @param string $ns
110      */
111     public function setProxyNamespace($ns)
112     {
113         $this->_attributes['proxyNamespace'] = $ns;
114     }
115
116     /**
117      * Sets the cache driver implementation that is used for metadata caching.
118      *
119      * @param MappingDriver $driverImpl
120      * @todo Force parameter to be a Closure to ensure lazy evaluation
121      *       (as soon as a metadata cache is in effect, the driver never needs to initialize).
122      */
123     public function setMetadataDriverImpl(MappingDriver $driverImpl)
124     {
125         $this->_attributes['metadataDriverImpl'] = $driverImpl;
126     }
127
128     /**
129      * Add a new default annotation driver with a correctly configured annotation reader. If $useSimpleAnnotationReader
130      * is true, the notation `@Entity` will work, otherwise, the notation `@ORM\Entity` will be supported.
131      *
132      * @param array $paths
133      * @param bool $useSimpleAnnotationReader
134      * @return AnnotationDriver
135      */
136     public function newDefaultAnnotationDriver($paths = array(), $useSimpleAnnotationReader = true)
137     {
138         AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
139
140         if ($useSimpleAnnotationReader) {
141             // Register the ORM Annotations in the AnnotationRegistry
142             $reader = new SimpleAnnotationReader();
143             $reader->addNamespace('Doctrine\ORM\Mapping');
144             $cachedReader = new CachedReader($reader, new ArrayCache());
145
146             return new AnnotationDriver($cachedReader, (array) $paths);
147         }
148
149         return new AnnotationDriver(
150             new CachedReader(new AnnotationReader(), new ArrayCache()),
151             (array) $paths
152         );
153     }
154
155     /**
156      * Adds a namespace under a certain alias.
157      *
158      * @param string $alias
159      * @param string $namespace
160      */
161     public function addEntityNamespace($alias, $namespace)
162     {
163         $this->_attributes['entityNamespaces'][$alias] = $namespace;
164     }
165
166     /**
167      * Resolves a registered namespace alias to the full namespace.
168      *
169      * @param string $entityNamespaceAlias
170      * @throws ORMException
171      * @return string
172      */
173     public function getEntityNamespace($entityNamespaceAlias)
174     {
175         if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
176             throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
177         }
178
179         return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
180     }
181
182     /**
183      * Set the entity alias map
184      *
185      * @param array $entityNamespaces
186      */
187     public function setEntityNamespaces(array $entityNamespaces)
188     {
189         $this->_attributes['entityNamespaces'] = $entityNamespaces;
190     }
191
192     /**
193      * Retrieves the list of registered entity namespace aliases.
194      *
195      * @return array
196      */
197     public function getEntityNamespaces()
198     {
199         return $this->_attributes['entityNamespaces'];
200     }
201
202     /**
203      * Gets the cache driver implementation that is used for the mapping metadata.
204      *
205      * @throws ORMException
206      * @return MappingDriver
207      */
208     public function getMetadataDriverImpl()
209     {
210         return isset($this->_attributes['metadataDriverImpl'])
211             ? $this->_attributes['metadataDriverImpl']
212             : null;
213     }
214
215     /**
216      * Gets the cache driver implementation that is used for the query cache (SQL cache).
217      *
218      * @return \Doctrine\Common\Cache\Cache
219      */
220     public function getQueryCacheImpl()
221     {
222         return isset($this->_attributes['queryCacheImpl'])
223             ? $this->_attributes['queryCacheImpl']
224             : null;
225     }
226
227     /**
228      * Sets the cache driver implementation that is used for the query cache (SQL cache).
229      *
230      * @param \Doctrine\Common\Cache\Cache $cacheImpl
231      */
232     public function setQueryCacheImpl(Cache $cacheImpl)
233     {
234         $this->_attributes['queryCacheImpl'] = $cacheImpl;
235     }
236
237     /**
238      * Gets the cache driver implementation that is used for the hydration cache (SQL cache).
239      *
240      * @return \Doctrine\Common\Cache\Cache
241      */
242     public function getHydrationCacheImpl()
243     {
244         return isset($this->_attributes['hydrationCacheImpl'])
245             ? $this->_attributes['hydrationCacheImpl']
246             : null;
247     }
248
249     /**
250      * Sets the cache driver implementation that is used for the hydration cache (SQL cache).
251      *
252      * @param \Doctrine\Common\Cache\Cache $cacheImpl
253      */
254     public function setHydrationCacheImpl(Cache $cacheImpl)
255     {
256         $this->_attributes['hydrationCacheImpl'] = $cacheImpl;
257     }
258
259     /**
260      * Gets the cache driver implementation that is used for metadata caching.
261      *
262      * @return \Doctrine\Common\Cache\Cache
263      */
264     public function getMetadataCacheImpl()
265     {
266         return isset($this->_attributes['metadataCacheImpl'])
267             ? $this->_attributes['metadataCacheImpl']
268             : null;
269     }
270
271     /**
272      * Sets the cache driver implementation that is used for metadata caching.
273      *
274      * @param \Doctrine\Common\Cache\Cache $cacheImpl
275      */
276     public function setMetadataCacheImpl(Cache $cacheImpl)
277     {
278         $this->_attributes['metadataCacheImpl'] = $cacheImpl;
279     }
280
281     /**
282      * Adds a named DQL query to the configuration.
283      *
284      * @param string $name The name of the query.
285      * @param string $dql The DQL query string.
286      */
287     public function addNamedQuery($name, $dql)
288     {
289         $this->_attributes['namedQueries'][$name] = $dql;
290     }
291
292     /**
293      * Gets a previously registered named DQL query.
294      *
295      * @param string $name The name of the query.
296      * @throws ORMException
297      * @return string The DQL query.
298      */
299     public function getNamedQuery($name)
300     {
301         if ( ! isset($this->_attributes['namedQueries'][$name])) {
302             throw ORMException::namedQueryNotFound($name);
303         }
304
305         return $this->_attributes['namedQueries'][$name];
306     }
307
308     /**
309      * Adds a named native query to the configuration.
310      *
311      * @param string                 $name The name of the query.
312      * @param string                 $sql  The native SQL query string.
313      * @param Query\ResultSetMapping $rsm  The ResultSetMapping used for the results of the SQL query.
314      */
315     public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm)
316     {
317         $this->_attributes['namedNativeQueries'][$name] = array($sql, $rsm);
318     }
319
320     /**
321      * Gets the components of a previously registered named native query.
322      *
323      * @param  string $name  The name of the query.
324      * @throws ORMException
325      * @return array         A tuple with the first element being the SQL string and the second
326      *                       element being the ResultSetMapping.
327      */
328     public function getNamedNativeQuery($name)
329     {
330         if ( ! isset($this->_attributes['namedNativeQueries'][$name])) {
331             throw ORMException::namedNativeQueryNotFound($name);
332         }
333
334         return $this->_attributes['namedNativeQueries'][$name];
335     }
336
337     /**
338      * Ensures that this Configuration instance contains settings that are
339      * suitable for a production environment.
340      *
341      * @throws ORMException If a configuration setting has a value that is not
342      *                      suitable for a production environment.
343      */
344     public function ensureProductionSettings()
345     {
346         if ( ! $this->getQueryCacheImpl()) {
347             throw ORMException::queryCacheNotConfigured();
348         }
349
350         if ( ! $this->getMetadataCacheImpl()) {
351             throw ORMException::metadataCacheNotConfigured();
352         }
353
354         if ($this->getAutoGenerateProxyClasses()) {
355             throw ORMException::proxyClassesAlwaysRegenerating();
356         }
357     }
358
359     /**
360      * Registers a custom DQL function that produces a string value.
361      * Such a function can then be used in any DQL statement in any place where string
362      * functions are allowed.
363      *
364      * DQL function names are case-insensitive.
365      *
366      * @param string $name
367      * @param string $className
368      * @throws ORMException
369      */
370     public function addCustomStringFunction($name, $className)
371     {
372         if (Query\Parser::isInternalFunction($name)) {
373             throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
374         }
375
376         $this->_attributes['customStringFunctions'][strtolower($name)] = $className;
377     }
378
379     /**
380      * Gets the implementation class name of a registered custom string DQL function.
381      *
382      * @param string $name
383      * @return string
384      */
385     public function getCustomStringFunction($name)
386     {
387         $name = strtolower($name);
388
389         return isset($this->_attributes['customStringFunctions'][$name])
390             ? $this->_attributes['customStringFunctions'][$name]
391             : null;
392     }
393
394     /**
395      * Sets a map of custom DQL string functions.
396      *
397      * Keys must be function names and values the FQCN of the implementing class.
398      * The function names will be case-insensitive in DQL.
399      *
400      * Any previously added string functions are discarded.
401      *
402      * @param array $functions The map of custom DQL string functions.
403      */
404     public function setCustomStringFunctions(array $functions)
405     {
406         foreach ($functions as $name => $className) {
407             $this->addCustomStringFunction($name, $className);
408         }
409     }
410
411     /**
412      * Registers a custom DQL function that produces a numeric value.
413      * Such a function can then be used in any DQL statement in any place where numeric
414      * functions are allowed.
415      *
416      * DQL function names are case-insensitive.
417      *
418      * @param string $name
419      * @param string $className
420      * @throws ORMException
421      */
422     public function addCustomNumericFunction($name, $className)
423     {
424         if (Query\Parser::isInternalFunction($name)) {
425             throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
426         }
427
428         $this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
429     }
430
431     /**
432      * Gets the implementation class name of a registered custom numeric DQL function.
433      *
434      * @param string $name
435      * @return string
436      */
437     public function getCustomNumericFunction($name)
438     {
439         $name = strtolower($name);
440
441         return isset($this->_attributes['customNumericFunctions'][$name])
442             ? $this->_attributes['customNumericFunctions'][$name]
443             : null;
444     }
445
446     /**
447      * Sets a map of custom DQL numeric functions.
448      *
449      * Keys must be function names and values the FQCN of the implementing class.
450      * The function names will be case-insensitive in DQL.
451      *
452      * Any previously added numeric functions are discarded.
453      *
454      * @param array $functions The map of custom DQL numeric functions.
455      */
456     public function setCustomNumericFunctions(array $functions)
457     {
458         foreach ($functions as $name => $className) {
459             $this->addCustomNumericFunction($name, $className);
460         }
461     }
462
463     /**
464      * Registers a custom DQL function that produces a date/time value.
465      * Such a function can then be used in any DQL statement in any place where date/time
466      * functions are allowed.
467      *
468      * DQL function names are case-insensitive.
469      *
470      * @param string $name
471      * @param string $className
472      * @throws ORMException
473      */
474     public function addCustomDatetimeFunction($name, $className)
475     {
476         if (Query\Parser::isInternalFunction($name)) {
477             throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
478         }
479
480         $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
481     }
482
483     /**
484      * Gets the implementation class name of a registered custom date/time DQL function.
485      *
486      * @param string $name
487      * @return string
488      */
489     public function getCustomDatetimeFunction($name)
490     {
491         $name = strtolower($name);
492
493         return isset($this->_attributes['customDatetimeFunctions'][$name])
494             ? $this->_attributes['customDatetimeFunctions'][$name]
495             : null;
496     }
497
498     /**
499      * Sets a map of custom DQL date/time functions.
500      *
501      * Keys must be function names and values the FQCN of the implementing class.
502      * The function names will be case-insensitive in DQL.
503      *
504      * Any previously added date/time functions are discarded.
505      *
506      * @param array $functions The map of custom DQL date/time functions.
507      */
508     public function setCustomDatetimeFunctions(array $functions)
509     {
510         foreach ($functions as $name => $className) {
511             $this->addCustomDatetimeFunction($name, $className);
512         }
513     }
514
515     /**
516      * Set the custom hydrator modes in one pass.
517      *
518      * @param array An array of ($modeName => $hydrator)
519      */
520     public function setCustomHydrationModes($modes)
521     {
522         $this->_attributes['customHydrationModes'] = array();
523
524         foreach ($modes as $modeName => $hydrator) {
525             $this->addCustomHydrationMode($modeName, $hydrator);
526         }
527     }
528
529     /**
530      * Get the hydrator class for the given hydration mode name.
531      *
532      * @param string $modeName The hydration mode name.
533      * @return string $hydrator The hydrator class name.
534      */
535     public function getCustomHydrationMode($modeName)
536     {
537         return isset($this->_attributes['customHydrationModes'][$modeName])
538             ? $this->_attributes['customHydrationModes'][$modeName]
539             : null;
540     }
541
542     /**
543      * Add a custom hydration mode.
544      *
545      * @param string $modeName The hydration mode name.
546      * @param string $hydrator The hydrator class name.
547      */
548     public function addCustomHydrationMode($modeName, $hydrator)
549     {
550         $this->_attributes['customHydrationModes'][$modeName] = $hydrator;
551     }
552
553     /**
554      * Set a class metadata factory.
555      *
556      * @param string $cmfName
557      */
558     public function setClassMetadataFactoryName($cmfName)
559     {
560         $this->_attributes['classMetadataFactoryName'] = $cmfName;
561     }
562
563     /**
564      * @return string
565      */
566     public function getClassMetadataFactoryName()
567     {
568         if ( ! isset($this->_attributes['classMetadataFactoryName'])) {
569             $this->_attributes['classMetadataFactoryName'] = 'Doctrine\ORM\Mapping\ClassMetadataFactory';
570         }
571
572         return $this->_attributes['classMetadataFactoryName'];
573     }
574
575     /**
576      * Add a filter to the list of possible filters.
577      *
578      * @param string $name The name of the filter.
579      * @param string $className The class name of the filter.
580      */
581     public function addFilter($name, $className)
582     {
583         $this->_attributes['filters'][$name] = $className;
584     }
585
586     /**
587      * Gets the class name for a given filter name.
588      *
589      * @param string $name The name of the filter.
590      *
591      * @return string The class name of the filter, or null of it is not
592      *  defined.
593      */
594     public function getFilterClassName($name)
595     {
596         return isset($this->_attributes['filters'][$name])
597             ? $this->_attributes['filters'][$name]
598             : null;
599     }
600
601     /**
602      * Set default repository class.
603      *
604      * @since 2.2
605      * @param string $className
606      * @throws ORMException If not is a \Doctrine\Common\Persistence\ObjectRepository
607      */
608     public function setDefaultRepositoryClassName($className)
609     {
610         $reflectionClass = new \ReflectionClass($className);
611
612         if ( ! $reflectionClass->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) {
613             throw ORMException::invalidEntityRepository($className);
614         }
615
616         $this->_attributes['defaultRepositoryClassName'] = $className;
617     }
618
619     /**
620      * Get default repository class.
621      *
622      * @since 2.2
623      * @return string
624      */
625     public function getDefaultRepositoryClassName()
626     {
627         return isset($this->_attributes['defaultRepositoryClassName'])
628             ? $this->_attributes['defaultRepositoryClassName']
629             : 'Doctrine\ORM\EntityRepository';
630     }
631
632     /**
633      * Set naming strategy.
634      *
635      * @since 2.3
636      * @param NamingStrategy $namingStrategy
637      */
638     public function setNamingStrategy(NamingStrategy $namingStrategy)
639     {
640         $this->_attributes['namingStrategy'] = $namingStrategy;
641     }
642
643     /**
644      * Get naming strategy..
645      *
646      * @since 2.3
647      * @return NamingStrategy
648      */
649     public function getNamingStrategy()
650     {
651         if ( ! isset($this->_attributes['namingStrategy'])) {
652             $this->_attributes['namingStrategy'] = new DefaultNamingStrategy();
653         }
654
655         return $this->_attributes['namingStrategy'];
656     }
657
658     /**
659      * Set quote strategy.
660      *
661      * @since 2.3
662      * @param Doctrine\ORM\Mapping\QuoteStrategy $quoteStrategy
663      */
664     public function setQuoteStrategy(QuoteStrategy $quoteStrategy)
665     {
666         $this->_attributes['quoteStrategy'] = $quoteStrategy;
667     }
668
669     /**
670      * Get quote strategy.
671      *
672      * @since 2.3
673      * @return Doctrine\ORM\Mapping\QuoteStrategy
674      */
675     public function getQuoteStrategy()
676     {
677         if ( ! isset($this->_attributes['quoteStrategy'])) {
678             $this->_attributes['quoteStrategy'] = new DefaultQuoteStrategy();
679         }
680
681         return $this->_attributes['quoteStrategy'];
682     }
683 }