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;
10 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
13 * The general feature management class. No instance is needed as this class
14 * has only public methods that are static.
16 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
35 class FrameworkFeature extends BaseFrameworkSystem {
36 // Exception code constants
37 const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
40 * "Cache" for enabled, available feature instances
42 * A typical available entry looks like this:
45 * 'is_enabled' => true,
46 * 'is_available' => true,
47 * 'instance' => SomeFeature Object
50 * And a typical disabled entry looks like this:
53 * 'is_enabled' => false,
54 * 'is_available' => false,
58 private static $enabledFeatures = array();
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
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.
74 * @param $featureName Name of the feature to be checked
75 * @return $isEnabled Whether the given feature is enabled
77 public static function isFeatureEnabled ($featureName) {
79 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
80 // Generate config key
81 $configKey = sprintf('enable_feature_%s', $featureName);
83 // Check configuration
84 self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y');
87 // Return "cached" status
88 return self::$enabledFeatures[$featureName]['is_enabled'];
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.
98 * @param $featureName Name of the feature to be checked on availability
99 * @return $isAvailable Whether the given feature is available
101 public static function isFeatureAvailable ($featureName) {
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;
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));
116 // Create config key (for feature class lookup)
117 $configKey = sprintf('feature_%s_class', $featureName);
119 // Now try to get the instance
120 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: configKey=%s', $configKey));
122 // Try to get an instance
123 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
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));
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'];
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.
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
149 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
151 * Please make sure that isFeatureAvailable() has been called and it has
152 * returned true before calling this method.
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));
157 // Array for call-back
159 self::$enabledFeatures[$featureName]['instance'],
160 sprintf('featureMethod%s', StringUtils::convertToClassName($featureMethod))
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);
170 $return = call_user_func_array($callable, $args);
172 // Return any returned value
173 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: return[]=%s - EXIT!', gettype($return)));