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