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
11 * @link http://www.ship-simu.org
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 class DebugMiddleware extends BaseMiddleware {
28 * The concrete output instance
30 private $outputHandler = null;
33 * An instance of this class
35 private static $thisInstance = null;
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
47 $this->setObjectDescription("Debug-Ausgabe-Handler");
49 // Create an unique ID
50 $this->createUniqueID();
53 self::$thisInstance = $this;
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.
61 * @param $debuggerClass The class name which we shall use for
62 * registering the *real* debug output
63 * @return $debugInstance An instance of this middleware class
65 public final static function createDebugMiddleware ($debuggerClass) {
66 // Create an instance if this middleware
67 $debugInstance = new DebugMiddleware();
69 // Is there a valid output handler provided?
70 if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && (method_exists($debuggerClass, 'outputStream'))) {
71 // Use the given output system
72 $debugInstance->setOutputHandler($debuggerClass);
73 } elseif ((!is_null($debuggerClass)) && (is_string($debuggerClass)) && (class_exists($debuggerClass))) {
74 // A name for a debug output class has been provided so we try to get it
75 $eval = sprintf("\$debuggerClass = %s::create%s();",
80 // Run the constructed name
83 // Was this successfull?
84 if ((is_object($debuggerClass)) && (method_exists($debuggerClass, "outputStream"))) {
85 // Set this as output class
86 $debugInstance->setOutputHandler($debuggerClass);
88 // No object or method is missing use fall-back
89 throw new MissingMethodException(array($debuggerClass, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
94 return $debugInstance;
98 * Getter for an instance of this class
100 * @return $thisInstance An instance of this class
102 public final static function getInstance() {
103 return self::$thisInstance;
107 * Setter for output handler
111 public final function setOutputHandler ($outputHandler) {
112 $this->outputHandler = $outputHandler;
116 * This method shall send debug output which can be HTML code for the
117 * browser or debug lines for a log file, etc. to the registered debug
122 public final function output ($outStream) {
123 // Check if the output handler is valid
124 if (is_null($this->outputHandler)) {
125 // Debug output handler was not set
126 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
127 } elseif (!is_object($this->outputHandler)) {
128 // The debug output handler is not an object
129 throw new NoObjectException($this->ouputHandler, self::EXCEPTION_IS_NO_OBJECT);
130 } elseif (!method_exists($this->outputHandler, 'outputStream')) {
131 // The required method outputStream() is missing
132 throw new MissingMethodException(array($this->outputHandler, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
135 // Is the output stream set
136 if (empty($outStream)) {
137 // Initialization phase
141 // Use the output handler
142 $this->outputHandler->outputStream($outStream);