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