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