Renamed Registry -> GenericRegistry to make it clear that this registry does
[core.git] / framework / main / classes / feature / class_FrameworkFeature.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Feature;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Loader\NoClassException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10
11 /**
12  * The general feature management class. No instance is needed as this class
13  * has only public methods that are static.
14  *
15  * @author              Roland Haeder <webmaster@ship-simu.org>
16  * @version             0.0.0
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18  * @license             GNU GPL 3.0 or any newer version
19  * @link                http://www.ship-simu.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 FrameworkFeature extends BaseFrameworkSystem {
35         // Exception code constants
36         const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
37
38         /**
39          * "Cache" for enabled, available feature instances
40          *
41          * A typical available entry looks like this:
42          *
43          * array(
44          *     'is_enabled'   => true,
45          *     'is_available' => true,
46          *     'instance'     => SomeFeature Object
47          * )
48          *
49          * And a typical disabled entry looks like this:
50          *
51          * array(
52          *     'is_enabled'   => false,
53          *     'is_available' => false,
54          *     'instance'     => NULL
55          * )
56          */
57         private static $enabledFeatures = array();
58
59         /**
60          * Protected constructor
61          *
62          * @return      void
63          */
64         protected function __construct () {
65                 // Call parent constructor
66                 parent::__construct(__CLASS__);
67         }
68
69         /**
70          * Checks whether the given feature is enabled in configuration. The user
71          * shall be able to disable features, even when they *could* be available.
72          *
73          * @param       $featureName    Name of the feature to be checked
74          * @return      $isEnabled              Whether the given feature is enabled
75          */
76         public static function isFeatureEnabled ($featureName) {
77                 // Is the cache set?
78                 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
79                         // Generate config key
80                         $configKey = sprintf('enable_feature_%s', $featureName);
81
82                         // Check configuration
83                         self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y');
84                 } // END - if
85
86                 // Return "cached" status
87                 return self::$enabledFeatures[$featureName]['is_enabled'];
88         }
89
90         /**
91          * Checks whether the given feature is enabled and available. It may be
92          * enabled by the user, but is not available due to e.g. a missing PECL
93          * extension or whatever is needed to have this feature available. If you
94          * don't write a pre filters for checking PHP requirements, this is the
95          * method you want to use.
96          *
97          * @param       $featureName    Name of the feature to be checked on availability
98          * @return      $isAvailable    Whether the given feature is available
99          */
100         public static function isFeatureAvailable ($featureName) {
101                 // Debug message
102                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
103
104                 // Is the cache set?
105                 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
106                         // Default is not available
107                         self::$enabledFeatures[$featureName]['is_available'] = false;
108                         self::$enabledFeatures[$featureName]['instance']     = NULL;
109
110                         // Is the feature enabled?
111                         if (!self::isFeatureEnabled($featureName)) {
112                                 // Then it can't be available
113                                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
114                                 return false;
115                         } // END - if
116
117                         // Create config key (for feature class lookup)
118                         $configKey = sprintf('feature_%s_class', $featureName);
119
120                         // Debug message
121                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
122
123                         // Now try to get the instance
124                         try {
125                                 // Try to get an instance
126                                 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
127
128                                 // Now let the feature test itself's availability
129                                 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
130                         } catch (NoClassException $e) {
131                                 // Feature class not found
132                                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
133                         }
134                 } // END - if
135
136                 // Debug message
137                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
138
139                 // Return "cached" status
140                 return self::$enabledFeatures[$featureName]['is_available'];
141         }
142
143         /**
144          * Calls the feature's method and handles some arguments (if not given,
145          * NULL) to it. Any returned value is being forwarded to the caller, even
146          * when the doc-tag says 'void' as returned value.
147          *
148          * @param       $featureName    Name of the feature, it must be available at this point
149          * @param       $featureMethod  Method name of the feature's class
150          * @param       $args                   Any arguments that should be handled over
151          * @return      $return                 Anything the feature's method has returned
152          * @throws      FeatureMethodNotCallableException       If the requested method cannot be called
153          */
154         public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
155                 /*
156                  * Please make sure that isFeatureAvailable() has been called and it has
157                  * returned true before calling this method.
158                  */
159                 assert(self::isFeatureAvailable($featureName));
160
161                 // Array for call-back
162                 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
163
164                 // So is the feature's method callable?
165                 if (!is_callable($callable)) {
166                         // Not callable method requested
167                         throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
168                 } // END - if
169
170                 // Then call it
171                 $return = call_user_func_array($callable, $args);
172
173                 // Return any returned value
174                 return $return;
175         }
176
177 }