]> git.mxchange.org Git - friendica.git/blob - src/LegacyModule.php
Merge pull request #7957 from annando/issue-7953
[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          * @throws \Exception
26          */
27         public static function setModuleFile($file_path)
28         {
29                 if (!is_readable($file_path)) {
30                         throw new \Exception(Core\L10n::t('Legacy module file not found: %s', $file_path));
31                 }
32
33                 self::$moduleName = basename($file_path, '.php');
34
35                 require_once $file_path;
36         }
37
38         public static function init(array $parameters = [])
39         {
40                 self::runModuleFunction('init', $parameters);
41         }
42
43         public static function content(array $parameters = [])
44         {
45                 return self::runModuleFunction('content', $parameters);
46         }
47
48         public static function post(array $parameters = [])
49         {
50                 self::runModuleFunction('post', $parameters);
51         }
52
53         public static function afterpost(array $parameters = [])
54         {
55                 self::runModuleFunction('afterpost', $parameters);
56         }
57
58         /**
59          * Runs the module function designated by the provided suffix if it exists, the BaseModule method otherwise
60          *
61          * @param string $function_suffix
62          * @return string
63          * @throws \Exception
64          */
65         private static function runModuleFunction($function_suffix, array $parameters = [])
66         {
67                 $function_name = static::$moduleName . '_' . $function_suffix;
68
69                 if (\function_exists($function_name)) {
70                         $a = self::getApp();
71                         return $function_name($a);
72                 } else {
73                         return parent::{$function_suffix}($parameters);
74                 }
75         }
76 }