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