TODO: We should find something better than BaseFrameworkSystem as a type-hint
[shipsimu.git] / ship-simu / 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
9  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
10  * @license             GNU GPL 3.0 or any newer version
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 abstract class FrameworkException extends ReflectionException {
26         /**
27          * Array for the backtrace
28          */
29         private $backTrace = array();
30
31         /**
32          * The super constructor for all exceptions
33          *
34          * @param               $message                The non-optional message for the exception
35          * @param               $code           An optional code for better debugging
36          * @return      void
37          */
38         public function __construct($message, $code = 0) {
39                 // Extract backtrace
40                 $this->saveBackTrace();
41
42                 // Cast all data
43                 $message = (string) $message;
44                 $code    = (int)    $code;
45
46                 // make sure everything is assigned properly
47                 parent::__construct($message, $code);
48         }
49
50         /**
51          * Save the current backtrace
52          *
53          * @return      void
54          */
55         private final function saveBackTrace () {
56                 $this->backTrace = debug_backtrace();
57         }
58
59         /**
60          * Get saved backtrace
61          *
62          * @return      $backTrace      The full backtrace in an array
63          */
64         public final function getBackTrace () {
65                 return $this->backTrace;
66         }
67
68         /**
69          * Returns the name of the thrown exception
70          *
71          * @return      $toString               The name of the thrown exception
72          */
73         public function __toString() {
74                 return get_class($this);
75         }
76
77         /**
78          * Getter for hex-decimal code
79          *
80          * @return      $hexCode        The exception code in hex-decimal format
81          */
82         public final function getHexCode () {
83                 // Get the decimal code
84                 $code = $this->getCode();
85
86                 // Format it to hex-decimal, 0x as prefix and 3 chars
87                 $hexCode = sprintf("0x%03s", dechex($code));
88
89                 // Return it
90                 return $hexCode;
91         }
92 }
93
94 // [EOF]
95 ?>