3 namespace CoreFramework\Channel\Compressor;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Middleware\BaseMiddleware;
10 * Middleware class for selecting the right compressor channel
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CompressorChannel extends BaseMiddleware implements Registerable {
33 * Real compressor instance
35 private $compressor = NULL;
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor!
44 parent::__construct(__CLASS__);
48 * Create a new compressor channel.
50 * @return $compressorInstance A prepared instance of this class
52 public static final function createCompressorChannel () {
54 $compressorInstance = new CompressorChannel();
56 // Is the compressor handler set?
58 (is_null($compressorInstance->getCompressor()))
59 || (!$compressorInstance->getCompressor() instanceof Compressor)
61 // Init base directory
63 $compressorInstance->getConfigInstance()->getConfigEntry('base_path') .
64 $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
66 // Get a directory pointer
67 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
69 // Read all directories but no sub directories, .htaccess files and NullCompressor class
70 while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('COMPRESSOR[' . __METHOD__ . ':' . __LINE__ . ']: directoryEntry=' . $directoryEntry);
74 // Is this a class file?
75 if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
76 /* Get the compressor's name. That's why you must name
77 * your files like your classes and also that's why you
78 * must keep on class in one file.
80 $className = substr($directoryEntry, 6, -4);
82 // Get an instance from our object factory
83 $tempInstance = ObjectFactory::createObjectByName($className);
86 if (is_null($tempInstance)) {
87 // Then skip to the next one
92 $compressorInstance->setCompressor($tempInstance);
94 // No more searches required because we have found a valid compressor stream
99 // Close the directory
100 $directoryInstance->closeDirectory();
103 // Check again if there is a compressor
105 (is_null($compressorInstance->getCompressor()))
106 || (!is_object($compressorInstance->getCompressor()))
107 || (!$compressorInstance instanceof Compressor)
109 // Set the null compressor handler. This should not be configureable!
110 // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay?
111 $compressorInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
114 // Return the compressor instance
115 return $compressorInstance;
119 * Getter for compressor instance
121 * @return $compressor The compressor instance
123 public final function getCompressor () {
124 return $this->compressor;
128 * Setter for compressor
130 * @param $compressorInstance The compressor instance we shall use
133 public final function setCompressor (Compressor $compressorInstance = NULL) {
134 $this->compressor = $compressorInstance;
138 * Getter for the file extension of the current compressor
140 public final function getCompressorExtension () {
141 // Get compressor extension from current compressor
142 return $this->getCompressor()->getCompressorExtension();