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