]> git.mxchange.org Git - core.git/blobdiff - inc/main/classes/feature/class_FrameworkFeature.php
Renamed classes/main/ to main/classes/ + added FuseFeature, an upcoming feature
[core.git] / inc / main / classes / feature / class_FrameworkFeature.php
diff --git a/inc/main/classes/feature/class_FrameworkFeature.php b/inc/main/classes/feature/class_FrameworkFeature.php
new file mode 100644 (file)
index 0000000..4c16afe
--- /dev/null
@@ -0,0 +1,170 @@
+<?php
+/**
+ * The general feature management class. No instance is needed as this class
+ * has only public methods that are static.
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class FrameworkFeature extends BaseFrameworkSystem {
+       // Exception code constants
+       const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
+
+       /**
+        * "Cache" for enabled, available feature instances
+        *
+        * A typical available entry looks like this:
+        *
+        * array(
+        *     'is_enabled'   => TRUE,
+        *     'is_available' => TRUE,
+        *     'instance'     => SomeFeature Object
+        * )
+        *
+        * And a typical disabled entry looks like this:
+        *
+        * array(
+        *     'is_enabled'   => FALSE,
+        *     'is_available' => FALSE,
+        *     'instance'     => NULL
+        * )
+        */
+       private static $enabledFeatures = array();
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Checks whether the given feature is enabled in configuration. The user
+        * shall be able to disable features, even when they *could* be available.
+        *
+        * @param       $featureName    Name of the feature to be checked
+        * @return      $isEnabled              Whether the given feature is enabled
+        */
+       public static function isFeatureEnabled ($featureName) {
+               // Is the cache set?
+               if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
+                       // Generate config key
+                       $configKey = sprintf('enable_feature_%s', $featureName);
+
+                       // Check configuration
+                       self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkConfiguration::getSelfInstance()->getConfigEntry($configKey) === 'Y');
+               } // END - if
+
+               // Return "cached" status
+               return self::$enabledFeatures[$featureName]['is_enabled'];
+       }
+
+       /**
+        * Checks whether the given feature is enabled and available. It may be
+        * enabled by the user, but is not available due to e.g. a missing PECL
+        * extension or whatever is needed to have this feature available. If you
+        * don't write a pre filters for checking PHP requirements, this is the
+        * method you want to use.
+        *
+        * @param       $featureName    Name of the feature to be checked on availability
+        * @return      $isAvailable    Whether the given feature is available
+        */
+       public static function isFeatureAvailable ($featureName) {
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
+
+               // Is the cache set?
+               if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
+                       // Default is not available
+                       self::$enabledFeatures[$featureName]['is_available'] = FALSE;
+                       self::$enabledFeatures[$featureName]['instance']     = NULL;
+
+                       // Is the feature enabled?
+                       if (!self::isFeatureEnabled($featureName)) {
+                               // Then it can't be available
+                               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
+                               return FALSE;
+                       } // END - if
+
+                       // Create config key (for feature class lookup)
+                       $configKey = sprintf('feature_%s_class', $featureName);
+
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
+
+                       // Now try to get the instance
+                       try {
+                               // Try to get an instance
+                               self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
+
+                               // Now let the feature test itself's availability
+                               self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
+                       } catch (NoClassException $e) {
+                               // Feature class not found
+                               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
+                       }
+               } // END - if
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
+
+               // Return "cached" status
+               return self::$enabledFeatures[$featureName]['is_available'];
+       }
+
+       /**
+        * Calls the feature's method and handles some arguments (if not given,
+        * NULL) to it. Any returned value is being forwarded to the caller, even
+        * when the doc-tag says 'void' as returned value.
+        *
+        * @param       $featureName    Name of the feature, it must be available at this point
+        * @param       $featureMethod  Method name of the feature's class
+        * @param       $args                   Any arguments that should be handled over
+        * @return      $return                 Anything the feature's method has returned
+        * @throws      FeatureMethodNotCallableException       If the requested method cannot be called
+        */
+       public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
+               /*
+                * Please make sure that isFeatureAvailable() has been called and it has
+                * returned TRUE before calling this method.
+                */
+               assert(self::isFeatureAvailable($featureName));
+
+               // Array for call-back
+               $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
+
+               // So is the feature's method callable?
+               if (!is_callable($callable)) {
+                       // Not callable method requested
+                       throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
+               } // END - if
+
+               // Then call it
+               $return = call_user_func_array($callable, $args);
+
+               // Return any returned value
+               return $return;
+       }
+}
+
+// [EOF]
+?>