DoctrineException.php 4.94 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  $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>.
 */
 
22 23
namespace Doctrine\Common;

24 25 26 27 28 29 30 31 32 33
/** 
 * 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>
34
 * @todo Remove
35
 */
36
class DoctrineException extends \Exception
romanb's avatar
romanb committed
37
{
38 39 40 41
    /**
     * @var array Lazy initialized array of error messages
     * @static
     */
42 43
    private static $_messages = array();

44 45 46 47 48 49
    /**
     * Initializes a new DoctrineException.
     *
     * @param string $message
     * @param Exception $cause Optional Exception
     */
romanb's avatar
romanb committed
50
    public function __construct($message = "", \Exception $cause = null)
romanb's avatar
romanb committed
51
    {
52 53 54
        $code = ($cause instanceof Exception) ? $cause->getCode() : 0;
        
        parent::__construct($message, $code, $cause);
romanb's avatar
romanb committed
55 56
    }
    
57 58 59 60 61 62 63 64
    /**
     * Throws a DoctrineException reporting not implemented method in a given class
     *
     * @static
     * @param string $method Method name
     * @param string $class  Class name
     * @throws DoctrineException
     */
65
    public static function notImplemented($method = null, $class = null)
romanb's avatar
romanb committed
66
    {
67 68 69
        if ($method && $class) {
            return new self("The method '$method' is not implemented in class '$class'.");
        } else if ($method && ! $class) {
70
            return new self($method);
71
        } else {
72
            return new self('Functionality is not implemented.');
73
        }
romanb's avatar
romanb committed
74 75
    }

76 77 78 79 80 81 82 83 84 85 86 87 88
    /**
     * 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())
89
    {
90 91 92
        $class = get_called_class();
        $messageKey = substr($class, strrpos($class, '\\') + 1) . "#$method";

93
        $end = end($arguments);
jwage's avatar
jwage committed
94
        $innerException = null;
95
        
96
        if ($end instanceof \Exception) {
jwage's avatar
jwage committed
97
            $innerException = $end;
98 99 100
            unset($arguments[count($arguments) - 1]);
        }

101
        if (($message = self::getExceptionMessage($messageKey)) !== false) {
102
            $message = sprintf($message, $arguments);
103
        } else {
104
            //$dumper  = function ($value) { return var_export($value, true); };
105
            $message = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $method));
106
            $message = ucfirst(str_replace('_', ' ', $message));
107
            /*if ($arguments) {
108
                $message .= ' (' . implode(', ', array_map($dumper, $arguments)) . ')';
109
            }*/
110
        }
111
        
jwage's avatar
jwage committed
112
        return new $class($message, $innerException);
113 114
    }

115 116 117 118 119 120 121
    /**
     * Retrieves error string given a message key for lookup 
     *
     * @static
     * @param string $messageKey
     * @return string|false Returns the error string if found; FALSE otherwise
     */
122
    public static function getExceptionMessage($messageKey)
123 124
    {
        if ( ! self::$_messages) {
125
            // Lazy-init messages
126
            self::$_messages = array(
127
                'DoctrineException#partialObjectsAreDangerous' =>
128 129
                        "Loading partial objects is dangerous. Fetch full objects or consider " .
                        "using a different fetch mode. If you really want partial objects, " .
130 131 132
                        "set the doctrine.forcePartialLoad query hint to TRUE.",
                'QueryException#nonUniqueResult' =>
                        "The query contains more than one result."
133 134
            );
        }
135
        
136 137
        if (isset(self::$_messages[$messageKey])) {
            return self::$_messages[$messageKey];
138
        }
139
        
140
        return false;
141 142
    }
}