3 * The middlware debug output system. A *real* or concrete output class shall
4 * become registered with this middleware because the back-fall class will
5 * become deprecated soon.
7 * @author Roland Haeder <webmaster@ship-simu.org>
9 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
10 * @license GNU GPL 3.0 or any newer version
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class DebugMiddleware extends BaseMiddleware {
27 * The concrete output instance
29 private $outputHandler = null;
32 * An instance of this class
34 private static $thisInstance = null;
41 private final function __construct () {
42 // Call parent constructor
43 parent::constructor(__CLASS__);
46 $this->setPartDescr("Debug-Ausgabe-Handler");
48 // Create an unique ID
49 $this->createUniqueID();
52 self::$thisInstance = $this;
56 * Create a new debug output system.
57 * If no output is given this class is currently being used for back-fall.
58 * This fall-back mechanism will become deprecated very soon.
60 * @param $debuggerClass The class name which we shall use for
61 * registering the *real* debug output
62 * @return $debugInstance An instance of this middleware class
64 public final static function createDebugMiddleware ($debuggerClass) {
65 // Create an instance if this middleware
66 $debugInstance = new DebugMiddleware();
68 // Is there a valid output handler provided?
69 if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && (method_exists($debuggerClass, 'outputStream'))) {
70 // Use the given output system
71 $debugInstance->setOutputHandler($debuggerClass);
72 } elseif ((!is_null($debuggerClass)) && (is_string($debuggerClass)) && (class_exists($debuggerClass))) {
73 // A name for a debug output class has been provided so we try to get it
74 $eval = sprintf("\$debuggerClass = %s::create%s();",
79 // Run the constructed name
82 // Was this successfull?
83 if ((is_object($debuggerClass)) && (method_exists($debuggerClass, "outputStream"))) {
84 // Set this as output class
85 $debugInstance->setOutputHandler($debuggerClass);
87 // No object or method is missing use fall-back
88 throw new MissingMethodException(array($debuggerClass, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
93 return $debugInstance;
97 * Getter for an instance of this class
99 * @return $thisInstance An instance of this class
101 public final static function getInstance() {
102 return self::$thisInstance;
106 * Setter for output handler
110 public final function setOutputHandler ($outputHandler) {
111 $this->outputHandler = $outputHandler;
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
121 public final function output ($outStream) {
122 // Check if the output handler is valid
123 if (is_null($this->outputHandler)) {
124 // Debug output handler was not set
125 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
126 } elseif (!is_object($this->outputHandler)) {
127 // The debug output handler is not an object
128 throw new NoObjectException($this->ouputHandler, self::EXCEPTION_IS_NO_OBJECT);
129 } elseif (!method_exists($this->outputHandler, 'outputStream')) {
130 // The required method outputStream() is missing
131 throw new MissingMethodException(array($this->outputHandler, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
134 // Is the output stream set
135 if (empty($outStream)) {
136 // Initialization phase
140 // Use the output handler
141 $this->outputHandler->outputStream($outStream);