* @version 0.0.0 * @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 * * 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 . */ 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 = []; /** * Protected constructor * * @return void */ private 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 * @throws InvalidArgumentException If a parameter is invalid */ 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 $configKey = sprintf('enable_feature_%s', $featureName); // Check configuration self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey) === 'Y'); } // 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']; } /** * 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 * @throws InvalidArgumentException If a parameter is invalid */ 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'])) { // 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 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not enabled. - EXIT!', $featureName)); return false; } // Create config key (for feature class lookup) $configKey = sprintf('feature_%s_class', $featureName); // 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); // 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__, __LINE__)->warningMessage(sprintf('FRAMEWORK-FEATURE: Feature "%s"is not available due to missing feature class. Disabling feature ...', $featureName)); } } // 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']; } /** * 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 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 (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'], 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); } // 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; } }