From e83bfeede34351e44eb18f43dbf1865918eb3b8c Mon Sep 17 00:00:00 2001 From: "Roman S. Borschel" Date: Thu, 15 Apr 2010 11:55:03 +0200 Subject: [PATCH] Simplified and streamlined configuration classes. --- lib/Doctrine/DBAL/Configuration.php | 19 +--- lib/Doctrine/ORM/Configuration.php | 96 +++++++++---------- .../ORM/Query/SelectSqlGenerationTest.php | 2 +- 3 files changed, 50 insertions(+), 67 deletions(-) diff --git a/lib/Doctrine/DBAL/Configuration.php b/lib/Doctrine/DBAL/Configuration.php index 3ad9e36a0..dfda73240 100644 --- a/lib/Doctrine/DBAL/Configuration.php +++ b/lib/Doctrine/DBAL/Configuration.php @@ -1,7 +1,5 @@ _attributes = array( - 'sqlLogger' => null - ); - } - /** * Sets the SQL logger to use. Defaults to NULL which means SQL logging is disabled. * * @param SQLLogger $logger */ - public function setSQLLogger($logger) + public function setSQLLogger(SqlLogger $logger) { $this->_attributes['sqlLogger'] = $logger; } @@ -73,6 +61,7 @@ class Configuration */ public function getSQLLogger() { - return $this->_attributes['sqlLogger']; + return isset($this->_attributes['sqlLogger']) ? + $this->_attributes['sqlLogger'] : null; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 229126635..abb8a15d8 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -27,8 +27,7 @@ use Doctrine\Common\Cache\Cache, * It combines all configuration options from DBAL & ORM. * * @since 2.0 - * @internal When adding a new configuration option just write a getter/setter - * pair and add the option to the _attributes array with a proper default value. + * @internal When adding a new configuration option just write a getter/setter pair. * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage @@ -36,31 +35,6 @@ use Doctrine\Common\Cache\Cache, */ class Configuration extends \Doctrine\DBAL\Configuration { - /** - * Creates a new configuration that can be used for Doctrine. - */ - public function __construct() - { - parent::__construct(); - $this->_attributes = array_merge($this->_attributes, array( - 'resultCacheImpl' => null, - 'queryCacheImpl' => null, - 'metadataCacheImpl' => null, - 'metadataDriverImpl' => null, - 'proxyDir' => null, - 'useCExtension' => false, - 'autoGenerateProxyClasses' => true, - 'proxyNamespace' => null, - 'entityNamespaces' => array(), - 'namedNativeQueries' => array(), - 'namedQueries' => array(), - // Custom DQL Functions - 'customDatetimeFunctions' => array(), - 'customNumericFunctions' => array(), - 'customStringFunctions' => array() - )); - } - /** * Sets the directory where Doctrine generates any necessary proxy class files. * @@ -78,7 +52,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getProxyDir() { - return $this->_attributes['proxyDir']; + return isset($this->_attributes['proxyDir']) ? + $this->_attributes['proxyDir'] : null; } /** @@ -89,7 +64,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getAutoGenerateProxyClasses() { - return $this->_attributes['autoGenerateProxyClasses']; + return isset($this->_attributes['autoGenerateProxyClasses']) ? + $this->_attributes['autoGenerateProxyClasses'] : true; } /** @@ -110,7 +86,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getProxyNamespace() { - return $this->_attributes['proxyNamespace']; + return isset($this->_attributes['proxyNamespace']) ? + $this->_attributes['proxyNamespace'] : null; } /** @@ -195,7 +172,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getMetadataDriverImpl() { - return $this->_attributes['metadataDriverImpl']; + return isset($this->_attributes['metadataDriverImpl']) ? + $this->_attributes['metadataDriverImpl'] : null; } /** @@ -205,7 +183,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getResultCacheImpl() { - return $this->_attributes['resultCacheImpl']; + return isset($this->_attributes['resultCacheImpl']) ? + $this->_attributes['resultCacheImpl'] : null; } /** @@ -225,7 +204,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getQueryCacheImpl() { - return $this->_attributes['queryCacheImpl']; + return isset($this->_attributes['queryCacheImpl']) ? + $this->_attributes['queryCacheImpl'] : null; } /** @@ -245,7 +225,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getMetadataCacheImpl() { - return $this->_attributes['metadataCacheImpl']; + return isset($this->_attributes['metadataCacheImpl']) ? + $this->_attributes['metadataCacheImpl'] : null; } /** @@ -266,7 +247,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getUseCExtension() { - return $this->_attributes['useCExtension']; + return isset($this->_attributes['useCExtension']) ? + $this->_attributes['useCExtension'] : false; } /** @@ -373,19 +355,23 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getCustomStringFunction($name) { - $name = strtolower($name); - return isset($this->_attributes['customStringFunctions'][$name]) ? $this->_attributes['customStringFunctions'][$name] : null; } /** - * Clean custom DQL functions that produces string values. + * 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 clearCustomStringFunctions() + public function setCustomStringFunctions(array $functions) { - $this->_attributes['customStringFunctions'] = array(); + $this->_attributes['customStringFunctions'] = $functions; } /** @@ -409,19 +395,23 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getCustomNumericFunction($name) { - $name = strtolower($name); - return isset($this->_attributes['customNumericFunctions'][$name]) ? $this->_attributes['customNumericFunctions'][$name] : null; } /** - * Clean custom DQL functions that produces numeric values. + * 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 clearCustomNumericFunctions() + public function setCustomNumericFunctions(array $functions) { - $this->_attributes['customNumericFunctions'] = array(); + $this->_attributes['customNumericFunctions'] = $functions; } /** @@ -445,18 +435,22 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getCustomDatetimeFunction($name) { - $name = strtolower($name); - return isset($this->_attributes['customDatetimeFunctions'][$name]) ? $this->_attributes['customDatetimeFunctions'][$name] : null; } /** - * Clean custom DQL functions that produces date/time values. - * + * 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 clearCustomDatetimeFunctions() + public function setCustomDatetimeFunctions(array $functions) { - $this->_attributes['customDatetimeFunctions'] = array(); + $this->_attributes['customDatetimeFunctions'] = $functions; } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 1ef832571..a85873a92 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -610,7 +610,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase 'SELECT ABS(c0_.phonenumber) AS sclr0 FROM cms_phonenumbers c0_' ); - $config->clearCustomNumericFunctions(); + $config->setCustomNumericFunctions(array()); } }