05fbad06ba59ace7c40a090953fbf1f72310e535
[shipsimu.git] / inc / classes / middleware / compressor / class_CompressorChannel.php
1 <?php
2 /**
3  * Middleware for selecting the right compressor channel
4  *
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
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 {
25         // Output handler instance
26         private $compressor = null;
27
28         // Public constructor
29         private function __construct () {
30                 // Call parent constructor!
31                 parent::constructor(__CLASS__);
32
33                 // Set description
34                 $this->setPartDescr("Komprimierungshandler");
35
36                 // Create an unique ID
37                 $this->createUniqueID();
38         }
39
40         // Create a new compressor channel based a given compression handler
41         public final static function createCompressorChannel ($baseDir) {
42                 // Get new instance
43                 $cInstance = new CompressorChannel();
44
45                 // Is the compressor handler set?
46                 if (
47                            (is_null($cInstance->getCompressor()))
48                         || (!is_object($cInstance->getCompressor()))
49                         || (!method_exists($cInstance->getCompressor(), 'compressStream'))
50                         || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
51                 ) {
52                         // Get a directory pointer
53                         $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
54
55                         // Read all directories but no sub directories
56                         while ($dir = $dirPointer->readDirectoryExcept(array("..", ".", ".htaccess"))) {
57                                 // Is this a class file?
58                                 if ((substr($dir, 0, 6) == "class_") && (substr($dir, -4, 4) == $cInstance->getConfigInstance()->readConfig("php_extension"))) {
59                                         // Get the compressor's name. That's why you must name
60                                         // your files like your classes and also that's why you
61                                         // must keep on class in one file.
62                                         $class = substr($dir, 6, -4);
63
64                                         // Create eval command
65                                         $eval = sprintf("\$tempInstance = %s::create%s();",
66                                                 $class,
67                                                 $class
68                                         );
69
70                                         // Debug message
71                                         if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
72                                                 $cInstance->__toString(),
73                                                 htmlentities($eval)
74                                         ));
75
76                                         // Run it. This will create an instance to the current class
77                                         eval($eval);
78
79                                         // Is the instance valid? We have the stream handler here
80                                         if ((!is_null($tempInstance)) && (method_exists($tempInstance, 'compressStream')) && (method_exists($tempInstance, 'decompressStream'))) {
81                                                 // Okay, this handler is valid
82                                                 $cInstance->setCompressor($tempInstance);
83
84                                                 // No more searches required because we have found a valid compressor stream
85                                                 break;
86                                         }
87                                 }
88                         }
89
90                         // Close the directory
91                         $dirPointer->closeDirectory();
92                 }
93
94                 // Check again if there is a compressor
95                 if (
96                            (is_null($cInstance->getCompressor()))
97                         || (!is_object($cInstance->getCompressor()))
98                         || (!method_exists($cInstance->getCompressor(), 'compressStream'))
99                         || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
100                 ) {
101                         // Set the null compressor handler
102                         $cInstance->setCompressor(NullCompressor::createNullCompressor());
103                 }
104
105                 // Return the compressor instance
106                 return $cInstance;
107         }
108
109         /**
110          * Getter for compressor instance
111          *
112          * @return      $compressor     The compressor instance
113          */
114         public final function getCompressor () {
115                 return $this->compressor;
116         }
117
118         /**
119          * Setter for compressor
120          *
121          * @param               $compressorInstance     The compressor instance we shall use
122          * @return      void
123          */
124         public final function setCompressor (Compressor $compressorInstance) {
125                 $this->compressor = $compressorInstance;
126         }
127
128         /**
129          * Getter for the file extension of the current compressor
130          */
131         public final function getCompressorExtension () {
132                 // Get compressor extension from current compressor
133                 return $this->getCompressor()->getCompressorExtension();
134         }
135 }
136
137 // [EOF]
138 ?>