Now use 'instanceof' keyword instead of checking for existing methods
[core.git] / inc / classes / middleware / compressor / class_CompressorChannel.php
index 3bcd0cb2aa576d70fdf032198e84defeec500b33..c1b213cfc51172e000ad4552d32c994c1842e751 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -25,7 +25,7 @@ class CompressorChannel extends BaseMiddleware implements Registerable {
        /**
         * Real compressor instance
         */
-       private $compressor = null;
+       private $compressor = NULL;
 
        /**
         * Protected constructor
@@ -38,40 +38,48 @@ class CompressorChannel extends BaseMiddleware implements Registerable {
        }
 
        /**
-        * Create a new compressor channel based a given base directory where
-        * we shall look for compressor classes
+        * Create a new compressor channel.
         *
-        * @param       $baseDir        Directory which holds our compressor classes
-        * @return      $cInstance      A prepared instance of this class
+        * @return      $compressorInstance             A prepared instance of this class
         */
-       public static final function createCompressorChannel ($baseDir) {
+       public static final function createCompressorChannel () {
                // Get new instance
-               $cInstance = new CompressorChannel();
+               $compressorInstance = new CompressorChannel();
 
                // Is the compressor handler set?
                if (
-                          (is_null($cInstance->getCompressor()))
-                       || (!is_object($cInstance->getCompressor()))
-                       || (!method_exists($cInstance->getCompressor(), 'compressStream'))
-                       || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
+                          (is_null($compressorInstance->getCompressor()))
+                       || (!$compressorInstance->getCompressor() instanceof Compressor)
                ) {
+                       // Init base directory
+                       $baseDir =
+                               $compressorInstance->getConfigInstance()->getConfigEntry('base_path') .
+                               $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
+
                        // Get a directory pointer
                        $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
 
                        // Read all directories but no sub directories
-                       while ($dir = $dirPointer->readDirectoryExcept(array('..', '.', '.htaccess', '.svn'))) {
+                       while ($directoryEntry = $dirPointer->readDirectoryExcept(array('..', '.', '.htaccess', '.svn', 'class_NullCompressor.php'))) {
                                // Is this a class file?
-                               if ((substr($dir, 0, 6) == 'class_') && (substr($dir, -4, 4) == '.php')) {
-                                       // Get the compressor's name. That's why you must name
-                                       // your files like your classes and also that's why you
-                                       // must keep on class in one file.
-                                       $className = substr($dir, 6, -4);
+                               if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
+                                       /* Get the compressor's name. That's why you must name
+                                        * your files like your classes and also that's why you
+                                        * must keep on class in one file.
+                                        */
+                                       $className = substr($directoryEntry, 6, -4);
 
                                        // Get an instance from our object factory
                                        $tempInstance = ObjectFactory::createObjectByName($className);
 
+                                       // Is it null?
+                                       if (is_null($tempInstance)) {
+                                               // Then skip to the next one
+                                               continue;
+                                       } // END - if
+
                                        // Set the compressor
-                                       $cInstance->setCompressor($tempInstance);
+                                       $compressorInstance->setCompressor($tempInstance);
 
                                        // No more searches required because we have found a valid compressor stream
                                        break;
@@ -84,17 +92,17 @@ class CompressorChannel extends BaseMiddleware implements Registerable {
 
                // Check again if there is a compressor
                if (
-                          (is_null($cInstance->getCompressor()))
-                       || (!is_object($cInstance->getCompressor()))
-                       || (!method_exists($cInstance->getCompressor(), 'compressStream'))
-                       || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
+                          (is_null($compressorInstance->getCompressor()))
+                       || (!is_object($compressorInstance->getCompressor()))
+                       || (!method_exists($compressorInstance->getCompressor(), 'compressStream'))
+                       || (!method_exists($compressorInstance->getCompressor(), 'decompressStream'))
                ) {
                        // Set the null compressor handler. This should not be configureable!
-                       $cInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
+                       $compressorInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
                } // END - if
 
                // Return the compressor instance
-               return $cInstance;
+               return $compressorInstance;
        }
 
        /**
@@ -112,7 +120,7 @@ class CompressorChannel extends BaseMiddleware implements Registerable {
         * @param               $compressorInstance     The compressor instance we shall use
         * @return      void
         */
-       public final function setCompressor (Compressor $compressorInstance = null) {
+       public final function setCompressor (Compressor $compressorInstance = NULL) {
                $this->compressor = $compressorInstance;
        }