Using the bebug middleware class will become deprecated (now already marked
[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  * @deprecated  See LoggerFactory for a more flexible approach
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class DebugMiddleware extends BaseMiddleware implements Registerable {
28         /**
29          * An instance of this class
30          */
31         private static $selfInstance = NULL;
32
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41
42                 // Set own instance
43                 self::$selfInstance = $this;
44
45                 // Set it so all can use it
46                 $this->setDebugInstance($this);
47         }
48
49         /**
50          * Create a new debug output system.
51          * If no output is given this class is currently being used for back-fall.
52          * This fall-back mechanism will become deprecated very soon.
53          *
54          * @param       $outputClass    The class name which we shall use for
55          *                                                      registering the *real* debug output
56          * @param       $className              Class where a output should be created and
57          *                                                      configured for
58          * @return      $debugInstance  An instance of this middleware class
59          */
60         public static final function createDebugMiddleware ($outputClass, $className) {
61                 //* DEBUG-DIE: */ die(__METHOD__.': outputClass=' . $outputClass . ',className=' . $className);
62
63                 // Create an instance if this middleware
64                 $debugInstance = new DebugMiddleware();
65
66                 // Default is that $outputClass may be invalid
67                 $isInitialized = FALSE;
68
69                 // Is there a valid output instance provided?
70                 if ((!is_null($outputClass)) && (is_object($outputClass)) && ($outputClass instanceof OutputStreamer)) {
71                         // Use the given output instance
72                         $debugInstance->setOutputInstance($outputClass);
73
74                         // All fine
75                         $isInitialized = TRUE;
76                 } elseif ((!is_null($outputClass)) && (is_string($outputClass)) && (class_exists($outputClass))) {
77                         // A name for a debug output class has been provided so we try to get it
78                         $outputInstance = ObjectFactory::createObjectByName($outputClass);
79
80                         // Set this as output class
81                         $debugInstance->setOutputInstance($outputInstance);
82
83                         // All fine
84                         $isInitialized = TRUE;
85                 }
86
87                 // Is the output class initialized?
88                 if ($isInitialized === TRUE) {
89                         // Then set class name
90                         $debugInstance->getOutputInstance()->setLoggerClassName($className);
91                 } // END - if
92
93                 // Return instance
94                 return $debugInstance;
95         }
96
97         /**
98          * Getter for an instance of this class
99          *
100          * @return      $selfInstance           An instance of this class
101          */
102         public static final function getSelfInstance() {
103                 return self::$selfInstance;
104         }
105
106         /**
107          * This method shall send debug output which can be HTML code for the
108          * browser or debug lines for a log file, etc. to the registered debug
109          * output instance.
110          *
111          * @param       $outStream      Data we shall 'stream' out to the world
112          * @param       $stripTags      Whether HTML tags shall be stripped out
113          * @return      void
114          */
115         public final function output ($outStream, $stripTags = FALSE) {
116                 // Is the output stream set
117                 if (empty($outStream)) {
118                         // @TODO Initialization phase
119                         return;
120                 } // END - if
121
122                 // Use the output instance
123                 $this->getOutputInstance()->outputStream($outStream, $stripTags);
124         }
125 }
126
127 // [EOF]
128 ?>