]> git.mxchange.org Git - friendica.git/blob - src/LegacyModule.php
Merge branch 'friendica:develop' into doc_faq_client-clean-up
[friendica.git] / src / LegacyModule.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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 use Friendica\Core\L10n;
25
26 /**
27  * This mock module enable class encapsulation of legacy global function modules.
28  * After having provided the module file name, all the methods will behave like a normal Module class.
29  *
30  * @author Hypolite Petovan <mrpetovan@gmail.com>
31  */
32 class LegacyModule extends BaseModule
33 {
34         /**
35          * The module name, which is the name of the file (without the .php suffix)
36          * It's used to check for existence of the module functions.
37          *
38          * @var string
39          */
40         private $moduleName = '';
41
42         public function __construct(L10n $l10n, string $file_path = '', array $parameters = [])
43         {
44                 parent::__construct($l10n, $parameters);
45
46                 $this->setModuleFile($file_path);
47
48                 $this->runModuleFunction('init');
49         }
50
51         /**
52          * The only method that needs to be called, with the module/addon file name.
53          *
54          * @param string $file_path
55          * @throws \Exception
56          */
57         private function setModuleFile($file_path)
58         {
59                 if (!is_readable($file_path)) {
60                         throw new \Exception(DI::l10n()->t('Legacy module file not found: %s', $file_path));
61                 }
62
63                 $this->moduleName = basename($file_path, '.php');
64
65                 require_once $file_path;
66         }
67
68         public function content(): string
69         {
70                 return $this->runModuleFunction('content');
71         }
72
73         public function post()
74         {
75                 $this->runModuleFunction('post');
76         }
77
78         /**
79          * Runs the module function designated by the provided suffix if it exists, the BaseModule method otherwise
80          *
81          * @param string $function_suffix
82          * @return string
83          * @throws \Exception
84          */
85         private function runModuleFunction(string $function_suffix)
86         {
87                 $function_name = $this->moduleName . '_' . $function_suffix;
88
89                 if (\function_exists($function_name)) {
90                         $a = DI::app();
91                         return $function_name($a);
92                 }
93
94                 return '';
95         }
96 }