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