]> git.mxchange.org Git - core.git/blob - framework/main/classes/feature/class_FrameworkFeature.php
6310f98e6f0add8a28e5ac2f9132744abfa7c3fe
[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 - 2020 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                 // Is the cache set?
102                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s - CALLED!', $featureName));
103                 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
104                         // Default is not available
105                         self::$enabledFeatures[$featureName]['is_available'] = false;
106                         self::$enabledFeatures[$featureName]['instance']     = NULL;
107
108                         // Is the feature enabled?
109                         if (!self::isFeatureEnabled($featureName)) {
110                                 // Then it can't be available
111                                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not enabled.', $featureName));
112                                 return false;
113                         } // END - if
114
115                         // Create config key (for feature class lookup)
116                         $configKey = sprintf('feature_%s_class', $featureName);
117
118                         // Now try to get the instance
119                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: configKey=%s', $configKey));
120                         try {
121                                 // Try to get an instance
122                                 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
123
124                                 // Now let the feature test itself's availability
125                                 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
126                         } catch (NoClassException $e) {
127                                 // Feature class not found
128                                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not available due to missing feature class. Disabling feature ...', $featureName));
129                         }
130                 } // END - if
131
132                 // Return "cached" status
133                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s,isAvailable=%d - EXIT!', $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
134                 return self::$enabledFeatures[$featureName]['is_available'];
135         }
136
137         /**
138          * Calls the feature's method and handles some arguments (if not given,
139          * NULL) to it. Any returned value is being forwarded to the caller, even
140          * when the doc-tag says 'void' as returned value.
141          *
142          * @param       $featureName    Name of the feature, it must be available at this point
143          * @param       $featureMethod  Method name of the feature's class
144          * @param       $args                   Any arguments that should be handled over
145          * @return      $return                 Anything the feature's method has returned
146          * @throws      FeatureMethodNotCallableException       If the requested method cannot be called
147          */
148         public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
149                 /*
150                  * Please make sure that isFeatureAvailable() has been called and it has
151                  * returned true before calling this method.
152                  */
153                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s,featureMethod=%s,args[]=%s - CALLED!', $featureName, $featureMethod, gettype($args)));
154                 assert(self::isFeatureAvailable($featureName));
155
156                 // Array for call-back
157                 $callable = array(
158                         self::$enabledFeatures[$featureName]['instance'],
159                         sprintf('featureMethod%s', self::convertToClassName($featureMethod))
160                 );
161
162                 // So is the feature's method callable?
163                 if (!is_callable($callable)) {
164                         // Not callable method requested
165                         throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
166                 } // END - if
167
168                 // Then call it
169                 $return = call_user_func_array($callable, $args);
170
171                 // Return any returned value
172                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: return[]=%s - EXIT!', gettype($return)));
173                 return $return;
174         }
175
176 }