Before a filter chain is executed we check if it is there
[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, 2009 Core Developer Team
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 - foreach
128
129                                 // Remove last chars (commata, space)
130                                 $info = substr($info, 0, -2);
131                         } // END - if
132
133                         // Prepare argument infos
134                         $info = "<em id=\"debug_args_".$dbgIndex."\">{$info}</em>";
135
136                         // File detection
137                         $file = "Unknown file";
138                         if (isset($dbgInfo['file'])) {
139                                 $file = basename($dbgInfo['file']);
140                         } // END - if
141
142                         // Line detection
143                         $line = "Unknown line";
144                         if (isset($dbgInfo['line'])) {
145                                 $line = "line {$dbgInfo['line']}";
146                         } // END - if
147
148                         // The message
149                         $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";
150                 } // END - if
151
152                 // Add end-message
153                 $dbgMsg .= "Debug backtrace end<br />\n";
154
155                 // Return full debug message
156                 return $dbgMsg;
157         }
158
159         /**
160          * Returns the name of the thrown exception
161          *
162          * @return      $toString               The name of the thrown exception
163          */
164         public function __toString() {
165                 return get_class($this);
166         }
167
168         /**
169          * Getter for hex-decimal code
170          *
171          * @param       $code           Integer code to encode in hex
172          * @return      $hexCode        The exception code in hex-decimal format
173          */
174         public final function getHexCode ($code = null) {
175                 // Get the decimal code
176                 if (is_null($code)) $code = $this->getCode();
177
178                 // Format it to hex-decimal, 0x as prefix and 3 chars
179                 $hexCode = sprintf("0x%03s", dechex($code));
180
181                 // Return it
182                 return $hexCode;
183         }
184
185         /**
186          * Setter for extra data
187          *
188          * @param       $extraData      Extra data to store
189          * @return      void
190          */
191         protected final function setExtraData ($extraData) {
192                 $this->extraData = $extraData;
193         }
194
195         /**
196          * Getter for extra data
197          *
198          * @return      $extraData      Extra data to store
199          */
200         public final function getExtraData () {
201                 return $this->extraData;
202         }
203 }
204
205 // [EOF]
206 ?>