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