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