fe77ed389591f3677868ffbfe2ae21137f62fe1f
[core.git] / inc / main / middleware / compressor / class_CompressorChannel.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Channel\Compressor;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Middleware\BaseMiddleware;
8 use CoreFramework\Registry\Registerable;
9
10 /**
11  * Middleware class for selecting the right compressor channel
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class CompressorChannel extends BaseMiddleware implements Registerable {
33         /**
34          * Real compressor instance
35          */
36         private $compressor = NULL;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor!
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Create a new compressor channel.
50          *
51          * @return      $compressorInstance             A prepared instance of this class
52          */
53         public static final function createCompressorChannel () {
54                 // Get new instance
55                 $compressorInstance = new CompressorChannel();
56
57                 // Is the compressor handler set?
58                 if (
59                            (is_null($compressorInstance->getCompressor()))
60                         || (!$compressorInstance->getCompressor() instanceof Compressor)
61                 ) {
62                         // Init base directory
63                         $baseDir =
64                                 $compressorInstance->getConfigInstance()->getConfigEntry('base_path') .
65                                 $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
66
67                         // Get a directory pointer
68                         $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
69
70                         // Read all directories but no sub directories, .htaccess files and NullCompressor class
71                         while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
72                                 // Debug message
73                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COMPRESSOR[' . __METHOD__ . ':' . __LINE__ . ']: directoryEntry=' . $directoryEntry);
74
75                                 // Is this a class file?
76                                 if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
77                                         /* Get the compressor's name. That's why you must name
78                                          * your files like your classes and also that's why you
79                                          * must keep on class in one file.
80                                          */
81                                         $className = substr($directoryEntry, 6, -4);
82
83                                         // Get an instance from our object factory
84                                         $tempInstance = ObjectFactory::createObjectByName($className);
85
86                                         // Is it null?
87                                         if (is_null($tempInstance)) {
88                                                 // Then skip to the next one
89                                                 continue;
90                                         } // END - if
91
92                                         // Set the compressor
93                                         $compressorInstance->setCompressor($tempInstance);
94
95                                         // No more searches required because we have found a valid compressor stream
96                                         break;
97                                 } // END - if
98                         } // END - while
99
100                         // Close the directory
101                         $directoryInstance->closeDirectory();
102                 } // END - if
103
104                 // Check again if there is a compressor
105                 if (
106                            (is_null($compressorInstance->getCompressor()))
107                         || (!is_object($compressorInstance->getCompressor()))
108                         || (!$compressorInstance instanceof Compressor)
109                 ) {
110                         // Set the null compressor handler. This should not be configureable!
111                         // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay?
112                         $compressorInstance->setCompressor(ObjectFactory::createObjectByName('NullCompressor'));
113                 } // END - if
114
115                 // Return the compressor instance
116                 return $compressorInstance;
117         }
118
119         /**
120          * Getter for compressor instance
121          *
122          * @return      $compressor     The compressor instance
123          */
124         public final function getCompressor () {
125                 return $this->compressor;
126         }
127
128         /**
129          * Setter for compressor
130          *
131          * @param               $compressorInstance     The compressor instance we shall use
132          * @return      void
133          */
134         public final function setCompressor (Compressor $compressorInstance = NULL) {
135                 $this->compressor = $compressorInstance;
136         }
137
138         /**
139          * Getter for the file extension of the current compressor
140          */
141         public final function getCompressorExtension () {
142                 // Get compressor extension from current compressor
143                 return $this->getCompressor()->getCompressorExtension();
144         }
145
146 }