source: trunk/zpush/backend/zarafa/mapi/class.baseexception.php @ 7589

Revision 7589, 7.1 KB checked in by douglas, 11 years ago (diff)

Ticket #3209 - Integrar módulo de sincronização Z-push ao Expresso

Line 
1<?php
2/*
3 * Copyright 2005 - 2012  Zarafa B.V.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License, version 3,
7 * as published by the Free Software Foundation with the following additional
8 * term according to sec. 7:
9 *
10 * According to sec. 7 of the GNU Affero General Public License, version
11 * 3, the terms of the AGPL are supplemented with the following terms:
12 *
13 * "Zarafa" is a registered trademark of Zarafa B.V. The licensing of
14 * the Program under the AGPL does not imply a trademark license.
15 * Therefore any rights, title and interest in our trademarks remain
16 * entirely with us.
17 *
18 * However, if you propagate an unmodified version of the Program you are
19 * allowed to use the term "Zarafa" to indicate that you distribute the
20 * Program. Furthermore you may use our trademarks where it is necessary
21 * to indicate the intended purpose of a product or service provided you
22 * use it in accordance with honest practices in industrial or commercial
23 * matters.  If you want to propagate modified versions of the Program
24 * under the name "Zarafa" or "Zarafa Server", you may only do so if you
25 * have a written permission by Zarafa B.V. (to acquire a permission
26 * please contact Zarafa at trademark@zarafa.com).
27 *
28 * The interactive user interface of the software displays an attribution
29 * notice containing the term "Zarafa" and/or the logo of Zarafa.
30 * Interactive user interfaces of unmodified and modified versions must
31 * display Appropriate Legal Notices according to sec. 5 of the GNU
32 * Affero General Public License, version 3, when you propagate
33 * unmodified or modified versions of the Program. In accordance with
34 * sec. 7 b) of the GNU Affero General Public License, version 3, these
35 * Appropriate Legal Notices must retain the logo of Zarafa or display
36 * the words "Initial Development by Zarafa" if the display of the logo
37 * is not reasonably feasible for technical reasons. The use of the logo
38 * of Zarafa in Legal Notices is allowed for unmodified and modified
39 * versions of the software.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU Affero General Public License for more details.
45 *
46 * You should have received a copy of the GNU Affero General Public License
47 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
48 *
49 */
50
51
52/**
53 * Defines a base exception class for all custom exceptions, so every exceptions that
54 * is thrown/caught by this application should extend this base class and make use of it.
55 * it removes some peculiarities between different versions of PHP and exception handling.
56 *
57 * Some basic function of Exception class
58 * getMessage()- message of exception
59 * getCode() - code of exception
60 * getFile() - source filename
61 * getLine() - source line
62 * getTrace() - n array of the backtrace()
63 * getTraceAsString() - formated string of trace
64 */
65class BaseException extends Exception
66{
67    /**
68     * Reference of previous exception, only used for PHP < 5.3
69     * can't use $previous here as its a private variable of parent class
70     */
71    private $_previous = null;
72
73    /**
74     * Base name of the file, so we don't have to use static path of the file
75     */
76    private $baseFile = null;
77
78    /**
79     * Flag to check if exception is already handled or not
80     */
81    public $isHandled = false;
82
83    /**
84     * The exception message to show at client side.
85     */
86    public $displayMessage = null;
87
88    /**
89     * Construct the exception
90     *
91     * @param  string $errorMessage
92     * @param  int $code
93     * @param  Exception $previous
94     * @param  string $displayMessage
95     * @return void
96     */
97    public function __construct($errorMessage, $code = 0, Exception $previous = null, $displayMessage = null) {
98        // assign display message
99        $this->displayMessage = $displayMessage;
100
101        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
102            parent::__construct($errorMessage, (int) $code);
103
104            // set previous exception
105            if(!is_null($previous)) {
106                $this->_previous = $previous;
107            }
108        } else {
109            parent::__construct($errorMessage, (int) $code, $previous);
110        }
111    }
112
113    /**
114     * Overloading of final methods to get rid of incompatibilities between different PHP versions.
115     *
116     * @param  string $method
117     * @param  array $args
118     * @return mixed
119     */
120    public function __call($method, array $args)
121    {
122        if ('getprevious' == strtolower($method)) {
123            return $this->_getPrevious();
124        }
125
126        return null;
127    }
128
129    /**
130     * @return string returns file name and line number combined where exception occured.
131     */
132    public function getFileLine()
133    {
134        return $this->getBaseFile() . ':' . $this->getLine();
135    }
136
137    /**
138     * @return string returns message that should be sent to client to display
139     */
140    public function getDisplayMessage()
141    {
142        if(!is_null($this->displayMessage)) {
143            return $this->displayMessage;
144        }
145
146        return $this->getMessage();
147    }
148
149    /**
150     * Function sets display message of an exception that will be sent to the client side
151     * to show it to user.
152     * @param string $message display message.
153     */
154    public function setDisplayMessage($message)
155    {
156        $this->displayMessage = $message;
157    }
158
159    /**
160     * Function sets a flag in exception class to indicate that exception is already handled
161     * so if it is caught again in the top level of function stack then we have to silently
162     * ignore it.
163     */
164    public function setHandled()
165    {
166        $this->isHandled = true;
167    }
168
169    /**
170     * @return string returns base path of the file where exception occured.
171     */
172    public function getBaseFile()
173    {
174        if(is_null($this->baseFile)) {
175            $this->baseFile = basename(parent::getFile());
176        }
177
178        return $this->baseFile;
179    }
180
181    /**
182     * Function will check for PHP version if it is greater than 5.3 then we can use its default implementation
183     * otherwise we have to use our own implementation of chanining functionality.
184     *
185     * @return Exception returns previous exception
186     */
187    public function _getPrevious()
188    {
189        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
190            return $this->_previous;
191        } else {
192            return parent::getPrevious();
193        }
194    }
195
196    /**
197     * String representation of the exception, also handles previous exception.
198     *
199     * @return string
200     */
201    public function __toString()
202    {
203        if (version_compare(PHP_VERSION, '5.3.0', '<')) {
204            if (($e = $this->getPrevious()) !== null) {
205                return $e->__toString()
206                        . "\n\nNext "
207                        . parent::__toString();
208            }
209        }
210
211        return parent::__toString();
212    }
213
214    /**
215     * Name of the class of exception.
216     *
217     * @return string
218     */
219    public function getName()
220    {
221        return get_class($this);
222    }
223
224    // @TODO getTrace and getTraceAsString
225}
226?>
Note: See TracBrowser for help on using the repository browser.