3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
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.
28 * @author Hypolite Petovan <mrpetovan@gmail.com>
30 class LegacyModule extends BaseModule
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.
38 private $moduleName = '';
40 public function __construct(string $file_path = '', array $parameters = [])
42 parent::__construct($parameters);
44 $this->setModuleFile($file_path);
48 * The only method that needs to be called, with the module/addon file name.
50 * @param string $file_path
53 private function setModuleFile($file_path)
55 if (!is_readable($file_path)) {
56 throw new \Exception(DI::l10n()->t('Legacy module file not found: %s', $file_path));
59 $this->moduleName = basename($file_path, '.php');
61 require_once $file_path;
64 public function init()
66 $this->runModuleFunction('init');
69 public function content(): string
71 return $this->runModuleFunction('content');
74 public function post()
76 $this->runModuleFunction('post');
80 * Runs the module function designated by the provided suffix if it exists, the BaseModule method otherwise
82 * @param string $function_suffix
86 private function runModuleFunction(string $function_suffix)
88 $function_name = $this->moduleName . '_' . $function_suffix;
90 if (\function_exists($function_name)) {
92 return $function_name($a);
94 return parent::{$function_suffix}($this->parameters);