e43ce8ba6b0a94fd82944ef2f885964c12842c2e
[shipsimu.git] / inc / classes / middleware / debug / class_DebugMiddleware.php
1 <?php
2 /**
3  * The middlware debug output system. A *real* or concrete output class shall
4  * become registered with this middleware because the back-fall class will
5  * become deprecated soon.
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 class DebugMiddleware extends BaseMiddleware {
26         /**
27          * The concrete output instance
28          */
29         private $outputHandler = null;
30
31         /**
32          * An instance of this class
33          */
34         private static $thisInstance = null;
35
36         /**
37          * Private constructor
38          *
39          * @return      void
40          */
41         private final function __construct () {
42                 // Call parent constructor
43                 parent::constructor(__CLASS__);
44
45                 // Set description
46                 $this->setPartDescr("Debug-Ausgabe-Handler");
47
48                 // Create an unique ID
49                 $this->createUniqueID();
50
51                 // Set own instance
52                 self::$thisInstance = $this;
53         }
54
55         /**
56          * Create a new debug output system.
57          * If no output is given this class is currently being used for back-fall.
58          * This fall-back mechanism will become deprecated very soon.
59          *
60          * @param               $debuggerClass       The class name which we shall use for
61          *                                                      registering the *real* debug output
62          * @return      $debugInstance          An instance of this middleware class
63          */
64         public final static function createDebugMiddleware ($debuggerClass) {
65                 // Create an instance if this middleware
66                 $debugInstance = new DebugMiddleware();
67
68                 // Is there a valid output handler provided?
69                 if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && (method_exists($debuggerClass, 'outputStream'))) {
70                         // Use the given output system
71                         $debugInstance->setOutputHandler($debuggerClass);
72                 } elseif ((!is_null($debuggerClass)) && (is_string($debuggerClass)) && (class_exists($debuggerClass))) {
73                         // A name for a debug output class has been provided so we try to get it
74                         $eval = sprintf("\$debuggerClass = %s::create%s();",
75                                 $debuggerClass,
76                                 $debuggerClass
77                         );
78
79                         // Run the constructed name
80                         @eval($eval);
81
82                         // Was this successfull?
83                         if ((is_object($debuggerClass)) && (method_exists($debuggerClass, "outputStream"))) {
84                                 // Set this as output class
85                                 $debugInstance->setOutputHandler($debuggerClass);
86                         } else {
87                                 // No object or method is missing use fall-back
88                                 throw new MissingMethodException(array($debuggerClass, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
89                         }
90                 }
91
92                 // Return instance
93                 return $debugInstance;
94         }
95
96         /**
97          * Getter for an instance of this class
98          *
99          * @return      $thisInstance           An instance of this class
100          */
101         public final static function getInstance() {
102                 return self::$thisInstance;
103         }
104
105         /**
106          * Setter for output handler
107          *
108          * @return      void
109          */
110         public final function setOutputHandler ($outputHandler) {
111                 $this->outputHandler = $outputHandler;
112         }
113
114         /**
115          * This method shall send debug output which can be HTML code for the
116          * browser or debug lines for a log file, etc. to the registered debug
117          * output handler.
118          *
119          * @return      void
120          */
121         public final function output ($outStream) {
122                 // Check if the output handler is valid
123                 if (is_null($this->outputHandler)) {
124                         // Debug output handler was not set
125                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
126                 } elseif (!is_object($this->outputHandler)) {
127                         // The debug output handler is not an object
128                         throw new NoObjectException($this->ouputHandler, self::EXCEPTION_IS_NO_OBJECT);
129                 } elseif (!method_exists($this->outputHandler, 'outputStream')) {
130                         // The required method outputStream() is missing
131                         throw new MissingMethodException(array($this->outputHandler, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
132                 }
133
134                 // Is the output stream set
135                 if (empty($outStream)) {
136                         // Initialization phase
137                         return;
138                 }
139
140                 // Use the output handler
141                 $this->outputHandler->outputStream($outStream);
142         }
143 }
144
145 // [EOF]
146 ?>