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