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 class DebugMiddleware extends BaseMiddleware {
9 * The concrete output instance
11 private $outputHandler = null;
14 * An instance of this class
16 private static $thisInstance = null;
23 private final function __construct () {
24 // Call parent constructor
25 parent::constructor(__CLASS__);
28 $this->setPartDescr("Debug-Ausgabe-Handler");
30 // Create an unique ID
31 $this->createUniqueID();
34 self::$thisInstance = $this;
38 * Create a new debug output system.
39 * If no output is given this class is currently being used for back-fall.
40 * This fall-back mechanism will become deprecated very soon.
42 * @param $debuggerClass The class name which we shall use for
43 * registering the *real* debug output
44 * @return $debugInstance An instance of this middleware class
46 public final static function createDebugMiddleware ($debuggerClass) {
47 // Create an instance if this middleware
48 $debugInstance = new DebugMiddleware();
50 // Is there a valid output handler provided?
51 if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && (method_exists($debuggerClass, 'outputStream'))) {
52 // Use the given output system
53 $debugInstance->setOutputHandler($debuggerClass);
54 } elseif ((!is_null($debuggerClass)) && (is_string($debuggerClass)) && (class_exists($debuggerClass))) {
55 // A name for a debug output class has been provided so we try to get it
56 $eval = sprintf("\$debuggerClass = %s::create%s();",
61 // Run the constructed name
64 // Was this successfull?
65 if ((is_object($debuggerClass)) && (method_exists($debuggerClass, "outputStream"))) {
66 // Set this as output class
67 $debugInstance->setOutputHandler($debuggerClass);
69 // No object or method is missing use fall-back
70 throw new MissingMethodException(array($debuggerClass, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
75 return $debugInstance;
79 * Getter for an instance of this class
81 * @return $thisInstance An instance of this class
83 public final static function getInstance() {
84 return self::$thisInstance;
88 * Setter for output handler
92 public final function setOutputHandler ($outputHandler) {
93 $this->outputHandler = $outputHandler;
97 * This method shall send debug output which can be HTML code for the
98 * browser or debug lines for a log file, etc. to the registered debug
103 public final function output ($outStream) {
104 // Check if the output handler is valid
105 if (is_null($this->outputHandler)) {
106 // Debug output handler was not set
107 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
108 } elseif (!is_object($this->outputHandler)) {
109 // The debug output handler is not an object
110 throw new NoObjectException($this->ouputHandler, self::EXCEPTION_IS_NO_OBJECT);
111 } elseif (!method_exists($this->outputHandler, 'outputStream')) {
112 // The required method outputStream() is missing
113 throw new MissingMethodException(array($this->outputHandler, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
116 // Is the output stream set
117 if (empty($outStream)) {
118 // Initialization phase
122 // Use the output handler
123 $this->outputHandler->outputStream($outStream);