createUniqueID -> generateUniqueId renamed, dataset criteria added, registration...
[shipsimu.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, this is free software
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 {
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                 // Set description
40                 $this->setObjectDescription("Komprimierungshandler");
41
42                 // Create an unique ID
43                 $this->generateUniqueId();
44         }
45
46         /**
47          * Create a new compressor channel based a given base directory where
48          * we shall look for compressor classes
49          *
50          * @param       $baseDir        Directory which holds our compressor classes
51          * @return      $cInstance      A prepared instance of this class
52          */
53         public final static function createCompressorChannel ($baseDir) {
54                 // Get new instance
55                 $cInstance = new CompressorChannel();
56
57                 // Is the compressor handler set?
58                 if (
59                            (is_null($cInstance->getCompressor()))
60                         || (!is_object($cInstance->getCompressor()))
61                         || (!method_exists($cInstance->getCompressor(), 'compressStream'))
62                         || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
63                 ) {
64                         // Get a directory pointer
65                         $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
66
67                         // Read all directories but no sub directories
68                         while ($dir = $dirPointer->readDirectoryExcept(array("..", ".", ".htaccess", ".svn"))) {
69                                 // Is this a class file?
70                                 if ((substr($dir, 0, 6) == "class_") && (substr($dir, -4, 4) == FrameworkConfiguration::getInstance()->readConfig('php_extension'))) {
71                                         // Get the compressor's name. That's why you must name
72                                         // your files like your classes and also that's why you
73                                         // must keep on class in one file.
74                                         $class = substr($dir, 6, -4);
75
76                                         // Create eval command
77                                         $eval = sprintf("\$tempInstance = %s::create%s();",
78                                                 $class,
79                                                 $class
80                                         );
81
82                                         // Debug message
83                                         if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
84                                                 $cInstance->__toString(),
85                                                 htmlentities($eval)
86                                         ));
87
88                                         // Run it. This will create an instance to the current class
89                                         eval($eval);
90
91                                         // Is the instance valid? We have the stream handler here
92                                         if ((!is_null($tempInstance)) && (method_exists($tempInstance, 'compressStream')) && (method_exists($tempInstance, 'decompressStream'))) {
93                                                 // Okay, this handler is valid
94                                                 $cInstance->setCompressor($tempInstance);
95
96                                                 // No more searches required because we have found a valid compressor stream
97                                                 break;
98                                         }
99                                 }
100                         }
101
102                         // Close the directory
103                         $dirPointer->closeDirectory();
104                 }
105
106                 // Check again if there is a compressor
107                 if (
108                            (is_null($cInstance->getCompressor()))
109                         || (!is_object($cInstance->getCompressor()))
110                         || (!method_exists($cInstance->getCompressor(), 'compressStream'))
111                         || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
112                 ) {
113                         // Set the null compressor handler. This should not be configureable!
114                         $cInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
115                 }
116
117                 // Return the compressor instance
118                 return $cInstance;
119         }
120
121         /**
122          * Getter for compressor instance
123          *
124          * @return      $compressor     The compressor instance
125          */
126         public final function getCompressor () {
127                 return $this->compressor;
128         }
129
130         /**
131          * Setter for compressor
132          *
133          * @param               $compressorInstance     The compressor instance we shall use
134          * @return      void
135          */
136         public final function setCompressor (Compressor $compressorInstance) {
137                 $this->compressor = $compressorInstance;
138         }
139
140         /**
141          * Getter for the file extension of the current compressor
142          */
143         public final function getCompressorExtension () {
144                 // Get compressor extension from current compressor
145                 return $this->getCompressor()->getCompressorExtension();
146         }
147 }
148
149 // [EOF]
150 ?>