81b2ffff97c3b39b2bd9860aa980576b2fedfde6
[core.git] / inc / classes / middleware / compressor / class_CompressorChannel.php
1 <?php
2 /**
3  * Middleware class for selecting the right compressor channel
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class CompressorChannel extends BaseMiddleware implements Registerable {
25         /**
26          * Real compressor instance
27          */
28         private $compressor = NULL;
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor!
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Create a new compressor channel.
42          *
43          * @return      $compressorInstance             A prepared instance of this class
44          */
45         public static final function createCompressorChannel () {
46                 // Get new instance
47                 $compressorInstance = new CompressorChannel();
48
49                 // Is the compressor handler set?
50                 if (
51                            (is_null($compressorInstance->getCompressor()))
52                         || (!is_object($compressorInstance->getCompressor()))
53                         || (!method_exists($compressorInstance->getCompressor(), 'compressStream'))
54                         || (!method_exists($compressorInstance->getCompressor(), 'decompressStream'))
55                 ) {
56                         // Init base directory
57                         $baseDir =
58                                 $compressorInstance->getConfigInstance()->getConfigEntry('base_path') .
59                                 $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
60
61                         // Get a directory pointer
62                         $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
63
64                         // Read all directories but no sub directories
65                         while ($directoryEntry = $dirPointer->readDirectoryExcept(array('..', '.', '.htaccess', '.svn', 'class_NullCompressor.php'))) {
66                                 // Is this a class file?
67                                 if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
68                                         /* Get the compressor's name. That's why you must name
69                                          * your files like your classes and also that's why you
70                                          * must keep on class in one file.
71                                          */
72                                         $className = substr($directoryEntry, 6, -4);
73
74                                         // Get an instance from our object factory
75                                         $tempInstance = ObjectFactory::createObjectByName($className);
76
77                                         // Is it null?
78                                         if (is_null($tempInstance)) {
79                                                 // Then skip to the next one
80                                                 continue;
81                                         } // END - if
82
83                                         // Set the compressor
84                                         $compressorInstance->setCompressor($tempInstance);
85
86                                         // No more searches required because we have found a valid compressor stream
87                                         break;
88                                 } // END - if
89                         } // END - while
90
91                         // Close the directory
92                         $dirPointer->closeDirectory();
93                 } // END - if
94
95                 // Check again if there is a compressor
96                 if (
97                            (is_null($compressorInstance->getCompressor()))
98                         || (!is_object($compressorInstance->getCompressor()))
99                         || (!method_exists($compressorInstance->getCompressor(), 'compressStream'))
100                         || (!method_exists($compressorInstance->getCompressor(), 'decompressStream'))
101                 ) {
102                         // Set the null compressor handler. This should not be configureable!
103                         $compressorInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
104                 } // END - if
105
106                 // Return the compressor instance
107                 return $compressorInstance;
108         }
109
110         /**
111          * Getter for compressor instance
112          *
113          * @return      $compressor     The compressor instance
114          */
115         public final function getCompressor () {
116                 return $this->compressor;
117         }
118
119         /**
120          * Setter for compressor
121          *
122          * @param               $compressorInstance     The compressor instance we shall use
123          * @return      void
124          */
125         public final function setCompressor (Compressor $compressorInstance = NULL) {
126                 $this->compressor = $compressorInstance;
127         }
128
129         /**
130          * Getter for the file extension of the current compressor
131          */
132         public final function getCompressorExtension () {
133                 // Get compressor extension from current compressor
134                 return $this->getCompressor()->getCompressorExtension();
135         }
136 }
137
138 // [EOF]
139 ?>