Code sync from ship-simu code (all class config entries must end with _class!)
[mailer.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.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 class DebugMiddleware extends BaseMiddleware implements Registerable {
27         /**
28          * The concrete output instance
29          */
30         private $outputInstance = null;
31
32         /**
33          * An instance of this class
34          */
35         private static $thisInstance = null;
36
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45
46                 // Set description
47                 $this->setObjectDescription("Debug-Ausgabe-Instance");
48
49                 // Create an unique ID
50                 $this->generateUniqueId();
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 instance provided?
70                 if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && ($debuggerClass instanceof OutputStreamer)) {
71                         // Use the given output instance
72                         $debugInstance->setOutputInstance($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                         $debuggerInstance = ObjectFactory::createObjectByName($debuggerClass);
76
77                         // Set this as output class
78                         $debugInstance->setOutputInstance($debuggerInstance);
79                 }
80
81                 // Return instance
82                 return $debugInstance;
83         }
84
85         /**
86          * Getter for an instance of this class
87          *
88          * @return      $thisInstance           An instance of this class
89          */
90         public final static function getInstance() {
91                 return self::$thisInstance;
92         }
93
94         /**
95          * Setter for output instance
96          *
97          * @param       $outputInstance The debug output instance
98          * @return      void
99          */
100         public final function setOutputInstance (OutputStreamer $outputInstance) {
101                 $this->outputInstance = $outputInstance;
102         }
103
104         /**
105          * This method shall send debug output which can be HTML code for the
106          * browser or debug lines for a log file, etc. to the registered debug
107          * output instance.
108          *
109          * @param       $outStream      Data we shall "stream" out to the world
110          * @return      void
111          */
112         public final function output ($outStream) {
113                 // Check if the output instance is valid
114                 if (is_null($this->outputInstance)) {
115                         // Debug output instance was not set
116                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
117                 } elseif (!is_object($this->outputInstance)) {
118                         // The debug output instance is not an object
119                         throw new NoObjectException($this->ouputInstance, self::EXCEPTION_IS_NO_OBJECT);
120                 } elseif (!method_exists($this->outputInstance, 'outputStream')) {
121                         // The required method outputStream() is missing
122                         throw new MissingMethodException(array($this->outputInstance, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
123                 }
124
125                 // Is the output stream set
126                 if (empty($outStream)) {
127                         // Initialization phase
128                         return;
129                 }
130
131                 // Use the output instance
132                 $this->outputInstance->outputStream($outStream);
133         }
134 }
135
136 // [EOF]
137 ?>