0.3.0 inital import
[mailer.git] / inc / classes / middleware / debug / class_DebugMiddleware.php
diff --git a/inc/classes/middleware/debug/class_DebugMiddleware.php b/inc/classes/middleware/debug/class_DebugMiddleware.php
new file mode 100644 (file)
index 0000000..1835ac8
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+/**
+ * The middlware debug output system. A *real* or concrete output class shall
+ * become registered with this middleware because the back-fall class will
+ * become deprecated soon.
+ */
+class DebugMiddleware extends BaseMiddleware {
+       /**
+        * The concrete output instance
+        */
+       private $outputHandler = null;
+
+       /**
+        * An instance of this class
+        */
+       private static $thisInstance = null;
+
+       /**
+        * Private constructor
+        *
+        * @return      void
+        */
+       private final function __construct () {
+               // Call parent constructor
+               parent::constructor(__CLASS__);
+
+               // Set description
+               $this->setPartDescr("Debug-Ausgabe-Handler");
+
+               // Create an unique ID
+               $this->createUniqueID();
+
+               // Set own instance
+               self::$thisInstance = $this;
+       }
+
+       /**
+        * Create a new debug output system.
+        * If no output is given this class is currently being used for back-fall.
+        * This fall-back mechanism will become deprecated very soon.
+        *
+        * @param               $debuggerClass       The class name which we shall use for
+        *                                                      registering the *real* debug output 
+        * @return      $debugInstance          An instance of this middleware class
+        */
+       public final static function createDebugMiddleware ($debuggerClass) {
+               // Create an instance if this middleware
+               $debugInstance = new DebugMiddleware();
+
+               // Is there a valid output handler provided?
+               if ((!is_null($debuggerClass)) && (is_object($debuggerClass)) && (method_exists($debuggerClass, 'outputStream'))) {
+                       // Use the given output system
+                       $debugInstance->setOutputHandler($debuggerClass);
+               } elseif ((!is_null($debuggerClass)) && (is_string($debuggerClass)) && (class_exists($debuggerClass))) {
+                       // A name for a debug output class has been provided so we try to get it
+                       $eval = sprintf("\$debuggerClass = %s::create%s();",
+                               $debuggerClass,
+                               $debuggerClass
+                       );
+
+                       // Run the constructed name
+                       @eval($eval);
+
+                       // Was this successfull?
+                       if ((is_object($debuggerClass)) && (method_exists($debuggerClass, "outputStream"))) {
+                               // Set this as output class
+                               $debugInstance->setOutputHandler($debuggerClass);
+                       } else {
+                               // No object or method is missing use fall-back
+                               throw new MissingMethodException(array($debuggerClass, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
+                       }
+               }
+
+               // Return instance
+               return $debugInstance;
+       }
+
+       /**
+        * Getter for an instance of this class
+        *
+        * @return      $thisInstance           An instance of this class
+        */
+       public final static function getInstance() {
+               return self::$thisInstance;
+       }
+
+       /**
+        * Setter for output handler
+        *
+        * @return      void
+        */
+       public final function setOutputHandler ($outputHandler) {
+               $this->outputHandler = $outputHandler;
+       }
+
+       /**
+        * This method shall send debug output which can be HTML code for the
+        * browser or debug lines for a log file, etc. to the registered debug
+        * output handler.
+        *
+        * @return      void
+        */
+       public final function output ($outStream) {
+               // Check if the output handler is valid
+               if (is_null($this->outputHandler)) {
+                       // Debug output handler was not set
+                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+               } elseif (!is_object($this->outputHandler)) {
+                       // The debug output handler is not an object
+                       throw new NoObjectException($this->ouputHandler, self::EXCEPTION_IS_NO_OBJECT);
+               } elseif (!method_exists($this->outputHandler, 'outputStream')) {
+                       // The required method outputStream() is missing
+                       throw new MissingMethodException(array($this->outputHandler, 'outputStream'), self::EXCEPTION_MISSING_METHOD);
+               }
+
+               // Is the output stream set
+               if (empty($outStream)) {
+                       // Initialization phase
+                       return;
+               }
+
+               // Use the output handler
+               $this->outputHandler->outputStream($outStream);
+       }
+}
+
+// [EOF]
+?>