1
0
mirror of synced 2025-01-18 06:21:40 +03:00

[DDC-1900] Throw exception when overwriting internal function.

This commit is contained in:
Benjamin Eberlei 2012-07-05 21:52:40 +02:00
parent cc4613533b
commit 12cddf20e3
3 changed files with 42 additions and 3 deletions

View File

@ -380,6 +380,10 @@ class Configuration extends \Doctrine\DBAL\Configuration
*/ */
public function addCustomStringFunction($name, $className) public function addCustomStringFunction($name, $className)
{ {
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}
$this->_attributes['customStringFunctions'][strtolower($name)] = $className; $this->_attributes['customStringFunctions'][strtolower($name)] = $className;
} }
@ -410,7 +414,9 @@ class Configuration extends \Doctrine\DBAL\Configuration
*/ */
public function setCustomStringFunctions(array $functions) public function setCustomStringFunctions(array $functions)
{ {
$this->_attributes['customStringFunctions'] = array_change_key_case($functions); foreach ($functions as $name => $className) {
$this->addCustomStringFunction($name, $className);
}
} }
/** /**
@ -425,6 +431,10 @@ class Configuration extends \Doctrine\DBAL\Configuration
*/ */
public function addCustomNumericFunction($name, $className) public function addCustomNumericFunction($name, $className)
{ {
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}
$this->_attributes['customNumericFunctions'][strtolower($name)] = $className; $this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
} }
@ -455,7 +465,9 @@ class Configuration extends \Doctrine\DBAL\Configuration
*/ */
public function setCustomNumericFunctions(array $functions) public function setCustomNumericFunctions(array $functions)
{ {
$this->_attributes['customNumericFunctions'] = array_change_key_case($functions); foreach ($functions as $name => $className) {
$this->addCustomNumericFunction($name, $className);
}
} }
/** /**
@ -470,6 +482,10 @@ class Configuration extends \Doctrine\DBAL\Configuration
*/ */
public function addCustomDatetimeFunction($name, $className) public function addCustomDatetimeFunction($name, $className)
{ {
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}
$this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className; $this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
} }
@ -500,7 +516,9 @@ class Configuration extends \Doctrine\DBAL\Configuration
*/ */
public function setCustomDatetimeFunctions(array $functions) public function setCustomDatetimeFunctions(array $functions)
{ {
$this->_attributes['customDatetimeFunctions'] = array_change_key_case($functions); foreach ($functions as $name => $className) {
$this->addCustomDatetimeFunction($name, $className);
}
} }
/** /**

View File

@ -149,4 +149,9 @@ class ORMException extends Exception
{ {
return new self("The identifier $fieldName is missing for a query of " . $className); return new self("The identifier $fieldName is missing for a query of " . $className);
} }
public static function overwriteInternalDQLFunctionNotAllowed($functionName)
{
return new self("It is not allowed to overwrite internal function '$functionName' in the DQL parser through user-defined functions.");
}
} }

View File

@ -136,6 +136,22 @@ class Parser
*/ */
private $_identVariableExpressions = array(); private $_identVariableExpressions = array();
/**
* Check if a function is internally defined. Used to prevent overwriting
* of built-in functions through user-defined functions.
*
* @param string $functionName
* @return bool
*/
static public function isInternalFunction($functionName)
{
$functionName = strtolower($functionName);
return isset(self::$_STRING_FUNCTIONS[$functionName])
|| isset(self::$_DATETIME_FUNCTIONS[$functionName])
|| isset(self::$_NUMERIC_FUNCTIONS[$functionName]);
}
/** /**
* Creates a new query parser object. * Creates a new query parser object.
* *