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