Initial import of current development status
[shipsimu.git] / ship-simu / inc / classes / middleware / compressor / class_CompressorChannel.php
1 <?php
2 // Kompressor-Auswahl Klasse
3 class CompressorChannel extends BaseMiddleware {
4         // Output handler instance
5         private $compressor = null;
6
7         // Public constructor
8         private function __construct () {
9                 // Call parent constructor!
10                 parent::constructor(__CLASS__);
11
12                 // Set description
13                 $this->setPartDescr("Komprimierungshandler");
14
15                 // Create an unique ID
16                 $this->createUniqueID();
17         }
18
19         // Create a new compressor channel based a given compression handler
20         public final static function createCompressorChannel ($baseDir) {
21                 // Get new instance
22                 $cInstance = new CompressorChannel();
23
24                 // Is the compressor handler set?
25                 if (
26                            (is_null($cInstance->getCompressor()))
27                         || (!is_object($cInstance->getCompressor()))
28                         || (!method_exists($cInstance->getCompressor(), 'compressStream'))
29                         || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
30                 ) {
31                         // Get a directory pointer
32                         $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
33
34                         // Read all directories but no sub directories
35                         while ($dir = $dirPointer->readDirectoryExcept(array("..", ".", ".htaccess"))) {
36                                 // Is this a class file?
37                                 if ((substr($dir, 0, 6) == "class_") && (substr($dir, -4, 4) == $cInstance->getConfigInstance()->readConfig("php_extension"))) {
38                                         // Get the compressor's name. That's why you must name
39                                         // your files like your classes and also that's why you
40                                         // must keep on class in one file.
41                                         $class = substr($dir, 6, -4);
42
43                                         // Create eval command
44                                         $eval = sprintf("\$tempInstance = %s::create%s();",
45                                                 $class,
46                                                 $class
47                                         );
48
49                                         // Debug message
50                                         if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $cInstance->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
51                                                 $cInstance->__toString(),
52                                                 htmlentities($eval)
53                                         ));
54
55                                         // Run it. This will create an instance to the current class
56                                         eval($eval);
57
58                                         // Is the instance valid? We have the stream handler here
59                                         if ((!is_null($tempInstance)) && (method_exists($tempInstance, 'compressStream')) && (method_exists($tempInstance, 'decompressStream'))) {
60                                                 // Okay, this handler is valid
61                                                 $cInstance->setCompressor($tempInstance);
62
63                                                 // No more searches required because we have found a valid compressor stream
64                                                 break;
65                                         }
66                                 }
67                         }
68
69                         // Close the directory
70                         $dirPointer->closeDirectory();
71                 }
72
73                 // Check again if there is a compressor
74                 if (
75                            (is_null($cInstance->getCompressor()))
76                         || (!is_object($cInstance->getCompressor()))
77                         || (!method_exists($cInstance->getCompressor(), 'compressStream'))
78                         || (!method_exists($cInstance->getCompressor(), 'decompressStream'))
79                 ) {
80                         // Set the null compressor handler
81                         $cInstance->setCompressor(NullCompressor::createNullCompressor());
82                 }
83         
84                 // Return the compressor instance
85                 return $cInstance;
86         }
87
88         /**
89          * Getter for compressor instance
90          *
91          * @return      $compressor     The compressor instance
92          */
93         public final function getCompressor () {
94                 return $this->compressor;
95         }
96
97         /**
98          * Setter for compressor
99          *
100          * @param               $compressorInstance     The compressor instance we shall use
101          * @return      void
102          */
103         public final function setCompressor (Compressor $compressorInstance) {
104                 $this->compressor = $compressorInstance;
105         }
106
107         /**
108          * Getter for the file extension of the current compressor
109          */
110         public final function getCompressorExtension () {
111                 // Get compressor extension from current compressor
112                 return $this->getCompressor()->getCompressorExtension();
113         }
114 }
115
116 // [EOF]
117 ?>