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

142 lines
4.9 KiB
PHP
Raw Normal View History

2008-09-12 17:08:01 +04:00
<?php
/*
* $Id$
*
* 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
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common;
/**
* Base Exception class of Doctrine
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision: 3938 $
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @todo Remove
*/
class DoctrineException extends \Exception
2008-09-12 17:08:01 +04:00
{
/**
* @var array Lazy initialized array of error messages
* @static
*/
private static $_messages = array();
/**
* Initializes a new DoctrineException.
*
* @param string $message
* @param Exception $cause Optional Exception
*/
2009-07-24 15:33:38 +04:00
public function __construct($message = "", \Exception $cause = null)
2008-09-12 17:08:01 +04:00
{
$code = ($cause instanceof Exception) ? $cause->getCode() : 0;
parent::__construct($message, $code, $cause);
2008-09-12 17:08:01 +04:00
}
/**
* Throws a DoctrineException reporting not implemented method in a given class
*
* @static
* @param string $method Method name
* @param string $class Class name
* @throws DoctrineException
*/
public static function notImplemented($method = null, $class = null)
2008-09-12 17:08:01 +04:00
{
if ($method && $class) {
return new self("The method '$method' is not implemented in class '$class'.");
} else if ($method && ! $class) {
return new self($method);
} else {
return new self('Functionality is not implemented.');
}
2008-09-12 17:08:01 +04:00
}
/**
* Implementation of __callStatic magic method.
*
* Received a method name and arguments. It lookups a $_messages HashMap
* for matching Class#Method key and executes the returned string value
* translating the placeholders with arguments passed.
*
* @static
* @param string $method Method name
* @param array $arguments Optional arguments to be translated in placeholders
* @throws DoctrineException
*/
public static function __callStatic($method, $arguments = array())
{
$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)) !== false) {
$message = sprintf($message, $arguments);
} else {
//$dumper = function ($value) { return var_export($value, true); };
$message = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $method));
$message = ucfirst(str_replace('_', ' ', $message));
/*if ($arguments) {
$message .= ' (' . implode(', ', array_map($dumper, $arguments)) . ')';
}*/
}
2009-07-15 02:36:09 +04:00
return new $class($message, $innerException);
}
/**
* Retrieves error string given a message key for lookup
*
* @static
* @param string $messageKey
* @return string|false Returns the error string if found; FALSE otherwise
*/
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;
}
}