541bbd2b218d7f00d4df40f88780de8bd1f0bbfc
[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          * An instance of this class
29          */
30         private static $selfInstance = NULL;
31
32         /**
33          * Protected constructor
34          *
35          * @return      void
36          */
37         protected function __construct () {
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40
41                 // Set own instance
42                 self::$selfInstance = $this;
43
44                 // Set it so all can use it
45                 $this->setDebugInstance($this);
46         }
47
48         /**
49          * Create a new debug output system.
50          * If no output is given this class is currently being used for back-fall.
51          * This fall-back mechanism will become deprecated very soon.
52          *
53          * @param       $outputClass    The class name which we shall use for
54          *                                                      registering the *real* debug output
55          * @param       $className              Class where a output should be created and
56          *                                                      configured for
57          * @return      $debugInstance  An instance of this middleware class
58          */
59         public static final function createDebugMiddleware ($outputClass, $className) {
60                 //* DEBUG-DIE: */ die(__METHOD__.': outputClass=' . $outputClass . ',className=' . $className);
61
62                 // Create an instance if this middleware
63                 $debugInstance = new DebugMiddleware();
64
65                 // Default is that $outputClass may be invalid
66                 $isInitialized = FALSE;
67
68                 // Is there a valid output instance provided?
69                 if ((!is_null($outputClass)) && (is_object($outputClass)) && ($outputClass instanceof OutputStreamer)) {
70                         // Use the given output instance
71                         $debugInstance->setOutputInstance($outputClass);
72
73                         // All fine
74                         $isInitialized = TRUE;
75                 } elseif ((!is_null($outputClass)) && (is_string($outputClass)) && (class_exists($outputClass))) {
76                         // A name for a debug output class has been provided so we try to get it
77                         $outputInstance = ObjectFactory::createObjectByName($outputClass);
78
79                         // Set this as output class
80                         $debugInstance->setOutputInstance($outputInstance);
81
82                         // All fine
83                         $isInitialized = TRUE;
84                 }
85
86                 // Is the output class initialized?
87                 if ($isInitialized === TRUE) {
88                         // Then set class name
89                         $debugInstance->getOutputInstance()->setLoggerClassName($className);
90                 } // END - if
91
92                 // Return instance
93                 return $debugInstance;
94         }
95
96         /**
97          * Getter for an instance of this class
98          *
99          * @return      $selfInstance           An instance of this class
100          */
101         public static final function getSelfInstance() {
102                 return self::$selfInstance;
103         }
104
105         /**
106          * This method shall send debug output which can be HTML code for the
107          * browser or debug lines for a log file, etc. to the registered debug
108          * output instance.
109          *
110          * @param       $outStream      Data we shall 'stream' out to the world
111          * @param       $stripTags      Whether HTML tags shall be stripped out
112          * @return      void
113          */
114         public final function output ($outStream, $stripTags = FALSE) {
115                 // Is the output stream set
116                 if (empty($outStream)) {
117                         // @TODO Initialization phase
118                         return;
119                 } // END - if
120
121                 // Use the output instance
122                 $this->outputInstance->outputStream($outStream, $stripTags);
123         }
124 }
125
126 // [EOF]
127 ?>