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