Simple exception handler and error handler added, profile update added with stubs
[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                 $this->backTrace = debug_backtrace();
58         }
59
60         /**
61          * Get saved backtrace
62          *
63          * @return      $backTrace      The full backtrace in an array
64          */
65         public final function getBackTrace () {
66                 return $this->backTrace;
67         }
68
69         /**
70          * Getter for printable backtrace
71          *
72          * @return      $backTrace      Backtrace for web pages
73          */
74         public final function getPrintableBackTrace () {
75                 // Get the backtrace
76                 $dbgTrace = $this->getBackTrace();
77
78                 // Taken from de.php.net user comments
79                 $dbgMsg = "<br />\nDebug backtrace begin:<br />\n";
80                 foreach ($dbgTrace as $dbgIndex => $dbgInfo) {
81                         // No info by default
82                         $info = "NULL";
83
84                         // Are there arguments?
85                         if ((isset($dbgInfo['args'])) && (is_array($dbgInfo['args'])) && (isset($dbgInfo['args'][0]))) {
86                                 //* DEBUG: */ echo $dbgIndex.": <pre>".htmlentities(print_r($dbgInfo['args'], true))."</pre>";
87                                 $info = "";
88                                 foreach ($dbgInfo['args'] as $debug) {
89                                         // Add only non-array elements
90                                         if (!is_array($debug)) {
91                                                 $info .= $debug.", ";
92                                         } // END - if
93                                 } // END - if
94
95                                 $info = substr($info, 0, -2);
96                         } // END - if
97
98                         // Prepare argument infos
99                         $info = "<em id=\"debug_args\">{$info}</em>";
100
101                         // File detection
102                         $file = "Unknown file";
103                         if (isset($dbgInfo['file'])) {
104                                 $file = basename($dbgInfo['file']);
105                         } // END - if
106
107                         // Line detection
108                         $line = "Unknown line";
109                         if (isset($dbgInfo['line'])) {
110                                 $line = "line {$dbgInfo['line']}";
111                         } // END - if
112
113                         // The message
114                         $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";
115                 } // END - if
116                 $dbgMsg .= "Debug backtrace end<br />\n";
117
118                 return $dbgMsg;
119         }
120
121         /**
122          * Returns the name of the thrown exception
123          *
124          * @return      $toString               The name of the thrown exception
125          */
126         public function __toString() {
127                 return get_class($this);
128         }
129
130         /**
131          * Getter for hex-decimal code
132          *
133          * @param       $code           Integer code to encode in hex
134          * @return      $hexCode        The exception code in hex-decimal format
135          */
136         public final function getHexCode ($code = null) {
137                 // Get the decimal code
138                 if (is_null($code)) $code = $this->getCode();
139
140                 // Format it to hex-decimal, 0x as prefix and 3 chars
141                 $hexCode = sprintf("0x%03s", dechex($code));
142
143                 // Return it
144                 return $hexCode;
145         }
146 }
147
148 // [EOF]
149 ?>