1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/lib/Doctrine/ORM/Configuration.php

695 lines
21 KiB
PHP
Raw Normal View History

<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
2012-05-26 16:37:00 +04:00
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM;
2012-10-12 15:53:20 +04:00
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
/**
* Configuration container for all configuration options of Doctrine.
* It combines all configuration options from DBAL & ORM.
*
* @since 2.0
* @internal When adding a new configuration option just write a getter/setter pair.
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Configuration extends \Doctrine\DBAL\Configuration
{
/**
* Sets the directory where Doctrine generates any necessary proxy class files.
*
* @param string $dir
*/
public function setProxyDir($dir)
{
$this->_attributes['proxyDir'] = $dir;
}
/**
* Gets the directory where Doctrine generates any necessary proxy class files.
*
* @return string
*/
public function getProxyDir()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['proxyDir'])
? $this->_attributes['proxyDir']
: null;
}
/**
* Gets a boolean flag that indicates whether proxy classes should always be regenerated
* during each script execution.
*
* @return boolean
*/
public function getAutoGenerateProxyClasses()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['autoGenerateProxyClasses'])
? $this->_attributes['autoGenerateProxyClasses']
: true;
}
/**
* Sets a boolean flag that indicates whether proxy classes should always be regenerated
* during each script execution.
*
* @param boolean $bool
*/
public function setAutoGenerateProxyClasses($bool)
{
$this->_attributes['autoGenerateProxyClasses'] = $bool;
}
/**
* Gets the namespace where proxy classes reside.
2011-08-05 18:48:05 +04:00
*
* @return string
*/
public function getProxyNamespace()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['proxyNamespace'])
? $this->_attributes['proxyNamespace']
: null;
}
/**
* Sets the namespace where proxy classes reside.
2011-08-05 18:48:05 +04:00
*
* @param string $ns
*/
public function setProxyNamespace($ns)
{
$this->_attributes['proxyNamespace'] = $ns;
}
/**
* Sets the cache driver implementation that is used for metadata caching.
*
* @param MappingDriver $driverImpl
* @todo Force parameter to be a Closure to ensure lazy evaluation
* (as soon as a metadata cache is in effect, the driver never needs to initialize).
*/
public function setMetadataDriverImpl(MappingDriver $driverImpl)
{
$this->_attributes['metadataDriverImpl'] = $driverImpl;
}
/**
* Add a new default annotation driver with a correctly configured annotation reader. If $useSimpleAnnotationReader
* is true, the notation `@Entity` will work, otherwise, the notation `@ORM\Entity` will be supported.
2011-08-05 18:48:05 +04:00
*
* @param array $paths
* @param bool $useSimpleAnnotationReader
* @return AnnotationDriver
*/
public function newDefaultAnnotationDriver($paths = array(), $useSimpleAnnotationReader = true)
{
AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');
2012-03-15 09:08:28 +04:00
if ($useSimpleAnnotationReader) {
// Register the ORM Annotations in the AnnotationRegistry
$reader = new SimpleAnnotationReader();
$reader->addNamespace('Doctrine\ORM\Mapping');
$cachedReader = new CachedReader($reader, new ArrayCache());
return new AnnotationDriver($cachedReader, (array) $paths);
}
2012-03-15 09:08:28 +04:00
return new AnnotationDriver(
new CachedReader(new AnnotationReader(), new ArrayCache()),
(array) $paths
);
}
/**
* Adds a namespace under a certain alias.
*
* @param string $alias
* @param string $namespace
*/
public function addEntityNamespace($alias, $namespace)
{
$this->_attributes['entityNamespaces'][$alias] = $namespace;
}
/**
* Resolves a registered namespace alias to the full namespace.
*
2011-08-05 18:48:05 +04:00
* @param string $entityNamespaceAlias
* @throws ORMException
* @return string
*/
public function getEntityNamespace($entityNamespaceAlias)
{
if ( ! isset($this->_attributes['entityNamespaces'][$entityNamespaceAlias])) {
throw ORMException::unknownEntityNamespace($entityNamespaceAlias);
}
return trim($this->_attributes['entityNamespaces'][$entityNamespaceAlias], '\\');
}
/**
* Set the entity alias map
*
* @param array $entityNamespaces
*/
public function setEntityNamespaces(array $entityNamespaces)
{
$this->_attributes['entityNamespaces'] = $entityNamespaces;
}
2011-08-05 18:48:05 +04:00
/**
* Retrieves the list of registered entity namespace aliases.
2011-08-05 18:48:05 +04:00
*
* @return array
*/
public function getEntityNamespaces()
{
return $this->_attributes['entityNamespaces'];
}
/**
* Gets the cache driver implementation that is used for the mapping metadata.
*
* @throws ORMException
* @return MappingDriver
*/
public function getMetadataDriverImpl()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['metadataDriverImpl'])
? $this->_attributes['metadataDriverImpl']
: null;
}
/**
* Gets the cache driver implementation that is used for the query cache (SQL cache).
*
* @return \Doctrine\Common\Cache\Cache
*/
public function getQueryCacheImpl()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['queryCacheImpl'])
? $this->_attributes['queryCacheImpl']
: null;
}
/**
* Sets the cache driver implementation that is used for the query cache (SQL cache).
*
* @param \Doctrine\Common\Cache\Cache $cacheImpl
*/
public function setQueryCacheImpl(Cache $cacheImpl)
{
$this->_attributes['queryCacheImpl'] = $cacheImpl;
}
/**
* Gets the cache driver implementation that is used for the hydration cache (SQL cache).
*
* @return \Doctrine\Common\Cache\Cache
*/
public function getHydrationCacheImpl()
{
return isset($this->_attributes['hydrationCacheImpl'])
? $this->_attributes['hydrationCacheImpl']
: null;
}
/**
* Sets the cache driver implementation that is used for the hydration cache (SQL cache).
*
* @param \Doctrine\Common\Cache\Cache $cacheImpl
*/
public function setHydrationCacheImpl(Cache $cacheImpl)
{
$this->_attributes['hydrationCacheImpl'] = $cacheImpl;
}
/**
* Gets the cache driver implementation that is used for metadata caching.
*
* @return \Doctrine\Common\Cache\Cache
*/
public function getMetadataCacheImpl()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['metadataCacheImpl'])
? $this->_attributes['metadataCacheImpl']
: null;
}
/**
* Sets the cache driver implementation that is used for metadata caching.
*
* @param \Doctrine\Common\Cache\Cache $cacheImpl
*/
public function setMetadataCacheImpl(Cache $cacheImpl)
{
$this->_attributes['metadataCacheImpl'] = $cacheImpl;
}
/**
* Adds a named DQL query to the configuration.
*
* @param string $name The name of the query.
* @param string $dql The DQL query string.
*/
public function addNamedQuery($name, $dql)
{
$this->_attributes['namedQueries'][$name] = $dql;
}
/**
* Gets a previously registered named DQL query.
*
* @param string $name The name of the query.
* @throws ORMException
* @return string The DQL query.
*/
public function getNamedQuery($name)
{
if ( ! isset($this->_attributes['namedQueries'][$name])) {
throw ORMException::namedQueryNotFound($name);
}
2012-03-15 09:08:28 +04:00
return $this->_attributes['namedQueries'][$name];
}
/**
* Adds a named native query to the configuration.
*
* @param string $name The name of the query.
* @param string $sql The native SQL query string.
* @param Query\ResultSetMapping $rsm The ResultSetMapping used for the results of the SQL query.
*/
public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm)
{
$this->_attributes['namedNativeQueries'][$name] = array($sql, $rsm);
}
/**
* Gets the components of a previously registered named native query.
*
* @param string $name The name of the query.
* @throws ORMException
* @return array A tuple with the first element being the SQL string and the second
* element being the ResultSetMapping.
*/
public function getNamedNativeQuery($name)
{
if ( ! isset($this->_attributes['namedNativeQueries'][$name])) {
throw ORMException::namedNativeQueryNotFound($name);
}
2012-03-15 09:08:28 +04:00
return $this->_attributes['namedNativeQueries'][$name];
}
/**
* Ensures that this Configuration instance contains settings that are
* suitable for a production environment.
*
* @throws ORMException If a configuration setting has a value that is not
* suitable for a production environment.
*/
public function ensureProductionSettings()
{
2012-03-15 09:08:28 +04:00
if ( ! $this->getQueryCacheImpl()) {
throw ORMException::queryCacheNotConfigured();
}
2012-03-15 09:08:28 +04:00
if ( ! $this->getMetadataCacheImpl()) {
throw ORMException::metadataCacheNotConfigured();
}
2012-03-15 09:08:28 +04:00
if ($this->getAutoGenerateProxyClasses()) {
throw ORMException::proxyClassesAlwaysRegenerating();
}
}
/**
* Registers a custom DQL function that produces a string value.
* Such a function can then be used in any DQL statement in any place where string
* functions are allowed.
*
* DQL function names are case-insensitive.
*
* @param string $name
* @param string $className
* @throws ORMException
*/
public function addCustomStringFunction($name, $className)
{
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}
$this->_attributes['customStringFunctions'][strtolower($name)] = $className;
}
/**
* Gets the implementation class name of a registered custom string DQL function.
2011-08-05 18:48:05 +04:00
*
* @param string $name
* @return string
*/
public function getCustomStringFunction($name)
{
$name = strtolower($name);
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['customStringFunctions'][$name])
? $this->_attributes['customStringFunctions'][$name]
: null;
}
/**
* Sets a map of custom DQL string functions.
*
* Keys must be function names and values the FQCN of the implementing class.
* The function names will be case-insensitive in DQL.
*
* Any previously added string functions are discarded.
*
* @param array $functions The map of custom DQL string functions.
*/
public function setCustomStringFunctions(array $functions)
{
foreach ($functions as $name => $className) {
$this->addCustomStringFunction($name, $className);
}
}
/**
* Registers a custom DQL function that produces a numeric value.
* Such a function can then be used in any DQL statement in any place where numeric
* functions are allowed.
*
* DQL function names are case-insensitive.
*
* @param string $name
* @param string $className
* @throws ORMException
*/
public function addCustomNumericFunction($name, $className)
{
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}
$this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
}
/**
* Gets the implementation class name of a registered custom numeric DQL function.
2011-08-05 18:48:05 +04:00
*
* @param string $name
* @return string
*/
public function getCustomNumericFunction($name)
{
$name = strtolower($name);
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['customNumericFunctions'][$name])
? $this->_attributes['customNumericFunctions'][$name]
: null;
}
/**
* Sets a map of custom DQL numeric functions.
*
* Keys must be function names and values the FQCN of the implementing class.
* The function names will be case-insensitive in DQL.
*
* Any previously added numeric functions are discarded.
*
* @param array $functions The map of custom DQL numeric functions.
*/
public function setCustomNumericFunctions(array $functions)
{
foreach ($functions as $name => $className) {
$this->addCustomNumericFunction($name, $className);
}
}
/**
* Registers a custom DQL function that produces a date/time value.
* Such a function can then be used in any DQL statement in any place where date/time
* functions are allowed.
*
* DQL function names are case-insensitive.
*
* @param string $name
* @param string $className
* @throws ORMException
*/
public function addCustomDatetimeFunction($name, $className)
{
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}
$this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
}
/**
* Gets the implementation class name of a registered custom date/time DQL function.
2011-08-05 18:48:05 +04:00
*
* @param string $name
* @return string
*/
public function getCustomDatetimeFunction($name)
{
$name = strtolower($name);
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['customDatetimeFunctions'][$name])
? $this->_attributes['customDatetimeFunctions'][$name]
: null;
}
/**
* Sets a map of custom DQL date/time functions.
*
* Keys must be function names and values the FQCN of the implementing class.
* The function names will be case-insensitive in DQL.
*
* Any previously added date/time functions are discarded.
*
* @param array $functions The map of custom DQL date/time functions.
*/
public function setCustomDatetimeFunctions(array $functions)
{
foreach ($functions as $name => $className) {
$this->addCustomDatetimeFunction($name, $className);
}
}
/**
* Set the custom hydrator modes in one pass.
*
* @param array An array of ($modeName => $hydrator)
*/
public function setCustomHydrationModes($modes)
{
$this->_attributes['customHydrationModes'] = array();
foreach ($modes as $modeName => $hydrator) {
$this->addCustomHydrationMode($modeName, $hydrator);
}
}
/**
* Get the hydrator class for the given hydration mode name.
*
* @param string $modeName The hydration mode name.
* @return string $hydrator The hydrator class name.
*/
public function getCustomHydrationMode($modeName)
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['customHydrationModes'][$modeName])
? $this->_attributes['customHydrationModes'][$modeName]
: null;
}
/**
* Add a custom hydration mode.
*
* @param string $modeName The hydration mode name.
* @param string $hydrator The hydrator class name.
*/
public function addCustomHydrationMode($modeName, $hydrator)
{
$this->_attributes['customHydrationModes'][$modeName] = $hydrator;
}
/**
* Set a class metadata factory.
2011-08-05 18:48:05 +04:00
*
* @param string $cmfName
*/
public function setClassMetadataFactoryName($cmfName)
{
$this->_attributes['classMetadataFactoryName'] = $cmfName;
}
/**
* @return string
*/
public function getClassMetadataFactoryName()
{
2012-03-15 09:08:28 +04:00
if ( ! isset($this->_attributes['classMetadataFactoryName'])) {
$this->_attributes['classMetadataFactoryName'] = 'Doctrine\ORM\Mapping\ClassMetadataFactory';
}
2012-03-15 09:08:28 +04:00
return $this->_attributes['classMetadataFactoryName'];
}
2011-12-06 00:14:31 +04:00
/**
* Add a filter to the list of possible filters.
*
* @param string $name The name of the filter.
* @param string|Query\Filter\SQLFilter $filter The filter class name or an
* SQLFilter instance.
*
* @throws \InvalidArgumentException If the filter is an object and it doesn't
* extend the Query\Filter\SQLFilter class.
2011-12-06 00:14:31 +04:00
*/
public function addFilter($name, $filter)
{
if (is_object($filter) && ! $filter instanceof Query\Filter\SQLFilter) {
throw new \InvalidArgumentException(
"A filter can be either a class name or an object extending \Doctrine\ORM\Query\Filter\SQLFilter," .
" instance of '" . get_class($filter) . "' given."
);
}
$this->_attributes['filters'][$name] = $filter;
}
2011-12-06 00:14:31 +04:00
/**
* Gets the class name for a given filter name.
*
* @param string $name The name of the filter.
*
* @return string|Query\Filter\SQLFilter The class name of the filter, an
* SQLFilter instance or null of it is not defined.
2011-12-06 00:14:31 +04:00
*/
public function getFilter($name)
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['filters'][$name])
? $this->_attributes['filters'][$name]
: null;
}
2011-09-08 21:36:13 +04:00
/**
* Set default repository class.
2011-12-20 01:56:19 +04:00
*
2011-09-08 21:36:13 +04:00
* @since 2.2
* @param string $className
* @throws ORMException If not is a \Doctrine\Common\Persistence\ObjectRepository
2011-09-08 21:36:13 +04:00
*/
public function setDefaultRepositoryClassName($className)
{
$reflectionClass = new \ReflectionClass($className);
2012-03-15 09:08:28 +04:00
if ( ! $reflectionClass->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) {
throw ORMException::invalidEntityRepository($className);
2011-09-08 21:36:13 +04:00
}
2012-03-15 09:08:28 +04:00
2011-09-08 21:36:13 +04:00
$this->_attributes['defaultRepositoryClassName'] = $className;
}
/**
* Get default repository class.
2011-12-20 01:56:19 +04:00
*
2011-09-08 21:36:13 +04:00
* @since 2.2
* @return string
*/
public function getDefaultRepositoryClassName()
{
2012-03-15 09:08:28 +04:00
return isset($this->_attributes['defaultRepositoryClassName'])
? $this->_attributes['defaultRepositoryClassName']
: 'Doctrine\ORM\EntityRepository';
2011-09-08 21:36:13 +04:00
}
2011-12-23 20:41:03 +04:00
/**
* Set naming strategy.
*
* @since 2.3
* @param NamingStrategy $namingStrategy
*/
public function setNamingStrategy(NamingStrategy $namingStrategy)
{
$this->_attributes['namingStrategy'] = $namingStrategy;
}
/**
* Get naming strategy..
*
* @since 2.3
* @return NamingStrategy
*/
public function getNamingStrategy()
{
2012-03-15 09:08:28 +04:00
if ( ! isset($this->_attributes['namingStrategy'])) {
2011-12-23 20:41:03 +04:00
$this->_attributes['namingStrategy'] = new DefaultNamingStrategy();
}
2011-12-23 20:41:03 +04:00
return $this->_attributes['namingStrategy'];
}
2012-05-11 04:18:09 +04:00
/**
2012-06-12 03:14:17 +04:00
* Set quote strategy.
2012-05-11 04:18:09 +04:00
*
* @since 2.3
2012-09-21 03:20:06 +04:00
* @param \Doctrine\ORM\Mapping\QuoteStrategy $quoteStrategy
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function setQuoteStrategy(QuoteStrategy $quoteStrategy)
2012-05-11 04:18:09 +04:00
{
2012-06-12 16:47:40 +04:00
$this->_attributes['quoteStrategy'] = $quoteStrategy;
2012-05-11 04:18:09 +04:00
}
/**
2012-06-12 03:14:17 +04:00
* Get quote strategy.
2012-05-11 04:18:09 +04:00
*
* @since 2.3
2012-09-21 03:20:06 +04:00
* @return \Doctrine\ORM\Mapping\QuoteStrategy
2012-05-11 04:18:09 +04:00
*/
2012-06-12 03:14:17 +04:00
public function getQuoteStrategy()
2012-05-11 04:18:09 +04:00
{
2012-06-12 03:14:17 +04:00
if ( ! isset($this->_attributes['quoteStrategy'])) {
2012-06-13 00:47:00 +04:00
$this->_attributes['quoteStrategy'] = new DefaultQuoteStrategy();
2012-06-12 03:14:17 +04:00
}
return $this->_attributes['quoteStrategy'];
2012-05-11 04:18:09 +04:00
}
}