3 namespace CoreFramework\Channel\Compressor;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Middleware\BaseMiddleware;
8 use CoreFramework\Registry\Registerable;
11 * Middleware class for selecting the right compressor channel
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class CompressorChannel extends BaseMiddleware implements Registerable {
34 * Real compressor instance
36 private $compressor = NULL;
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor!
45 parent::__construct(__CLASS__);
49 * Create a new compressor channel.
51 * @return $compressorInstance A prepared instance of this class
53 public static final function createCompressorChannel () {
55 $compressorInstance = new CompressorChannel();
57 // Is the compressor handler set?
59 (is_null($compressorInstance->getCompressor()))
60 || (!$compressorInstance->getCompressor() instanceof Compressor)
62 // Init base directory
64 $compressorInstance->getConfigInstance()->getConfigEntry('base_path') .
65 $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
67 // Get a directory pointer
68 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
70 // Read all directories but no sub directories, .htaccess files and NullCompressor class
71 while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
73 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COMPRESSOR[' . __METHOD__ . ':' . __LINE__ . ']: directoryEntry=' . $directoryEntry);
75 // Is this a class file?
76 if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
77 /* Get the compressor's name. That's why you must name
78 * your files like your classes and also that's why you
79 * must keep on class in one file.
81 $className = substr($directoryEntry, 6, -4);
83 // Get an instance from our object factory
84 $tempInstance = ObjectFactory::createObjectByName($className);
87 if (is_null($tempInstance)) {
88 // Then skip to the next one
93 $compressorInstance->setCompressor($tempInstance);
95 // No more searches required because we have found a valid compressor stream
100 // Close the directory
101 $directoryInstance->closeDirectory();
104 // Check again if there is a compressor
106 (is_null($compressorInstance->getCompressor()))
107 || (!is_object($compressorInstance->getCompressor()))
108 || (!$compressorInstance instanceof Compressor)
110 // Set the null compressor handler. This should not be configureable!
111 // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay?
112 $compressorInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
115 // Return the compressor instance
116 return $compressorInstance;
120 * Getter for compressor instance
122 * @return $compressor The compressor instance
124 public final function getCompressor () {
125 return $this->compressor;
129 * Setter for compressor
131 * @param $compressorInstance The compressor instance we shall use
134 public final function setCompressor (Compressor $compressorInstance = NULL) {
135 $this->compressor = $compressorInstance;
139 * Getter for the file extension of the current compressor
141 public final function getCompressorExtension () {
142 // Get compressor extension from current compressor
143 return $this->getCompressor()->getCompressorExtension();