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