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