3 namespace CoreFramework\Feature;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Object\BaseFrameworkSystem;
10 * The general feature management class. No instance is needed as this class
11 * has only public methods that are static.
13 * @author Roland Haeder <webmaster@ship-simu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.ship-simu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class FrameworkFeature extends BaseFrameworkSystem {
33 // Exception code constants
34 const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
37 * "Cache" for enabled, available feature instances
39 * A typical available entry looks like this:
42 * 'is_enabled' => TRUE,
43 * 'is_available' => TRUE,
44 * 'instance' => SomeFeature Object
47 * And a typical disabled entry looks like this:
50 * 'is_enabled' => FALSE,
51 * 'is_available' => FALSE,
55 private static $enabledFeatures = array();
58 * Protected constructor
62 protected function __construct () {
63 // Call parent constructor
64 parent::__construct(__CLASS__);
68 * Checks whether the given feature is enabled in configuration. The user
69 * shall be able to disable features, even when they *could* be available.
71 * @param $featureName Name of the feature to be checked
72 * @return $isEnabled Whether the given feature is enabled
74 public static function isFeatureEnabled ($featureName) {
76 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
77 // Generate config key
78 $configKey = sprintf('enable_feature_%s', $featureName);
80 // Check configuration
81 self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkConfiguration::getSelfInstance()->getConfigEntry($configKey) === 'Y');
84 // Return "cached" status
85 return self::$enabledFeatures[$featureName]['is_enabled'];
89 * Checks whether the given feature is enabled and available. It may be
90 * enabled by the user, but is not available due to e.g. a missing PECL
91 * extension or whatever is needed to have this feature available. If you
92 * don't write a pre filters for checking PHP requirements, this is the
93 * method you want to use.
95 * @param $featureName Name of the feature to be checked on availability
96 * @return $isAvailable Whether the given feature is available
98 public static function isFeatureAvailable ($featureName) {
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
103 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
104 // Default is not available
105 self::$enabledFeatures[$featureName]['is_available'] = FALSE;
106 self::$enabledFeatures[$featureName]['instance'] = NULL;
108 // Is the feature enabled?
109 if (!self::isFeatureEnabled($featureName)) {
110 // Then it can't be available
111 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
115 // Create config key (for feature class lookup)
116 $configKey = sprintf('feature_%s_class', $featureName);
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
121 // Now try to get the instance
123 // Try to get an instance
124 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
126 // Now let the feature test itself's availability
127 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
128 } catch (NoClassException $e) {
129 // Feature class not found
130 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
135 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
137 // Return "cached" status
138 return self::$enabledFeatures[$featureName]['is_available'];
142 * Calls the feature's method and handles some arguments (if not given,
143 * NULL) to it. Any returned value is being forwarded to the caller, even
144 * when the doc-tag says 'void' as returned value.
146 * @param $featureName Name of the feature, it must be available at this point
147 * @param $featureMethod Method name of the feature's class
148 * @param $args Any arguments that should be handled over
149 * @return $return Anything the feature's method has returned
150 * @throws FeatureMethodNotCallableException If the requested method cannot be called
152 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
154 * Please make sure that isFeatureAvailable() has been called and it has
155 * returned TRUE before calling this method.
157 assert(self::isFeatureAvailable($featureName));
159 // Array for call-back
160 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
162 // So is the feature's method callable?
163 if (!is_callable($callable)) {
164 // Not callable method requested
165 throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
169 $return = call_user_func_array($callable, $args);
171 // Return any returned value