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