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.
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>.
20 namespace Doctrine\ORM;
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;
36 * Configuration container for all configuration options of Doctrine.
37 * It combines all configuration options from DBAL & ORM.
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>
46 class Configuration extends \Doctrine\DBAL\Configuration
49 * Sets the directory where Doctrine generates any necessary proxy class files.
53 public function setProxyDir($dir)
55 $this->_attributes['proxyDir'] = $dir;
59 * Gets the directory where Doctrine generates any necessary proxy class files.
63 public function getProxyDir()
65 return isset($this->_attributes['proxyDir'])
66 ? $this->_attributes['proxyDir']
71 * Gets a boolean flag that indicates whether proxy classes should always be regenerated
72 * during each script execution.
76 public function getAutoGenerateProxyClasses()
78 return isset($this->_attributes['autoGenerateProxyClasses'])
79 ? $this->_attributes['autoGenerateProxyClasses']
84 * Sets a boolean flag that indicates whether proxy classes should always be regenerated
85 * during each script execution.
87 * @param boolean $bool
89 public function setAutoGenerateProxyClasses($bool)
91 $this->_attributes['autoGenerateProxyClasses'] = $bool;
95 * Gets the namespace where proxy classes reside.
99 public function getProxyNamespace()
101 return isset($this->_attributes['proxyNamespace'])
102 ? $this->_attributes['proxyNamespace']
107 * Sets the namespace where proxy classes reside.
111 public function setProxyNamespace($ns)
113 $this->_attributes['proxyNamespace'] = $ns;
117 * Sets the cache driver implementation that is used for metadata caching.
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).
123 public function setMetadataDriverImpl(MappingDriver $driverImpl)
125 $this->_attributes['metadataDriverImpl'] = $driverImpl;
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.
132 * @param array $paths
133 * @param bool $useSimpleAnnotationReader
134 * @return AnnotationDriver
136 public function newDefaultAnnotationDriver($paths = array(), $useSimpleAnnotationReader = true)
138 AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
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());
146 return new AnnotationDriver($cachedReader, (array) $paths);
149 return new AnnotationDriver(
150 new CachedReader(new AnnotationReader(), new ArrayCache()),
156 * Adds a namespace under a certain alias.
158 * @param string $alias
159 * @param string $namespace
161 public function addEntityNamespace($alias, $namespace)
163 $this->_attributes['entityNamespaces'][$alias] = $namespace;
167 * Resolves a registered namespace alias to the full namespace.
169 * @param string $entityNamespaceAlias
170 * @throws ORMException
173 public function getEntityNamespace($entityNamespaceAlias)
175 if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
176 throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
179 return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
183 * Set the entity alias map
185 * @param array $entityNamespaces
187 public function setEntityNamespaces(array $entityNamespaces)
189 $this->_attributes['entityNamespaces'] = $entityNamespaces;
193 * Retrieves the list of registered entity namespace aliases.
197 public function getEntityNamespaces()
199 return $this->_attributes['entityNamespaces'];
203 * Gets the cache driver implementation that is used for the mapping metadata.
205 * @throws ORMException
206 * @return MappingDriver
208 public function getMetadataDriverImpl()
210 return isset($this->_attributes['metadataDriverImpl'])
211 ? $this->_attributes['metadataDriverImpl']
216 * Gets the cache driver implementation that is used for the query cache (SQL cache).
218 * @return \Doctrine\Common\Cache\Cache
220 public function getQueryCacheImpl()
222 return isset($this->_attributes['queryCacheImpl'])
223 ? $this->_attributes['queryCacheImpl']
228 * Sets the cache driver implementation that is used for the query cache (SQL cache).
230 * @param \Doctrine\Common\Cache\Cache $cacheImpl
232 public function setQueryCacheImpl(Cache $cacheImpl)
234 $this->_attributes['queryCacheImpl'] = $cacheImpl;
238 * Gets the cache driver implementation that is used for the hydration cache (SQL cache).
240 * @return \Doctrine\Common\Cache\Cache
242 public function getHydrationCacheImpl()
244 return isset($this->_attributes['hydrationCacheImpl'])
245 ? $this->_attributes['hydrationCacheImpl']
250 * Sets the cache driver implementation that is used for the hydration cache (SQL cache).
252 * @param \Doctrine\Common\Cache\Cache $cacheImpl
254 public function setHydrationCacheImpl(Cache $cacheImpl)
256 $this->_attributes['hydrationCacheImpl'] = $cacheImpl;
260 * Gets the cache driver implementation that is used for metadata caching.
262 * @return \Doctrine\Common\Cache\Cache
264 public function getMetadataCacheImpl()
266 return isset($this->_attributes['metadataCacheImpl'])
267 ? $this->_attributes['metadataCacheImpl']
272 * Sets the cache driver implementation that is used for metadata caching.
274 * @param \Doctrine\Common\Cache\Cache $cacheImpl
276 public function setMetadataCacheImpl(Cache $cacheImpl)
278 $this->_attributes['metadataCacheImpl'] = $cacheImpl;
282 * Adds a named DQL query to the configuration.
284 * @param string $name The name of the query.
285 * @param string $dql The DQL query string.
287 public function addNamedQuery($name, $dql)
289 $this->_attributes['namedQueries'][$name] = $dql;
293 * Gets a previously registered named DQL query.
295 * @param string $name The name of the query.
296 * @throws ORMException
297 * @return string The DQL query.
299 public function getNamedQuery($name)
301 if ( ! isset($this->_attributes['namedQueries'][$name])) {
302 throw ORMException::namedQueryNotFound($name);
305 return $this->_attributes['namedQueries'][$name];
309 * Adds a named native query to the configuration.
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.
315 public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm)
317 $this->_attributes['namedNativeQueries'][$name] = array($sql, $rsm);
321 * Gets the components of a previously registered named native query.
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.
328 public function getNamedNativeQuery($name)
330 if ( ! isset($this->_attributes['namedNativeQueries'][$name])) {
331 throw ORMException::namedNativeQueryNotFound($name);
334 return $this->_attributes['namedNativeQueries'][$name];
338 * Ensures that this Configuration instance contains settings that are
339 * suitable for a production environment.
341 * @throws ORMException If a configuration setting has a value that is not
342 * suitable for a production environment.
344 public function ensureProductionSettings()
346 if ( ! $this->getQueryCacheImpl()) {
347 throw ORMException::queryCacheNotConfigured();
350 if ( ! $this->getMetadataCacheImpl()) {
351 throw ORMException::metadataCacheNotConfigured();
354 if ($this->getAutoGenerateProxyClasses()) {
355 throw ORMException::proxyClassesAlwaysRegenerating();
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.
364 * DQL function names are case-insensitive.
366 * @param string $name
367 * @param string $className
368 * @throws ORMException
370 public function addCustomStringFunction($name, $className)
372 if (Query\Parser::isInternalFunction($name)) {
373 throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
376 $this->_attributes['customStringFunctions'][strtolower($name)] = $className;
380 * Gets the implementation class name of a registered custom string DQL function.
382 * @param string $name
385 public function getCustomStringFunction($name)
387 $name = strtolower($name);
389 return isset($this->_attributes['customStringFunctions'][$name])
390 ? $this->_attributes['customStringFunctions'][$name]
395 * Sets a map of custom DQL string functions.
397 * Keys must be function names and values the FQCN of the implementing class.
398 * The function names will be case-insensitive in DQL.
400 * Any previously added string functions are discarded.
402 * @param array $functions The map of custom DQL string functions.
404 public function setCustomStringFunctions(array $functions)
406 foreach ($functions as $name => $className) {
407 $this->addCustomStringFunction($name, $className);
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.
416 * DQL function names are case-insensitive.
418 * @param string $name
419 * @param string $className
420 * @throws ORMException
422 public function addCustomNumericFunction($name, $className)
424 if (Query\Parser::isInternalFunction($name)) {
425 throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
428 $this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
432 * Gets the implementation class name of a registered custom numeric DQL function.
434 * @param string $name
437 public function getCustomNumericFunction($name)
439 $name = strtolower($name);
441 return isset($this->_attributes['customNumericFunctions'][$name])
442 ? $this->_attributes['customNumericFunctions'][$name]
447 * Sets a map of custom DQL numeric functions.
449 * Keys must be function names and values the FQCN of the implementing class.
450 * The function names will be case-insensitive in DQL.
452 * Any previously added numeric functions are discarded.
454 * @param array $functions The map of custom DQL numeric functions.
456 public function setCustomNumericFunctions(array $functions)
458 foreach ($functions as $name => $className) {
459 $this->addCustomNumericFunction($name, $className);
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.
468 * DQL function names are case-insensitive.
470 * @param string $name
471 * @param string $className
472 * @throws ORMException
474 public function addCustomDatetimeFunction($name, $className)
476 if (Query\Parser::isInternalFunction($name)) {
477 throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
480 $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
484 * Gets the implementation class name of a registered custom date/time DQL function.
486 * @param string $name
489 public function getCustomDatetimeFunction($name)
491 $name = strtolower($name);
493 return isset($this->_attributes['customDatetimeFunctions'][$name])
494 ? $this->_attributes['customDatetimeFunctions'][$name]
499 * Sets a map of custom DQL date/time functions.
501 * Keys must be function names and values the FQCN of the implementing class.
502 * The function names will be case-insensitive in DQL.
504 * Any previously added date/time functions are discarded.
506 * @param array $functions The map of custom DQL date/time functions.
508 public function setCustomDatetimeFunctions(array $functions)
510 foreach ($functions as $name => $className) {
511 $this->addCustomDatetimeFunction($name, $className);
516 * Set the custom hydrator modes in one pass.
518 * @param array An array of ($modeName => $hydrator)
520 public function setCustomHydrationModes($modes)
522 $this->_attributes['customHydrationModes'] = array();
524 foreach ($modes as $modeName => $hydrator) {
525 $this->addCustomHydrationMode($modeName, $hydrator);
530 * Get the hydrator class for the given hydration mode name.
532 * @param string $modeName The hydration mode name.
533 * @return string $hydrator The hydrator class name.
535 public function getCustomHydrationMode($modeName)
537 return isset($this->_attributes['customHydrationModes'][$modeName])
538 ? $this->_attributes['customHydrationModes'][$modeName]
543 * Add a custom hydration mode.
545 * @param string $modeName The hydration mode name.
546 * @param string $hydrator The hydrator class name.
548 public function addCustomHydrationMode($modeName, $hydrator)
550 $this->_attributes['customHydrationModes'][$modeName] = $hydrator;
554 * Set a class metadata factory.
556 * @param string $cmfName
558 public function setClassMetadataFactoryName($cmfName)
560 $this->_attributes['classMetadataFactoryName'] = $cmfName;
566 public function getClassMetadataFactoryName()
568 if ( ! isset($this->_attributes['classMetadataFactoryName'])) {
569 $this->_attributes['classMetadataFactoryName'] = 'Doctrine\ORM\Mapping\ClassMetadataFactory';
572 return $this->_attributes['classMetadataFactoryName'];
576 * Add a filter to the list of possible filters.
578 * @param string $name The name of the filter.
579 * @param string $className The class name of the filter.
581 public function addFilter($name, $className)
583 $this->_attributes['filters'][$name] = $className;
587 * Gets the class name for a given filter name.
589 * @param string $name The name of the filter.
591 * @return string The class name of the filter, or null of it is not
594 public function getFilterClassName($name)
596 return isset($this->_attributes['filters'][$name])
597 ? $this->_attributes['filters'][$name]
602 * Set default repository class.
605 * @param string $className
606 * @throws ORMException If not is a \Doctrine\Common\Persistence\ObjectRepository
608 public function setDefaultRepositoryClassName($className)
610 $reflectionClass = new \ReflectionClass($className);
612 if ( ! $reflectionClass->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) {
613 throw ORMException::invalidEntityRepository($className);
616 $this->_attributes['defaultRepositoryClassName'] = $className;
620 * Get default repository class.
625 public function getDefaultRepositoryClassName()
627 return isset($this->_attributes['defaultRepositoryClassName'])
628 ? $this->_attributes['defaultRepositoryClassName']
629 : 'Doctrine\ORM\EntityRepository';
633 * Set naming strategy.
636 * @param NamingStrategy $namingStrategy
638 public function setNamingStrategy(NamingStrategy $namingStrategy)
640 $this->_attributes['namingStrategy'] = $namingStrategy;
644 * Get naming strategy..
647 * @return NamingStrategy
649 public function getNamingStrategy()
651 if ( ! isset($this->_attributes['namingStrategy'])) {
652 $this->_attributes['namingStrategy'] = new DefaultNamingStrategy();
655 return $this->_attributes['namingStrategy'];
659 * Set quote strategy.
662 * @param Doctrine\ORM\Mapping\QuoteStrategy $quoteStrategy
664 public function setQuoteStrategy(QuoteStrategy $quoteStrategy)
666 $this->_attributes['quoteStrategy'] = $quoteStrategy;
670 * Get quote strategy.
673 * @return Doctrine\ORM\Mapping\QuoteStrategy
675 public function getQuoteStrategy()
677 if ( ! isset($this->_attributes['quoteStrategy'])) {
678 $this->_attributes['quoteStrategy'] = new DefaultQuoteStrategy();
681 return $this->_attributes['quoteStrategy'];