3 namespace Org\Mxchange\CoreFramework\Feature;
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;
12 * The general feature management class. No instance is needed as this class
13 * has only public methods that are static.
15 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
34 class FrameworkFeature extends BaseFrameworkSystem {
35 // Exception code constants
36 const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
39 * "Cache" for enabled, available feature instances
41 * A typical available entry looks like this:
44 * 'is_enabled' => true,
45 * 'is_available' => true,
46 * 'instance' => SomeFeature Object
49 * And a typical disabled entry looks like this:
52 * 'is_enabled' => false,
53 * 'is_available' => false,
57 private static $enabledFeatures = array();
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
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.
73 * @param $featureName Name of the feature to be checked
74 * @return $isEnabled Whether the given feature is enabled
76 public static function isFeatureEnabled ($featureName) {
78 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
79 // Generate config key
80 $configKey = sprintf('enable_feature_%s', $featureName);
82 // Check configuration
83 self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y');
86 // Return "cached" status
87 return self::$enabledFeatures[$featureName]['is_enabled'];
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.
97 * @param $featureName Name of the feature to be checked on availability
98 * @return $isAvailable Whether the given feature is available
100 public static function isFeatureAvailable ($featureName) {
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;
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));
115 // Create config key (for feature class lookup)
116 $configKey = sprintf('feature_%s_class', $featureName);
118 // Now try to get the instance
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: configKey=%s', $configKey));
121 // Try to get an instance
122 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
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));
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'];
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.
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
148 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
150 * Please make sure that isFeatureAvailable() has been called and it has
151 * returned true before calling this method.
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));
156 // Array for call-back
158 self::$enabledFeatures[$featureName]['instance'],
159 sprintf('featureMethod%s', self::convertToClassName($featureMethod))
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);
169 $return = call_user_func_array($callable, $args);
171 // Return any returned value
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: return[]=%s - EXIT!', gettype($return)));