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