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 implements Feature {
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 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
94 // Default is not available
95 self::$enabledFeatures[$featureName]['is_available'] = FALSE;
96 self::$enabledFeatures[$featureName]['instance'] = NULL;
98 // Is the feature enabled?
99 if (!self::isFeatureEnabled($featureName)) {
100 // Then it can't be available
104 // Create config key (for feature class lookup)
105 $configKey = sprintf('feature_%s_class', $featureName);
107 // Now try to get the instance
109 // Try to get an instance
110 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
112 // Now let the feature test itself's availability
113 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
114 } catch (NoClassException $e) {
115 // Feature class not found
116 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature %s not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
120 // Return "cached" status
121 return self::$enabledFeatures[$featureName]['is_available'];
125 * Calls the feature's method and handles some arguments (if not given,
126 * NULL) to it. Any returned value is being forwarded to the caller, even
127 * when the doc-tag says 'void' as returned value.
129 * @param $featureName Name of the feature, it must be available at this point
130 * @param $featureMethod Method name of the feature's class
131 * @param $args Any arguments that should be handled over
132 * @return $return Anything the feature's method has returned
133 * @throws FeatureMethodNotCallableException If the requested method cannot be called
135 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
137 * Please make sure that isFeatureAvailable() has been called and it has
138 * returned TRUE before calling this method.
140 assert(self::isFeatureAvailable($featureName));
142 // Array for call-back
143 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
145 // So is the feature's method callable?
146 if (!is_callable($callable)) {
147 // Not callable method requested
148 throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
152 $return = call_user_func_array($callable, $args);
154 // Return any returned value