Merge pull request #9242 from nupplaphil/bug/9142-message-id
[friendica.git/.git] / src / LegacyModule.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica;
23
24 /**
25  * This mock module enable class encapsulation of legacy global function modules.
26  * After having provided the module file name, all the methods will behave like a normal Module class.
27  *
28  * @author Hypolite Petovan <mrpetovan@gmail.com>
29  */
30 class LegacyModule extends BaseModule
31 {
32         /**
33          * The module name, which is the name of the file (without the .php suffix)
34          * It's used to check for existence of the module functions.
35          *
36          * @var string
37          */
38         private static $moduleName = '';
39
40         /**
41          * The only method that needs to be called, with the module/addon file name.
42          *
43          * @param string $file_path
44          * @throws \Exception
45          */
46         public static function setModuleFile($file_path)
47         {
48                 if (!is_readable($file_path)) {
49                         throw new \Exception(DI::l10n()->t('Legacy module file not found: %s', $file_path));
50                 }
51
52                 self::$moduleName = basename($file_path, '.php');
53
54                 require_once $file_path;
55         }
56
57         public static function init(array $parameters = [])
58         {
59                 self::runModuleFunction('init', $parameters);
60         }
61
62         public static function content(array $parameters = [])
63         {
64                 return self::runModuleFunction('content', $parameters);
65         }
66
67         public static function post(array $parameters = [])
68         {
69                 self::runModuleFunction('post', $parameters);
70         }
71
72         public static function afterpost(array $parameters = [])
73         {
74                 self::runModuleFunction('afterpost', $parameters);
75         }
76
77         /**
78          * Runs the module function designated by the provided suffix if it exists, the BaseModule method otherwise
79          *
80          * @param string $function_suffix
81          * @return string
82          * @throws \Exception
83          */
84         private static function runModuleFunction($function_suffix, array $parameters = [])
85         {
86                 $function_name = static::$moduleName . '_' . $function_suffix;
87
88                 if (\function_exists($function_name)) {
89                         $a = DI::app();
90                         return $function_name($a);
91                 } else {
92                         return parent::{$function_suffix}($parameters);
93                 }
94         }
95 }