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