]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/feature/class_FrameworkFeature.php
Continued:
[core.git] / framework / main / classes / feature / class_FrameworkFeature.php
index 5bfb48f5a6781bda8c4e540573a9ff48382bc83c..58610e4082649beacdc3cf438e33cb7e2e49665b 100644 (file)
@@ -5,17 +5,22 @@ namespace Org\Mxchange\CoreFramework\Feature;
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Loader\NoClassException;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 
+// Import SPL stuff
+use \BadMethodCallException;
+use \InvalidArgumentException;
+
 /**
  * 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 - 2021 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -73,8 +78,16 @@ class FrameworkFeature extends BaseFrameworkSystem {
         *
         * @param       $featureName    Name of the feature to be checked
         * @return      $isEnabled              Whether the given feature is enabled
+        * @throws      InvalidArgumentException        If a parameter is invalid
         */
-       public static function isFeatureEnabled ($featureName) {
+       public static function isFeatureEnabled (string $featureName) {
+               // Check parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s - CALLED!', $featureName));
+               if (empty($featureName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "featureName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Is the cache set?
                if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
                        // Generate config key
@@ -82,9 +95,10 @@ class FrameworkFeature extends BaseFrameworkSystem {
 
                        // Check configuration
                        self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y');
-               } // END - if
+               }
 
                // Return "cached" status
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: is_enabled[%s]=%d - EXIT!', $featureName, intval(self::$enabledFeatures[$featureName]['is_enabled'])));
                return self::$enabledFeatures[$featureName]['is_enabled'];
        }
 
@@ -97,10 +111,17 @@ class FrameworkFeature extends BaseFrameworkSystem {
         *
         * @param       $featureName    Name of the feature to be checked on availability
         * @return      $isAvailable    Whether the given feature is available
+        * @throws      InvalidArgumentException        If a parameter is invalid
         */
-       public static function isFeatureAvailable ($featureName) {
-               // Is the cache set?
+       public static function isFeatureAvailable (string $featureName) {
+               // Check parameter
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s - CALLED!', $featureName));
+               if (empty($featureName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "featureName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
+               // Is the cache set?
                if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
                        // Default is not available
                        self::$enabledFeatures[$featureName]['is_available'] = false;
@@ -111,7 +132,7 @@ class FrameworkFeature extends BaseFrameworkSystem {
                                // Then it can't be available
                                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not enabled.', $featureName));
                                return false;
-                       } // END - if
+                       }
 
                        // Create config key (for feature class lookup)
                        $configKey = sprintf('feature_%s_class', $featureName);
@@ -128,7 +149,7 @@ class FrameworkFeature extends BaseFrameworkSystem {
                                // Feature class not found
                                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not available due to missing feature class. Disabling feature ...', $featureName));
                        }
-               } // END - if
+               }
 
                // Return "cached" status
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s,isAvailable=%d - EXIT!', $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
@@ -144,15 +165,23 @@ class FrameworkFeature extends BaseFrameworkSystem {
         * @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      InvalidArgumentException        If a parameter has an invalid value
+        * @throws      BadMethodCallException  If this method has been invoked but the feature isn't available
         * @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.
-                */
+       public static function callFeature (string $featureName, string $featureMethod, array $args = NULL) {
+               // Check parameter
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FRAMEWORK-FEATURE: featureName=%s,featureMethod=%s,args[]=%s - CALLED!', $featureName, $featureMethod, gettype($args)));
-               assert(self::isFeatureAvailable($featureName));
+               if (empty($featureName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "featureName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif (empty($featureMethod)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "featureMethod" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif (!self::isFeatureAvailable($featureName)) {
+                       // Throw BMCE
+                       throw new BadMethodCallException(sprintf('Feature "%s" is not available but method "%s" should be invoked.', $featureName, $featureMethod), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
+               }
 
                // Array for call-back
                $callable = array(
@@ -164,7 +193,7 @@ class FrameworkFeature extends BaseFrameworkSystem {
                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);