3 namespace CoreFramework\Feature;
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Factory\ObjectFactory;
8 use CoreFramework\Loader\NoClassException;
9 use 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 - 2017 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('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
105 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
106 // Default is not available
107 self::$enabledFeatures[$featureName]['is_available'] = false;
108 self::$enabledFeatures[$featureName]['instance'] = NULL;
110 // Is the feature enabled?
111 if (!self::isFeatureEnabled($featureName)) {
112 // Then it can't be available
113 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
117 // Create config key (for feature class lookup)
118 $configKey = sprintf('feature_%s_class', $featureName);
121 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
123 // Now try to get the instance
125 // Try to get an instance
126 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
128 // Now let the feature test itself's availability
129 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
130 } catch (NoClassException $e) {
131 // Feature class not found
132 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
137 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
139 // Return "cached" status
140 return self::$enabledFeatures[$featureName]['is_available'];
144 * Calls the feature's method and handles some arguments (if not given,
145 * NULL) to it. Any returned value is being forwarded to the caller, even
146 * when the doc-tag says 'void' as returned value.
148 * @param $featureName Name of the feature, it must be available at this point
149 * @param $featureMethod Method name of the feature's class
150 * @param $args Any arguments that should be handled over
151 * @return $return Anything the feature's method has returned
152 * @throws FeatureMethodNotCallableException If the requested method cannot be called
154 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
156 * Please make sure that isFeatureAvailable() has been called and it has
157 * returned true before calling this method.
159 assert(self::isFeatureAvailable($featureName));
161 // Array for call-back
162 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
164 // So is the feature's method callable?
165 if (!is_callable($callable)) {
166 // Not callable method requested
167 throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
171 $return = call_user_func_array($callable, $args);
173 // Return any returned value