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