1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/lib/Doctrine/Common/DoctrineException.php

64 lines
2.1 KiB
PHP
Raw Normal View History

2008-09-12 17:08:01 +04:00
<?php
namespace Doctrine\Common;
class DoctrineException extends \Exception
2008-09-12 17:08:01 +04:00
{
private static $_messages = array();
2009-07-24 15:33:38 +04:00
public function __construct($message = "", \Exception $cause = null)
2008-09-12 17:08:01 +04:00
{
2009-07-24 15:33:38 +04:00
parent::__construct($message, 0, $cause);
2008-09-12 17:08:01 +04:00
}
public static function notImplemented($method, $class)
{
return new self("The method '$method' is not implemented in class '$class'.");
2008-09-12 17:08:01 +04:00
}
public static function __callStatic($method, $arguments)
{
$class = get_called_class();
$messageKey = substr($class, strrpos($class, '\\') + 1) . "#$method";
$end = end($arguments);
2009-07-15 02:36:09 +04:00
$innerException = null;
if ($end instanceof \Exception) {
2009-07-15 02:36:09 +04:00
$innerException = $end;
unset($arguments[count($arguments) - 1]);
}
if ($message = self::getExceptionMessage($messageKey)) {
$message = sprintf($message, $arguments);
} else {
$message = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $method));
$message = ucfirst(str_replace('_', ' ', $message));
$args = array();
foreach ($arguments as $argument) {
$args[] = var_export($argument, true);
}
$message .= ' (' . implode(', ', $args) . ')';
}
2009-07-15 02:36:09 +04:00
return new $class($message, $innerException);
}
public static function getExceptionMessage($messageKey)
{
if ( ! self::$_messages) {
// Lazy-init messages
self::$_messages = array(
'DoctrineException#partialObjectsAreDangerous' =>
"Loading partial objects is dangerous. Fetch full objects or consider " .
"using a different fetch mode. If you really want partial objects, " .
"set the doctrine.forcePartialLoad query hint to TRUE.",
'QueryException#nonUniqueResult' =>
"The query contains more than one result."
);
}
if (isset(self::$_messages[$messageKey])) {
return self::$_messages[$messageKey];
}
return false;
}
}