]> git.mxchange.org Git - friendica.git/blob - src/LegacyModule.php
Add 'addon' folder as 'Friendica\Addon' namespace for autoload
[friendica.git] / src / LegacyModule.php
1 <?php
2
3 namespace Friendica;
4
5 /**
6  * This mock module enable class encapsulation of legacy global function modules.
7  * After having provided the module file name, all the methods will behave like a normal Module class.
8  *
9  * @author Hypolite Petovan <mrpetovan@gmail.com>
10  */
11 class LegacyModule extends BaseModule
12 {
13         /**
14          * The module name, which is the name of the file (without the .php suffix)
15          * It's used to check for existence of the module functions.
16          *
17          * @var string
18          */
19         private static $moduleName = '';
20
21         /**
22          * The only method that needs to be called, with the module/addon file name.
23          *
24          * @param string $file_path
25          */
26         public static function setModuleFile($file_path)
27         {
28                 if (!is_readable($file_path)) {
29                         throw new Exception(Core\L10n::t('Legacy module file not found: %s', $file_path));
30                 }
31
32                 self::$moduleName = basename($file_path, '.php');
33
34                 require_once $file_path;
35         }
36
37         public static function init()
38         {
39                 self::runModuleFunction('init');
40         }
41
42         public static function content()
43         {
44                 return self::runModuleFunction('content');
45         }
46
47         public static function post()
48         {
49                 self::runModuleFunction('post');
50         }
51
52         public static function afterpost()
53         {
54                 self::runModuleFunction('afterpost');
55         }
56
57         /**
58          * Runs the module function designated by the provided suffix if it exists, the BaseModule method otherwise
59          *
60          * @param string $function_suffix
61          * @return string
62          */
63         private static function runModuleFunction($function_suffix)
64         {
65                 $function_name = static::$moduleName . '_' . $function_suffix;
66
67                 if (\function_exists($function_name)) {
68                         $a = self::getApp();
69                         return $function_name($a);
70                 } else {
71                         return parent::{$function_suffix}();
72                 }
73         }
74 }