Some rewrites:
[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
56         /**
57          * Create a new debug output system.
58          * If no output is given this class is currently being used for back-fall.
59          * This fall-back mechanism will become deprecated very soon.
60          *
61          * @param       $outputClass    The class name which we shall use for
62          *                                                      registering the *real* debug output
63          * @param       $className              Class where a output should be created and
64          *                                                      configured for
65          * @return      $debugInstance  An instance of this middleware class
66          */
67         public static final function createDebugMiddleware ($outputClass, $className) {
68                 //* DEBUG-DIE: */ die(__METHOD__.': outputClass=' . $outputClass . ',className=' . $className);
69
70                 // Create an instance if this middleware
71                 $debugInstance = new DebugMiddleware();
72
73                 // Default is that $outputClass may be invalid
74                 $isInitialized = false;
75
76                 // Is there a valid output instance provided?
77                 if ((!is_null($outputClass)) && (is_object($outputClass)) && ($outputClass instanceof OutputStreamer)) {
78                         // Use the given output instance
79                         $debugInstance->setOutputInstance($outputClass);
80
81                         // All fine
82                         $isInitialized = true;
83                 } elseif ((!is_null($outputClass)) && (is_string($outputClass)) && (class_exists($outputClass))) {
84                         // A name for a debug output class has been provided so we try to get it
85                         $outputInstance = ObjectFactory::createObjectByName($outputClass);
86
87                         // Set this as output class
88                         $debugInstance->setOutputInstance($outputInstance);
89
90                         // All fine
91                         $isInitialized = true;
92                 }
93
94                 // Is the output class initialized?
95                 if ($isInitialized === true) {
96                         // Then set class name
97                         $debugInstance->getOutputInstance()->setLoggerClassName($className);
98                 } // END - if
99
100                 // Return instance
101                 return $debugInstance;
102         }
103
104         /**
105          * Getter for an instance of this class
106          *
107          * @return      $selfInstance           An instance of this class
108          */
109         public static final function getSelfInstance() {
110                 return self::$selfInstance;
111         }
112
113         /**
114          * This method shall send debug output which can be HTML code for the
115          * browser or debug lines for a log file, etc. to the registered debug
116          * output instance.
117          *
118          * @param       $outStream      Data we shall 'stream' out to the world
119          * @param       $stripTags      Whether HTML tags shall be stripped out
120          * @return      void
121          */
122         public final function output ($outStream, $stripTags = false) {
123                 // Is the output stream set
124                 if (empty($outStream)) {
125                         // @TODO Initialization phase
126                         return;
127                 } elseif (is_null($this->getOutputInstance())) {
128                         // Should not be NULL
129                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
130                 }
131
132                 // Use the output instance
133                 $this->getOutputInstance()->outputStream($outStream, $stripTags);
134         }
135
136 }