2008-09-12 17:08:01 +04:00
|
|
|
<?php
|
2009-08-11 01:36:57 +04:00
|
|
|
/*
|
|
|
|
* $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>.
|
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Common;
|
|
|
|
|
2009-08-11 01:36:57 +04:00
|
|
|
/**
|
|
|
|
* 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>
|
2009-12-16 00:06:32 +03:00
|
|
|
* @todo Remove
|
2009-08-11 01:36:57 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class DoctrineException extends \Exception
|
2008-09-12 17:08:01 +04:00
|
|
|
{
|
2009-08-11 01:36:57 +04:00
|
|
|
/**
|
|
|
|
* @var array Lazy initialized array of error messages
|
|
|
|
* @static
|
|
|
|
*/
|
2009-02-19 10:00:54 +03:00
|
|
|
private static $_messages = array();
|
|
|
|
|
2009-08-11 01:36:57 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2009-08-11 01:36:57 +04:00
|
|
|
$code = ($cause instanceof Exception) ? $cause->getCode() : 0;
|
|
|
|
|
|
|
|
parent::__construct($message, $code, $cause);
|
2008-09-12 17:08:01 +04:00
|
|
|
}
|
|
|
|
|
2009-08-11 01:36:57 +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
|
|
|
|
*/
|
2009-08-27 02:03:39 +04:00
|
|
|
public static function notImplemented($method = null, $class = null)
|
2008-09-12 17:08:01 +04:00
|
|
|
{
|
2009-08-27 02:03:39 +04:00
|
|
|
if ($method && $class) {
|
|
|
|
return new self("The method '$method' is not implemented in class '$class'.");
|
|
|
|
} else if ($method && ! $class) {
|
2009-08-28 14:48:40 +04:00
|
|
|
return new self($method);
|
2009-08-27 02:03:39 +04:00
|
|
|
} else {
|
2009-08-28 14:48:40 +04:00
|
|
|
return new self('Functionality is not implemented.');
|
2009-08-27 02:03:39 +04:00
|
|
|
}
|
2008-09-12 17:08:01 +04:00
|
|
|
}
|
|
|
|
|
2009-08-11 01:36:57 +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())
|
2009-02-19 10:00:54 +03:00
|
|
|
{
|
2009-03-30 23:43:05 +04:00
|
|
|
$class = get_called_class();
|
|
|
|
$messageKey = substr($class, strrpos($class, '\\') + 1) . "#$method";
|
|
|
|
|
2009-02-19 10:00:54 +03:00
|
|
|
$end = end($arguments);
|
2009-07-15 02:36:09 +04:00
|
|
|
$innerException = null;
|
2009-08-11 01:36:57 +04:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
if ($end instanceof \Exception) {
|
2009-07-15 02:36:09 +04:00
|
|
|
$innerException = $end;
|
2009-02-19 10:00:54 +03:00
|
|
|
unset($arguments[count($arguments) - 1]);
|
|
|
|
}
|
|
|
|
|
2009-08-11 01:36:57 +04:00
|
|
|
if (($message = self::getExceptionMessage($messageKey)) !== false) {
|
2009-03-28 23:59:07 +03:00
|
|
|
$message = sprintf($message, $arguments);
|
2009-02-19 10:00:54 +03:00
|
|
|
} else {
|
2009-10-28 13:31:47 +03:00
|
|
|
//$dumper = function ($value) { return var_export($value, true); };
|
2009-08-11 01:36:57 +04:00
|
|
|
$message = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $method));
|
2009-10-23 02:19:17 +04:00
|
|
|
$message = ucfirst(str_replace('_', ' ', $message));
|
2009-10-28 13:31:47 +03:00
|
|
|
/*if ($arguments) {
|
2009-10-23 02:19:17 +04:00
|
|
|
$message .= ' (' . implode(', ', array_map($dumper, $arguments)) . ')';
|
2009-10-28 13:31:47 +03:00
|
|
|
}*/
|
2009-02-19 10:00:54 +03:00
|
|
|
}
|
2009-03-30 23:43:05 +04:00
|
|
|
|
2009-07-15 02:36:09 +04:00
|
|
|
return new $class($message, $innerException);
|
2009-03-28 23:59:07 +03:00
|
|
|
}
|
|
|
|
|
2009-08-11 01:36:57 +04:00
|
|
|
/**
|
|
|
|
* Retrieves error string given a message key for lookup
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param string $messageKey
|
|
|
|
* @return string|false Returns the error string if found; FALSE otherwise
|
|
|
|
*/
|
2009-03-30 23:43:05 +04:00
|
|
|
public static function getExceptionMessage($messageKey)
|
2009-03-28 23:59:07 +03:00
|
|
|
{
|
|
|
|
if ( ! self::$_messages) {
|
2009-03-30 23:43:05 +04:00
|
|
|
// Lazy-init messages
|
2009-03-28 23:59:07 +03:00
|
|
|
self::$_messages = array(
|
2009-03-30 23:43:05 +04:00
|
|
|
'QueryException#nonUniqueResult' =>
|
|
|
|
"The query contains more than one result."
|
2009-03-28 23:59:07 +03:00
|
|
|
);
|
|
|
|
}
|
2009-08-11 01:36:57 +04:00
|
|
|
|
2009-03-30 23:43:05 +04:00
|
|
|
if (isset(self::$_messages[$messageKey])) {
|
|
|
|
return self::$_messages[$messageKey];
|
2009-03-28 23:59:07 +03:00
|
|
|
}
|
2009-08-11 01:36:57 +04:00
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
return false;
|
2009-02-19 10:00:54 +03:00
|
|
|
}
|
|
|
|
}
|