Debug infos with var()=%d are for length and can be decimal.
[core.git] / inc / classes / main / feature / class_FrameworkFeature.php
1 <?php
2 /**
3  * The general feature management class. No instance is needed as this class
4  * has only public methods that are static.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class FrameworkFeature extends BaseFrameworkSystem {
26         // Exception code constants
27         const EXCEPTION_FEATURE_METHOD_NOT_CALLABLE = 0x400;
28
29         /**
30          * "Cache" for enabled, available feature instances
31          *
32          * A typical available entry looks like this:
33          *
34          * array(
35          *     'is_enabled'   => TRUE,
36          *     'is_available' => TRUE,
37          *     'instance'     => SomeFeature Object
38          * )
39          *
40          * And a typical disabled entry looks like this:
41          *
42          * array(
43          *     'is_enabled'   => FALSE,
44          *     'is_available' => FALSE,
45          *     'instance'     => NULL
46          * )
47          */
48         private static $enabledFeatures = array();
49
50         /**
51          * Protected constructor
52          *
53          * @return      void
54          */
55         protected function __construct () {
56                 // Call parent constructor
57                 parent::__construct(__CLASS__);
58         }
59
60         /**
61          * Checks whether the given feature is enabled in configuration. The user
62          * shall be able to disable features, even when they *could* be available.
63          *
64          * @param       $featureName    Name of the feature to be checked
65          * @return      $isEnabled              Whether the given feature is enabled
66          */
67         public static function isFeatureEnabled ($featureName) {
68                 // Is the cache set?
69                 if (!isset(self::$enabledFeatures[$featureName]['is_enabled'])) {
70                         // Generate config key
71                         $configKey = sprintf('enable_feature_%s', $featureName);
72
73                         // Check configuration
74                         self::$enabledFeatures[$featureName]['is_enabled'] = (FrameworkConfiguration::getSelfInstance()->getConfigEntry($configKey) === 'Y');
75                 } // END - if
76
77                 // Return "cached" status
78                 return self::$enabledFeatures[$featureName]['is_enabled'];
79         }
80
81         /**
82          * Checks whether the given feature is enabled and available. It may be
83          * enabled by the user, but is not available due to e.g. a missing PECL
84          * extension or whatever is needed to have this feature available. If you
85          * don't write a pre filters for checking PHP requirements, this is the
86          * method you want to use.
87          *
88          * @param       $featureName    Name of the feature to be checked on availability
89          * @return      $isAvailable    Whether the given feature is available
90          */
91         public static function isFeatureAvailable ($featureName) {
92                 // Debug message
93                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s - CALLED!', __METHOD__, __LINE__, $featureName));
94
95                 // Is the cache set?
96                 if (!isset(self::$enabledFeatures[$featureName]['is_available'])) {
97                         // Default is not available
98                         self::$enabledFeatures[$featureName]['is_available'] = FALSE;
99                         self::$enabledFeatures[$featureName]['instance']     = NULL;
100
101                         // Is the feature enabled?
102                         if (!self::isFeatureEnabled($featureName)) {
103                                 // Then it can't be available
104                                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not enabled.', __METHOD__, __LINE__, $featureName));
105                                 return FALSE;
106                         } // END - if
107
108                         // Create config key (for feature class lookup)
109                         $configKey = sprintf('feature_%s_class', $featureName);
110
111                         // Debug message
112                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: configKey=%s', __METHOD__, __LINE__, $configKey));
113
114                         // Now try to get the instance
115                         try {
116                                 // Try to get an instance
117                                 self::$enabledFeatures[$featureName]['instance'] = ObjectFactory::createObjectByConfiguredName($configKey);
118
119                                 // Now let the feature test itself's availability
120                                 self::$enabledFeatures[$featureName]['is_available'] = self::$enabledFeatures[$featureName]['instance']->isFeatureAvailable();
121                         } catch (NoClassException $e) {
122                                 // Feature class not found
123                                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Feature "%s"is not available due to missing feature class. Disabling feature ...', __METHOD__, __LINE__, $featureName));
124                         }
125                 } // END - if
126
127                 // Debug message
128                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: featureName=%s,isAvailable=%d - EXIT!', __METHOD__, __LINE__, $featureName, intval(self::$enabledFeatures[$featureName]['is_available'])));
129
130                 // Return "cached" status
131                 return self::$enabledFeatures[$featureName]['is_available'];
132         }
133
134         /**
135          * Calls the feature's method and handles some arguments (if not given,
136          * NULL) to it. Any returned value is being forwarded to the caller, even
137          * when the doc-tag says 'void' as returned value.
138          *
139          * @param       $featureName    Name of the feature, it must be available at this point
140          * @param       $featureMethod  Method name of the feature's class
141          * @param       $args                   Any arguments that should be handled over
142          * @return      $return                 Anything the feature's method has returned
143          * @throws      FeatureMethodNotCallableException       If the requested method cannot be called
144          */
145         public static function callFeature ($featureName, $featureMethod, array $args = NULL) {
146                 /*
147                  * Please make sure that isFeatureAvailable() has been called and it has
148                  * returned TRUE before calling this method.
149                  */
150                 assert(self::isFeatureAvailable($featureName));
151
152                 // Array for call-back
153                 $callable = array(self::$enabledFeatures[$featureName]['instance'], 'featureMethod' . self::convertToClassName($featureMethod));
154
155                 // So is the feature's method callable?
156                 if (!is_callable($callable)) {
157                         // Not callable method requested
158                         throw new FeatureMethodNotCallableException(array(self::$enabledFeatures[$featureName]['instance'], $featureMethod), self::EXCEPTION_FEATURE_METHOD_NOT_CALLABLE);
159                 } // END - if
160
161                 // Then call it
162                 $return = call_user_func_array($callable, $args);
163
164                 // Return any returned value
165                 return $return;
166         }
167 }
168
169 // [EOF]
170 ?>