267744c2556d2ec651d03e188418f9287a01bb93
[mailer.git] / inc / classes / exceptions / class_FrameworkException.php
1 <?php
2 /**
3  * A general abstract exception. You should not throw this even when you
4  * remove the "abstract" key-word. Better you make your own exception and
5  * attach a dedicated message to it.
6  *
7  * @author              Roland Haeder <webmaster@ship-simu.org>
8  * @version             0.0.0
9  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
10  * @license             GNU GPL 3.0 or any newer version
11  * @link                http://www.ship-simu.org
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 abstract class FrameworkException extends ReflectionException {
27         /**
28          * Array for the backtrace
29          */
30         private $backTrace = array();
31
32         /**
33          * Extra data
34          */
35         private $extraData = "";
36
37         /**
38          * The super constructor for all exceptions
39          *
40          * @param       $message        The non-optional message for the exception
41          * @param       $code           An optional code for better debugging
42          * @return      void
43          */
44         public function __construct($message, $code = 0) {
45                 // Extract backtrace
46                 $this->saveBackTrace();
47
48                 // Cast all data
49                 $message = (string) $message;
50                 $code    = (int)    $code;
51
52                 // In emergency exit?
53                 if (defined('EMERGENCY_EXIT_CALLED')) {
54                         // Output message
55                         printf("[%s:] Message: %s, Backtrace: <pre>%s</pre>",
56                                 $this->__toString(),
57                                 $message,
58                                 $this->getPrintableBackTrace()
59                         );
60
61                         // End here
62                         exit;
63                 } // END - if
64
65                 // Make sure everything is assigned properly
66                 parent::__construct($message, $code);
67         }
68
69         /**
70          * Save the current backtrace
71          *
72          * @return      void
73          */
74         private final function saveBackTrace () {
75                 // Get full backtrace
76                 $this->backTrace = debug_backtrace();
77
78                 // Remove this call
79                 $dummy = array_shift($this->backTrace);
80
81                 // resort the array
82                 ksort($this->backTrace);
83         }
84
85         /**
86          * Get saved backtrace
87          *
88          * @return      $backTrace      The full backtrace in an array
89          */
90         public final function getBackTrace () {
91                 return $this->backTrace;
92         }
93
94         /**
95          * Getter for printable backtrace
96          *
97          * @return      $backTrace      Backtrace for web pages
98          */
99         public final function getPrintableBackTrace () {
100                 // Get the backtrace
101                 $dbgTrace = $this->getBackTrace();
102
103                 // Taken from de.php.net user comments
104                 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
105                 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
106                         // No info by default
107                         $info = "NULL";
108
109                         // Are there arguments?
110                         if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
111                                 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
112                                 $info = "";
113                                 foreach ($dbgInfo['args'] as $debug) {
114                                         // Add only non-array elements
115                                         if (!is_array($debug)) {
116                                                 $info .= $debug.", ";
117                                         } // END - if
118                                 } // END - if
119
120                                 $info = substr($info, 0, -2);
121                         } // END - if
122
123                         // Prepare argument infos
124                         $info = "<em id=\"debug_args_".$dbgIndex."\">{$info}</em>";
125
126                         // File detection
127                         $file = "Unknown file";
128                         if (isset($dbgInfo['file'])) {
129                                 $file = basename($dbgInfo['file']);
130                         } // END - if
131
132                         // Line detection
133                         $line = "Unknown line";
134                         if (isset($dbgInfo['line'])) {
135                                 $line = "line {$dbgInfo['line']}";
136                         } // END - if
137
138                         // The message
139                         $dbgMsg .= "\t at <em id=\"debug_id_".$dbgIndex."\">".$dbgIndex."</em> <em id=\"debug_file_".$dbgIndex."\">".$file."</em> (<em id=\"debug_line_".$dbgIndex."\">".$line."</em>) -&gt; ".$dbgInfo['function']."(".$info.")<br />\n";
140                 } // END - if
141                 $dbgMsg .= "Debug backtrace end<br />\n";
142
143                 return $dbgMsg;
144         }
145
146         /**
147          * Returns the name of the thrown exception
148          *
149          * @return      $toString               The name of the thrown exception
150          */
151         public function __toString() {
152                 return get_class($this);
153         }
154
155         /**
156          * Getter for hex-decimal code
157          *
158          * @param       $code           Integer code to encode in hex
159          * @return      $hexCode        The exception code in hex-decimal format
160          */
161         public final function getHexCode ($code = null) {
162                 // Get the decimal code
163                 if (is_null($code)) $code = $this->getCode();
164
165                 // Format it to hex-decimal, 0x as prefix and 3 chars
166                 $hexCode = sprintf("0x%03s", dechex($code));
167
168                 // Return it
169                 return $hexCode;
170         }
171
172         /**
173          * Setter for extra data
174          *
175          * @param       $extraData      Extra data to store
176          * @return      void
177          */
178         protected final function setExtraData ($extraData) {
179                 $this->extraData = $extraData;
180         }
181
182         /**
183          * Getter for extra data
184          *
185          * @return      $extraData      Extra data to store
186          */
187         public final function getExtraData () {
188                 return $this->extraData;
189         }
190 }
191
192 // [EOF]
193 ?>