X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Ffeature%2Fclass_FrameworkFeature.php;h=4f4bfa4e928576559cbd24860e2ffee75b3e1452;hp=84410bc36505fab91dcf50394fb0e412809b3a8c;hb=HEAD;hpb=a60894f1d6ef33613d2d0351075aa07aa257f304 diff --git a/framework/main/classes/feature/class_FrameworkFeature.php b/framework/main/classes/feature/class_FrameworkFeature.php index 84410bc3..3d2fbeb7 100644 --- a/framework/main/classes/feature/class_FrameworkFeature.php +++ b/framework/main/classes/feature/class_FrameworkFeature.php @@ -4,9 +4,15 @@ namespace Org\Mxchange\CoreFramework\Feature; // Import framework stuff use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; -use Org\Mxchange\CoreFramework\Factory\ObjectFactory; +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 @@ -14,7 +20,7 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -54,14 +60,14 @@ class FrameworkFeature extends BaseFrameworkSystem { * 'instance' => NULL * ) */ - private static $enabledFeatures = array(); + private static $enabledFeatures = []; /** * Protected constructor * * @return void */ - protected function __construct () { + private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } @@ -72,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__)->traceMessage(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 @@ -81,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__)->traceMessage(sprintf('FRAMEWORK-FEATURE: is_enabled[%s]=%d - EXIT!', $featureName, intval(self::$enabledFeatures[$featureName]['is_enabled']))); return self::$enabledFeatures[$featureName]['is_enabled']; } @@ -96,10 +111,15 @@ 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) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName)); + public static function isFeatureAvailable (string $featureName) { + // Check parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(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'])) { @@ -110,17 +130,15 @@ class FrameworkFeature extends BaseFrameworkSystem { // Is the feature enabled? if (!self::isFeatureEnabled($featureName)) { // Then it can't be available - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not enabled. - EXIT!', $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__, __LINE__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey)); - // Now try to get the instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FRAMEWORK-FEATURE: configKey=%s', $configKey)); try { // Try to get an instance self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey); @@ -129,14 +147,12 @@ class FrameworkFeature extends BaseFrameworkSystem { self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable(); } catch (NoClassException $e) { // Feature class not found - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName)); + self::createDebugInstance(__CLASS__, __LINE__)->warningMessage(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not available due to missing feature class. Disabling feature ...', $featureName)); } - } // END - if - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available']))); + } // Return "cached" status + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-FEATURE: featureName=%s,isAvailable=%d - EXIT!', $featureName, intval(self::$enabledFeatures[$featureName]['is_available']))); return self::$enabledFeatures[$featureName]['is_available']; } @@ -149,28 +165,41 @@ 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. - */ - assert(self::isFeatureAvailable($featureName)); + public static function callFeature (string $featureName, string $featureMethod, array $args = NULL) { + // Check parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-FEATURE: featureName=%s,featureMethod=%s,args[]=%s - CALLED!', $featureName, $featureMethod, gettype($args))); + 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(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod)); + $callable = array( + self::$enabledFeatures[$featureName]['instance'], + sprintf('featureMethod%s', StringUtils::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 + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-FEATURE: return[]=%s - EXIT!', gettype($return))); return $return; }