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