3 namespace Org\Mxchange\CoreFramework\Feature;
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;
14 use \InvalidArgumentException;
17 * The general feature management class. No instance is needed as this class
18 * has only public methods that are static.
20 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
39 class FrameworkFeature extends BaseFrameworkSystem {
40 // Exception code constants
41 const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
44 * "Cache" for enabled, available feature instances
46 * A typical available entry looks like this:
49 * 'is_enabled' => true,
50 * 'is_available' => true,
51 * 'instance' => SomeFeature Object
54 * And a typical disabled entry looks like this:
57 * 'is_enabled' => false,
58 * 'is_available' => false,
62 private static $enabledFeatures = [];
65 * Protected constructor
69 private function __construct () {
70 // Call parent constructor
71 parent::__construct(__CLASS__);
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.
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
82 public static function isFeatureEnabled (string $featureName) {
84 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s - CALLED!', $featureName));
85 if (empty($featureName)) {
87 throw new InvalidArgumentException('Parameter "featureName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
91 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
92 // Generate config key
93 $configKey = sprintf('enable_feature_%s', $featureName);
95 // Check configuration
96 self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y');
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'];
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.
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
115 public static function isFeatureAvailable (string $featureName) {
117 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s - CALLED!', $featureName));
118 if (empty($featureName)) {
120 throw new InvalidArgumentException('Parameter "featureName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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;
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));
136 // Create config key (for feature class lookup)
137 $configKey = sprintf('feature_%s_class', $featureName);
139 // Now try to get the instance
140 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: configKey=%s', $configKey));
142 // Try to get an instance
143 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
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));
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'];
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.
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
169 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
171 * Please make sure that isFeatureAvailable() has been called and it has
172 * returned true before calling this method.
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));
177 // Array for call-back
179 self::$enabledFeatures[$featureName]['instance'],
180 sprintf('featureMethod%s', StringUtils::convertToClassName($featureMethod))
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);
190 $return = call_user_func_array($callable, $args);
192 // Return any returned value
193 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: return[]=%s - EXIT!', gettype($return)));