3 namespace Org\Mxchange\CoreFramework\Feature;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Loader\NoClassException;
9 use Org\Mxchange\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 <<<<<<< HEAD:framework/main/classes/feature/class_FrameworkFeature.php
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
21 >>>>>>> Some updates::inc/main/classes/feature/class_FrameworkFeature.php
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.ship-simu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class FrameworkFeature extends BaseFrameworkSystem {
39 // Exception code constants
40 const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
43 * "Cache" for enabled, available feature instances
45 * A typical available entry looks like this:
48 * 'is_enabled' => true,
49 * 'is_available' => true,
50 * 'instance' => SomeFeature Object
53 * And a typical disabled entry looks like this:
56 * 'is_enabled' => false,
57 * 'is_available' => false,
61 private static $enabledFeatures = array();
64 * Protected constructor
68 protected function __construct () {
69 // Call parent constructor
70 parent::__construct(__CLASS__);
74 * Checks whether the given feature is enabled in configuration. The user
75 * shall be able to disable features, even when they *could* be available.
77 * @param $featureName Name of the feature to be checked
78 * @return $isEnabled Whether the given feature is enabled
80 public static function isFeatureEnabled ($featureName) {
82 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
83 // Generate config key
84 $configKey = sprintf('enable_feature_%s', $featureName);
86 // Check configuration
87 self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y');
90 // Return "cached" status
91 return self::$enabledFeatures[$featureName]['is_enabled'];
95 * Checks whether the given feature is enabled and available. It may be
96 * enabled by the user, but is not available due to e.g. a missing PECL
97 * extension or whatever is needed to have this feature available. If you
98 * don't write a pre filters for checking PHP requirements, this is the
99 * method you want to use.
101 * @param $featureName Name of the feature to be checked on availability
102 * @return $isAvailable Whether the given feature is available
104 public static function isFeatureAvailable ($featureName) {
106 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
109 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
110 // Default is not available
111 self::$enabledFeatures[$featureName]['is_available'] = false;
112 self::$enabledFeatures[$featureName]['instance'] = NULL;
114 // Is the feature enabled?
115 if (!self::isFeatureEnabled($featureName)) {
116 // Then it can't be available
117 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
121 // Create config key (for feature class lookup)
122 $configKey = sprintf('feature_%s_class', $featureName);
125 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
127 // Now try to get the instance
129 // Try to get an instance
130 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
132 // Now let the feature test itself's availability
133 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
134 } catch (NoClassException $e) {
135 // Feature class not found
136 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
141 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
143 // Return "cached" status
144 return self::$enabledFeatures[$featureName]['is_available'];
148 * Calls the feature's method and handles some arguments (if not given,
149 * NULL) to it. Any returned value is being forwarded to the caller, even
150 * when the doc-tag says 'void' as returned value.
152 * @param $featureName Name of the feature, it must be available at this point
153 * @param $featureMethod Method name of the feature's class
154 * @param $args Any arguments that should be handled over
155 * @return $return Anything the feature's method has returned
156 * @throws FeatureMethodNotCallableException If the requested method cannot be called
158 public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
160 * Please make sure that isFeatureAvailable() has been called and it has
161 * returned true before calling this method.
163 assert(self::isFeatureAvailable($featureName));
165 // Array for call-back
166 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
168 // So is the feature's method callable?
169 if (!is_callable($callable)) {
170 // Not callable method requested
171 throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
175 $return = call_user_func_array($callable, $args);
177 // Return any returned value