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