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