3 * Middleware class for selecting the right compressor channel
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 based a given base directory where
42 * we shall look for compressor classes
44 * @param $baseDir Directory which holds our compressor classes
45 * @return $cInstance A prepared instance of this class
47 public final static function createCompressorChannel ($baseDir) {
49 $cInstance = new CompressorChannel();
51 // Is the compressor handler set?
53 (is_null($cInstance->getCompressor()))
54 || (!is_object($cInstance->getCompressor()))
55 || (!method_exists($cInstance->getCompressor(), 'compressStream'))
56 || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
58 // Get a directory pointer
59 $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
61 // Read all directories but no sub directories
62 while ($dir = $dirPointer->readDirectoryExcept(array('..', '.', '.htaccess', '.svn'))) {
63 // Is this a class file?
64 if ((substr($dir, 0, 6) == 'class_') && (substr($dir, -4, 4) == '.php')) {
65 // Get the compressor's name. That's why you must name
66 // your files like your classes and also that's why you
67 // must keep on class in one file.
68 $className = substr($dir, 6, -4);
70 // Get an instance from our object factory
71 $tempInstance = ObjectFactory::createObjectByName($className);
74 $cInstance->setCompressor($tempInstance);
76 // No more searches required because we have found a valid compressor stream
81 // Close the directory
82 $dirPointer->closeDirectory();
85 // Check again if there is a compressor
87 (is_null($cInstance->getCompressor()))
88 || (!is_object($cInstance->getCompressor()))
89 || (!method_exists($cInstance->getCompressor(), 'compressStream'))
90 || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
92 // Set the null compressor handler. This should not be configureable!
93 $cInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
96 // Return the compressor instance
101 * Getter for compressor instance
103 * @return $compressor The compressor instance
105 public final function getCompressor () {
106 return $this->compressor;
110 * Setter for compressor
112 * @param $compressorInstance The compressor instance we shall use
115 public final function setCompressor (Compressor $compressorInstance = null) {
116 $this->compressor = $compressorInstance;
120 * Getter for the file extension of the current compressor
122 public final function getCompressorExtension () {
123 // Get compressor extension from current compressor
124 return $this->getCompressor()->getCompressorExtension();