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