Updated copyright:
[core.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@shipsimu.org>
8  * @version             0.0.0
9  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
10  * @license             GNU GPL 3.0 or any newer version
11  * @link                http://www.shipsimu.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 $selfInstance = 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 own instance
47                 self::$selfInstance = $this;
48
49                 // Set it so all can use it
50                 $this->setDebugInstance($this);
51         }
52
53         /**
54          * Create a new debug output system.
55          * If no output is given this class is currently being used for back-fall.
56          * This fall-back mechanism will become deprecated very soon.
57          *
58          * @param       $debuggerClass  The class name which we shall use for
59          *                                                      registering the *real* debug output
60          * @return      $debugInstance  An instance of this middleware class
61          */
62         public static final function createDebugMiddleware ($debuggerClass) {
63                 // Create an instance if this middleware
64                 $debugInstance = new DebugMiddleware();
65
66                 // Is there a valid output instance provided?
67                 if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && ($debuggerClass instanceof OutputStreamer)) {
68                         // Use the given output instance
69                         $debugInstance->setOutputInstance($debuggerClass);
70                 } elseif ((!is_null($debuggerClass)) && (is_string($debuggerClass)) && (class_exists($debuggerClass))) {
71                         // A name for a debug output class has been provided so we try to get it
72                         $debuggerInstance = ObjectFactory::createObjectByName($debuggerClass);
73
74                         // Set this as output class
75                         $debugInstance->setOutputInstance($debuggerInstance);
76                 }
77
78                 // Return instance
79                 return $debugInstance;
80         }
81
82         /**
83          * Getter for an instance of this class
84          *
85          * @return      $selfInstance           An instance of this class
86          */
87         public static final function getSelfInstance() {
88                 return self::$selfInstance;
89         }
90
91         /**
92          * Setter for output instance
93          *
94          * @param       $outputInstance The debug output instance
95          * @return      void
96          */
97         public final function setOutputInstance (OutputStreamer $outputInstance) {
98                 $this->outputInstance = $outputInstance;
99         }
100
101         /**
102          * This method shall send debug output which can be HTML code for the
103          * browser or debug lines for a log file, etc. to the registered debug
104          * output instance.
105          *
106          * @param       $outStream      Data we shall 'stream' out to the world
107          * @param       $stripTags      Whether HTML tags shall be stripped out
108          * @return      void
109          */
110         public final function output ($outStream, $stripTags = FALSE) {
111                 // Is the output stream set
112                 if (empty($outStream)) {
113                         // @TODO Initialization phase
114                         return;
115                 } // END - if
116
117                 // Use the output instance
118                 $this->outputInstance->outputStream($outStream, $stripTags);
119         }
120 }
121
122 // [EOF]
123 ?>