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