3 * Middleware class for selecting the right compressor channel
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class CompressorChannel extends BaseMiddleware implements Registerable {
26 * Real compressor instance
28 private $compressor = NULL;
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor!
37 parent::__construct(__CLASS__);
41 * Create a new compressor channel.
43 * @return $compressorInstance A prepared instance of this class
45 public static final function createCompressorChannel () {
47 $compressorInstance = new CompressorChannel();
49 // Is the compressor handler set?
51 (is_null($compressorInstance->getCompressor()))
52 || (!$compressorInstance->getCompressor() instanceof Compressor)
54 // Init base directory
56 $compressorInstance->getConfigInstance()->getConfigEntry('base_path') .
57 $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
59 // Get a directory pointer
60 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
62 // Read all directories but no sub directories, .htaccess files and NullCompressor class
63 while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
65 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('COMPRESSOR[' . __METHOD__ . ':' . __LINE__ . ']: directoryEntry=' . $directoryEntry);
67 // Is this a class file?
68 if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
69 /* Get the compressor's name. That's why you must name
70 * your files like your classes and also that's why you
71 * must keep on class in one file.
73 $className = substr($directoryEntry, 6, -4);
75 // Get an instance from our object factory
76 $tempInstance = ObjectFactory::createObjectByName($className);
79 if (is_null($tempInstance)) {
80 // Then skip to the next one
85 $compressorInstance->setCompressor($tempInstance);
87 // No more searches required because we have found a valid compressor stream
92 // Close the directory
93 $directoryInstance->closeDirectory();
96 // Check again if there is a compressor
98 (is_null($compressorInstance->getCompressor()))
99 || (!is_object($compressorInstance->getCompressor()))
100 || (!$compressorInstance instanceof Compressor)
102 // Set the null compressor handler. This should not be configureable!
103 // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay?
104 $compressorInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
107 // Return the compressor instance
108 return $compressorInstance;
112 * Getter for compressor instance
114 * @return $compressor The compressor instance
116 public final function getCompressor () {
117 return $this->compressor;
121 * Setter for compressor
123 * @param $compressorInstance The compressor instance we shall use
126 public final function setCompressor (Compressor $compressorInstance = NULL) {
127 $this->compressor = $compressorInstance;
131 * Getter for the file extension of the current compressor
133 public final function getCompressorExtension () {
134 // Get compressor extension from current compressor
135 return $this->getCompressor()->getCompressorExtension();