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