Updated domain without a dash :(
[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@shipsimu.org>
8  * @version             0.0.0
9  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
10  * @license             GNU GPL 3.0 or any newer version
11  * @link                http://www.shipsimu.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                 // Make sure everything is assigned properly
46                 parent::__construct($message, $code);
47
48                 // Extract backtrace
49                 $this->saveBackTrace();
50
51                 // Cast all data
52                 $message = (string) $message;
53                 $code    = (int)    $code;
54
55                 // In emergency exit?
56                 if (defined('EMERGENCY_EXIT_CALLED')) {
57                         // Output message
58                         printf("[%s:] Message: %s, Backtrace: <pre>%s</pre>",
59                                 $this->__toString(),
60                                 $message,
61                                 $this->getPrintableBackTrace()
62                         );
63
64                         // End here
65                         exit();
66                 } // END - if
67
68                 // Should we log exceptions? (bad implementation)
69                 if (defined('LOG_EXCEPTIONS')) {
70                         // Log the error
71                         error_log(sprintf("[%s:] %s (%s)",
72                                 $this->__toString(),
73                                 $message,
74                                 $this->getHexCode()
75                         ));
76                 } // END - if
77         }
78
79         /**
80          * Save the current backtrace
81          *
82          * @return      void
83          */
84         private final function saveBackTrace () {
85                 // Get full backtrace
86                 $this->backTrace = debug_backtrace();
87
88                 // Remove this call
89                 $dummy = array_shift($this->backTrace);
90
91                 // resort the array
92                 ksort($this->backTrace);
93         }
94
95         /**
96          * Get saved backtrace
97          *
98          * @return      $backTrace      The full backtrace in an array
99          */
100         public final function getBackTrace () {
101                 return $this->backTrace;
102         }
103
104         /**
105          * Getter for printable backtrace
106          *
107          * @return      $backTrace      Backtrace for web pages
108          */
109         public final function getPrintableBackTrace () {
110                 // Get the backtrace
111                 $dbgTrace = $this->getBackTrace();
112
113                 // Taken from de.php.net user comments
114                 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
115                 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
116                         // No info by default
117                         $info = 'NULL';
118
119                         // Are there arguments?
120                         if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
121                                 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], TRUE))."</pre>";
122                                 $info = '';
123                                 foreach ($dbgInfo['args'] as $debug) {
124                                         // Add only non-array elements
125                                         if (!is_array($debug)) {
126                                                 $info .= $debug . ', ';
127                                         } // END - if
128                                 } // END - foreach
129
130                                 // Remove last chars (commata, space)
131                                 $info = substr($info, 0, -2);
132                         } // END - if
133
134                         // Prepare argument infos
135                         $info = '<em id="debug_args_' . $dbgIndex . '">' . $info . '</em>';
136
137                         // File detection
138                         $file = 'Unknown file';
139                         if (isset($dbgInfo['file'])) {
140                                 $file = basename($dbgInfo['file']);
141                         } // END - if
142
143                         // Line detection
144                         $line = 'Unknown line';
145                         if (isset($dbgInfo['line'])) {
146                                 $line = 'line ' . $dbgInfo['line'];
147                         } // END - if
148
149                         // The message
150                         $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";
151                 } // END - if
152
153                 // Add end-message
154                 $dbgMsg .= "Debug backtrace end<br />\n";
155
156                 // Return full debug message
157                 return $dbgMsg;
158         }
159
160         /**
161          * Returns the name of the thrown exception
162          *
163          * @return      $toString               The name of the thrown exception
164          */
165         public function __toString() {
166                 return get_class($this);
167         }
168
169         /**
170          * Getter for hex-decimal code
171          *
172          * @param       $code           Integer code to encode in hex
173          * @return      $hexCode        The exception code in hex-decimal format
174          */
175         public final function getHexCode ($code = NULL) {
176                 // Get the decimal code
177                 if (is_null($code)) $code = $this->getCode();
178
179                 // Format it to hex-decimal, 0x as prefix and 3 chars
180                 $hexCode = sprintf("0x%03s", dechex($code));
181
182                 // Return it
183                 return $hexCode;
184         }
185
186         /**
187          * Setter for extra data
188          *
189          * @param       $extraData      Extra data to store
190          * @return      void
191          */
192         protected final function setExtraData ($extraData) {
193                 $this->extraData = $extraData;
194         }
195
196         /**
197          * Getter for extra data
198          *
199          * @return      $extraData      Extra data to store
200          */
201         public final function getExtraData () {
202                 return $this->extraData;
203         }
204 }
205
206 // [EOF]
207 ?>