]> git.mxchange.org Git - core.git/blobdiff - framework/main/middleware/compressor/class_CompressorChannel.php
Continued with renaming-season:
[core.git] / framework / main / middleware / compressor / class_CompressorChannel.php
diff --git a/framework/main/middleware/compressor/class_CompressorChannel.php b/framework/main/middleware/compressor/class_CompressorChannel.php
new file mode 100644 (file)
index 0000000..1e7bf82
--- /dev/null
@@ -0,0 +1,156 @@
+<?php
+// Own namespace
+namespace CoreFramework\Middleware\Compressor;
+
+// Import framework stuff
+use CoreFramework\Compressor\Compressor;
+use CoreFramework\Factory\ObjectFactory;
+use CoreFramework\Middleware\BaseMiddleware;
+use CoreFramework\Registry\Registerable;
+
+/**
+ * Middleware class for selecting the right compressor channel
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class CompressorChannel extends BaseMiddleware implements Registerable {
+       /**
+        * Real compressor instance
+        */
+       private $compressor = NULL;
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor!
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Create a new compressor channel.
+        *
+        * @return      $compressorInstance             A prepared instance of this class
+        */
+       public static final function createCompressorChannel () {
+               // Get new instance
+               $compressorInstance = new CompressorChannel();
+
+               // Is the compressor handler set?
+               if (
+                          (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
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
+
+                       // Read all directories but no sub directories, .htaccess files and NullCompressor class
+                       while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
+                               // Debug message
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COMPRESSOR[' . __METHOD__ . ':' . __LINE__ . ']: directoryEntry=' . $directoryEntry);
+
+                               // Is this a class file?
+                               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.
+                                        */
+
+                                       // Base name of compressor class
+                                       $baseClassName = substr($directoryEntry, 6, -4);
+
+                                       // Create full class name (with namespace
+                                       $fullClassName = sprintf(
+                                               'CoreFramework\Compressor\%s\%s',
+                                               str_replace('Compressor', '', $baseClassName),
+                                               $baseClassName
+                                       );
+
+                                       // Get an instance from our object factory
+                                       $tempInstance = ObjectFactory::createObjectByName($fullClassName);
+
+                                       // Is it null?
+                                       if (is_null($tempInstance)) {
+                                               // Then skip to the next one
+                                               continue;
+                                       } // END - if
+
+                                       // Set the compressor
+                                       $compressorInstance->setCompressor($tempInstance);
+
+                                       // No more searches required because we have found a valid compressor stream
+                                       break;
+                               } // END - if
+                       } // END - while
+
+                       // Close the directory
+                       $directoryInstance->closeDirectory();
+               } // END - if
+
+               // Check again if there is a compressor
+               if (
+                          (is_null($compressorInstance->getCompressor()))
+                       || (!is_object($compressorInstance->getCompressor()))
+                       || (!$compressorInstance instanceof Compressor)
+               ) {
+                       // Set the null compressor handler. This should not be configureable!
+                       // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay?
+                       $compressorInstance->setCompressor(ObjectFactory::createObjectByName('CoreFramework\Compressor\Null\NullCompressor'));
+               } // END - if
+
+               // Return the compressor instance
+               return $compressorInstance;
+       }
+
+       /**
+        * Getter for compressor instance
+        *
+        * @return      $compressor     The compressor instance
+        */
+       public final function getCompressor () {
+               return $this->compressor;
+       }
+
+       /**
+        * Setter for compressor
+        *
+        * @param               $compressorInstance     The compressor instance we shall use
+        * @return      void
+        */
+       public final function setCompressor (Compressor $compressorInstance = NULL) {
+               $this->compressor = $compressorInstance;
+       }
+
+       /**
+        * Getter for the file extension of the current compressor
+        */
+       public final function getCompressorExtension () {
+               // Get compressor extension from current compressor
+               return $this->getCompressor()->getCompressorExtension();
+       }
+
+}