3 namespace Org\Mxchange\CoreFramework\Middleware\Compressor;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Compressor\Compressor;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 * Middleware class for selecting the right compressor channel
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class CompressorChannel extends BaseMiddleware implements Registerable {
35 * Real compressor instance
37 private $compressor = NULL;
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor!
46 parent::__construct(__CLASS__);
50 * Create a new compressor channel.
52 * @return $compressorInstance A prepared instance of this class
54 public static final function createCompressorChannel () {
56 $compressorInstance = new CompressorChannel();
58 // Is the compressor handler set?
60 (is_null($compressorInstance->getCompressor()))
61 || (!$compressorInstance->getCompressor() instanceof Compressor)
63 // Init base directory
65 $compressorInstance->getConfigInstance()->getConfigEntry('framework_base_path') .
66 $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
68 // Get a directory pointer
69 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
71 // Read all directories but no sub directories, .htaccess files and NullCompressor class
72 while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
74 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COMPRESSOR[' . __METHOD__ . ':' . __LINE__ . ']: directoryEntry=' . $directoryEntry);
76 // Is this a class file?
77 if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
78 /* Get the compressor's name. That's why you must name
79 * your files like your classes and also that's why you
80 * must keep on class in one file.
83 // Base name of compressor class
84 $baseClassName = substr($directoryEntry, 6, -4);
86 // Create full class name (with namespace
87 $fullClassName = sprintf(
88 'Org\Mxchange\CoreFramework\Compressor\%s\%s',
89 str_replace('Compressor', '', $baseClassName),
93 // Get an instance from our object factory
94 $tempInstance = ObjectFactory::createObjectByName($fullClassName);
97 if (is_null($tempInstance)) {
98 // Then skip to the next one
102 // Set the compressor
103 $compressorInstance->setCompressor($tempInstance);
105 // No more searches required because we have found a valid compressor stream
110 // Close the directory
111 $directoryInstance->closeDirectory();
114 // Check again if there is a compressor
116 (is_null($compressorInstance->getCompressor()))
117 || (!is_object($compressorInstance->getCompressor()))
118 || (!$compressorInstance instanceof Compressor)
120 // Set the null compressor handler. This should not be configureable!
121 // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay?
122 $compressorInstance->setCompressor(ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Compressor\Null\NullCompressor'));
125 // Return the compressor instance
126 return $compressorInstance;
130 * Getter for compressor instance
132 * @return $compressor The compressor instance
134 public final function getCompressor () {
135 return $this->compressor;
139 * Setter for compressor
141 * @param $compressorInstance The compressor instance we shall use
144 public final function setCompressor (Compressor $compressorInstance = NULL) {
145 $this->compressor = $compressorInstance;
149 * Getter for the file extension of the current compressor
151 public final function getCompressorExtension () {
152 // Get compressor extension from current compressor
153 return $this->getCompressor()->getCompressorExtension();