3 * The general feature management class. No instance is needed as this class
4 * has only public methods that are static.
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class FrameworkFeature extends BaseFrameworkSystem {
26 // Exception code constants
27 const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
30 * "Cache" for enabled, available feature instances
32 * A typical available entry looks like this:
35 * 'is_enabled' => TRUE,
36 * 'is_available' => TRUE,
37 * 'instance' => SomeFeature Object
40 * And a typical disabled entry looks like this:
43 * 'is_enabled' => FALSE,
44 * 'is_available' => FALSE,
48 private static $enabledFeatures = array();
51 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
61 * Checks whether the given feature is enabled in configuration. The user
62 * shall be able to disable features, even when they *could* be available.
64 * @param $featureName Name of the feature to be checked
65 * @return $isEnabled Whether the given feature is enabled
67 public static function isFeatureEnabled ($featureName) {
69 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
70 // Generate config key
71 $configKey = sprintf('enable_feature_%s', $featureName);
73 // Check configuration
74 self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkConfiguration::getSelfInstance()->getConfigEntry($configKey) === 'Y');
77 // Return "cached" status
78 return self::$enabledFeatures[$featureName]['is_enabled'];
82 * Checks whether the given feature is enabled and available. It may be
83 * enabled by the user, but is not available due to e.g. a missing PECL
84 * extension or whatever is needed to have this feature available. If you
85 * don't write a pre filters for checking PHP requirements, this is the
86 * method you want to use.
88 * @param $featureName Name of the feature to be checked on availability
89 * @return $isAvailable Whether the given feature is available
91 public static function isFeatureAvailable ($featureName) {
93 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
96 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
97 // Default is not available
98 self::$enabledFeatures[$featureName]['is_available'] = FALSE;
99 self::$enabledFeatures[$featureName]['instance'] = NULL;
101 // Is the feature enabled?
102 if (!self::isFeatureEnabled($featureName)) {
103 // Then it can't be available
104 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
108 // Create config key (for feature class lookup)
109 $configKey = sprintf('feature_%s_class', $featureName);
112 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
114 // Now try to get the instance
116 // Try to get an instance
117 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
119 // Now let the feature test itself's availability
120 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
121 } catch (NoClassException $e) {
122 // Feature class not found
123 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
130 // Return "cached" status
131 return self::$enabledFeatures[$featureName]['is_available'];
135 * Calls the feature's method and handles some arguments (if not given,
136 * NULL) to it. Any returned value is being forwarded to the caller, even
137 * when the doc-tag says 'void' as returned value.
139 * @param $featureName Name of the feature, it must be available at this point
140 * @param $featureMethod Method name of the feature's class
141 * @param $args Any arguments that should be handled over
142 * @return $return Anything the feature's method has returned
143 * @throws FeatureMethodNotCallableException If the requested method cannot be called
145 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
147 * Please make sure that isFeatureAvailable() has been called and it has
148 * returned TRUE before calling this method.
150 assert(self::isFeatureAvailable($featureName));
152 // Array for call-back
153 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
155 // So is the feature's method callable?
156 if (!is_callable($callable)) {
157 // Not callable method requested
158 throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
162 $return = call_user_func_array($callable, $args);
164 // Return any returned value