c3a2c1a38ff395788fb3eb187d1038ba6b0e217c
[core.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                 // Make sure everything is assigned properly
65                 parent::__construct($message, $code);
66
67                 // Log it away if DEBUG_ALL is set
68                 if (defined('DEBUG_ALL')) {
69                         // Log the error
70                         error_log(sprintf("[%s:] %s (%s)",
71                                 $this->__toString(),
72                                 $message,
73                                 $this->getHexCode()
74                         ));
75                 } // END - if
76         }
77
78         /**
79          * Save the current backtrace
80          *
81          * @return      void
82          */
83         private final function saveBackTrace () {
84                 // Get full backtrace
85                 $this->backTrace = debug_backtrace();
86
87                 // Remove this call
88                 $dummy = array_shift($this->backTrace);
89
90                 // resort the array
91                 ksort($this->backTrace);
92         }
93
94         /**
95          * Get saved backtrace
96          *
97          * @return      $backTrace      The full backtrace in an array
98          */
99         public final function getBackTrace () {
100                 return $this->backTrace;
101         }
102
103         /**
104          * Getter for printable backtrace
105          *
106          * @return      $backTrace      Backtrace for web pages
107          */
108         public final function getPrintableBackTrace () {
109                 // Get the backtrace
110                 $dbgTrace = $this->getBackTrace();
111
112                 // Taken from de.php.net user comments
113                 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
114                 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
115                         // No info by default
116                         $info = "NULL";
117
118                         // Are there arguments?
119                         if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
120                                 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
121                                 $info = "";
122                                 foreach ($dbgInfo['args'] as $debug) {
123                                         // Add only non-array elements
124                                         if (!is_array($debug)) {
125                                                 $info .= $debug.", ";
126                                         } // END - if
127                                 } // END - if
128
129                                 $info = substr($info, 0, -2);
130                         } // END - if
131
132                         // Prepare argument infos
133                         $info = "<em id=\"debug_args_".$dbgIndex."\">{$info}</em>";
134
135                         // File detection
136                         $file = "Unknown file";
137                         if (isset($dbgInfo['file'])) {
138                                 $file = basename($dbgInfo['file']);
139                         } // END - if
140
141                         // Line detection
142                         $line = "Unknown line";
143                         if (isset($dbgInfo['line'])) {
144                                 $line = "line {$dbgInfo['line']}";
145                         } // END - if
146
147                         // The message
148                         $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";
149                 } // END - if
150                 $dbgMsg .= "Debug backtrace end<br />\n";
151
152                 return $dbgMsg;
153         }
154
155         /**
156          * Returns the name of the thrown exception
157          *
158          * @return      $toString               The name of the thrown exception
159          */
160         public function __toString() {
161                 return get_class($this);
162         }
163
164         /**
165          * Getter for hex-decimal code
166          *
167          * @param       $code           Integer code to encode in hex
168          * @return      $hexCode        The exception code in hex-decimal format
169          */
170         public final function getHexCode ($code = null) {
171                 // Get the decimal code
172                 if (is_null($code)) $code = $this->getCode();
173
174                 // Format it to hex-decimal, 0x as prefix and 3 chars
175                 $hexCode = sprintf("0x%03s", dechex($code));
176
177                 // Return it
178                 return $hexCode;
179         }
180
181         /**
182          * Setter for extra data
183          *
184          * @param       $extraData      Extra data to store
185          * @return      void
186          */
187         protected final function setExtraData ($extraData) {
188                 $this->extraData = $extraData;
189         }
190
191         /**
192          * Getter for extra data
193          *
194          * @return      $extraData      Extra data to store
195          */
196         public final function getExtraData () {
197                 return $this->extraData;
198         }
199 }
200
201 // [EOF]
202 ?>