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