Account status page partly implemented, backtrace now without own saveBacktrace(...
[shipsimu.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, this is free software
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          * The super constructor for all exceptions
34          *
35          * @param               $message                The non-optional message for the exception
36          * @param               $code           An optional code for better debugging
37          * @return      void
38          */
39         public function __construct($message, $code = 0) {
40                 // Extract backtrace
41                 $this->saveBackTrace();
42
43                 // Cast all data
44                 $message = (string) $message;
45                 $code    = (int)    $code;
46
47                 // Make sure everything is assigned properly
48                 parent::__construct($message, $code);
49         }
50
51         /**
52          * Save the current backtrace
53          *
54          * @return      void
55          */
56         private final function saveBackTrace () {
57                 // Get full backtrace
58                 $this->backTrace = debug_backtrace();
59
60                 // Remove this call
61                 $dummy = array_shift($this->backTrace);
62
63                 // resort the array
64                 ksort($this->backTrace);
65         }
66
67         /**
68          * Get saved backtrace
69          *
70          * @return      $backTrace      The full backtrace in an array
71          */
72         public final function getBackTrace () {
73                 return $this->backTrace;
74         }
75
76         /**
77          * Getter for printable backtrace
78          *
79          * @return      $backTrace      Backtrace for web pages
80          */
81         public final function getPrintableBackTrace () {
82                 // Get the backtrace
83                 $dbgTrace = $this->getBackTrace();
84
85                 // Taken from de.php.net user comments
86                 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
87                 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
88                         // No info by default
89                         $info = "NULL";
90
91                         // Are there arguments?
92                         if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
93                                 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
94                                 $info = "";
95                                 foreach ($dbgInfo['args'] as $debug) {
96                                         // Add only non-array elements
97                                         if (!is_array($debug)) {
98                                                 $info .= $debug.", ";
99                                         } // END - if
100                                 } // END - if
101
102                                 $info = substr($info, 0, -2);
103                         } // END - if
104
105                         // Prepare argument infos
106                         $info = "<em id=\"debug_args\">{$info}</em>";
107
108                         // File detection
109                         $file = "Unknown file";
110                         if (isset($dbgInfo['file'])) {
111                                 $file = basename($dbgInfo['file']);
112                         } // END - if
113
114                         // Line detection
115                         $line = "Unknown line";
116                         if (isset($dbgInfo['line'])) {
117                                 $line = "line {$dbgInfo['line']}";
118                         } // END - if
119
120                         // The message
121                         $dbgMsg .= "\t at <em id=\"debug_id\">".$dbgIndex."</em> <em id=\"debug_file\">".$file."</em> (<em id=\"debug_line\">".$line."</em>) -&gt; ".$dbgInfo['function']."(".$info.")<br />\n";
122                 } // END - if
123                 $dbgMsg .= "Debug backtrace end<br />\n";
124
125                 return $dbgMsg;
126         }
127
128         /**
129          * Returns the name of the thrown exception
130          *
131          * @return      $toString               The name of the thrown exception
132          */
133         public function __toString() {
134                 return get_class($this);
135         }
136
137         /**
138          * Getter for hex-decimal code
139          *
140          * @param       $code           Integer code to encode in hex
141          * @return      $hexCode        The exception code in hex-decimal format
142          */
143         public final function getHexCode ($code = null) {
144                 // Get the decimal code
145                 if (is_null($code)) $code = $this->getCode();
146
147                 // Format it to hex-decimal, 0x as prefix and 3 chars
148                 $hexCode = sprintf("0x%03s", dechex($code));
149
150                 // Return it
151                 return $hexCode;
152         }
153 }
154
155 // [EOF]
156 ?>