Merge pull request #5979 from MrPetovan/task/mock-legacy-module
Encapsulate legacy module functions in a BaseModule-inherited class
This commit is contained in:
commit
f14dae8d77
29
index.php
29
index.php
|
@ -260,6 +260,8 @@ if (strlen($a->module)) {
|
||||||
} else {
|
} else {
|
||||||
include_once "addon/{$a->module}/{$a->module}.php";
|
include_once "addon/{$a->module}/{$a->module}.php";
|
||||||
if (function_exists($a->module . '_module')) {
|
if (function_exists($a->module . '_module')) {
|
||||||
|
Friendica\LegacyModule::setModuleFile("addon/{$a->module}/{$a->module}.php");
|
||||||
|
$a->module_class = 'Friendica\\LegacyModule';
|
||||||
$a->module_loaded = true;
|
$a->module_loaded = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -273,10 +275,11 @@ if (strlen($a->module)) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If not, next look for a 'standard' program module in the 'mod' directory
|
* If not, next look for a 'standard' program module in the 'mod' directory
|
||||||
|
* We emulate a Module class through the LegacyModule class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (! $a->module_loaded && file_exists("mod/{$a->module}.php")) {
|
if (! $a->module_loaded && file_exists("mod/{$a->module}.php")) {
|
||||||
include_once "mod/{$a->module}.php";
|
Friendica\LegacyModule::setModuleFile("mod/{$a->module}.php");
|
||||||
|
$a->module_class = 'Friendica\\LegacyModule';
|
||||||
$a->module_loaded = true;
|
$a->module_loaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,16 +339,11 @@ if ($a->module_loaded) {
|
||||||
|
|
||||||
Addon::callHooks($a->module . '_mod_init', $placeholder);
|
Addon::callHooks($a->module . '_mod_init', $placeholder);
|
||||||
|
|
||||||
if ($a->module_class) {
|
|
||||||
call_user_func([$a->module_class, 'init']);
|
call_user_func([$a->module_class, 'init']);
|
||||||
} else if (function_exists($a->module . '_init')) {
|
|
||||||
$func = $a->module . '_init';
|
|
||||||
$func($a);
|
|
||||||
}
|
|
||||||
|
|
||||||
// "rawContent" is especially meant for technical endpoints.
|
// "rawContent" is especially meant for technical endpoints.
|
||||||
// This endpoint doesn't need any theme initialization or other comparable stuff.
|
// This endpoint doesn't need any theme initialization or other comparable stuff.
|
||||||
if (!$a->error && $a->module_class) {
|
if (!$a->error) {
|
||||||
call_user_func([$a->module_class, 'rawContent']);
|
call_user_func([$a->module_class, 'rawContent']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,34 +354,19 @@ if ($a->module_loaded) {
|
||||||
|
|
||||||
if (! $a->error && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
if (! $a->error && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
Addon::callHooks($a->module . '_mod_post', $_POST);
|
Addon::callHooks($a->module . '_mod_post', $_POST);
|
||||||
if ($a->module_class) {
|
|
||||||
call_user_func([$a->module_class, 'post']);
|
call_user_func([$a->module_class, 'post']);
|
||||||
} else if (function_exists($a->module . '_post')) {
|
|
||||||
$func = $a->module . '_post';
|
|
||||||
$func($a);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $a->error) {
|
if (! $a->error) {
|
||||||
Addon::callHooks($a->module . '_mod_afterpost', $placeholder);
|
Addon::callHooks($a->module . '_mod_afterpost', $placeholder);
|
||||||
if ($a->module_class) {
|
|
||||||
call_user_func([$a->module_class, 'afterpost']);
|
call_user_func([$a->module_class, 'afterpost']);
|
||||||
} else if (function_exists($a->module . '_afterpost')) {
|
|
||||||
$func = $a->module . '_afterpost';
|
|
||||||
$func($a);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $a->error) {
|
if (! $a->error) {
|
||||||
$arr = ['content' => $a->page['content']];
|
$arr = ['content' => $a->page['content']];
|
||||||
Addon::callHooks($a->module . '_mod_content', $arr);
|
Addon::callHooks($a->module . '_mod_content', $arr);
|
||||||
$a->page['content'] = $arr['content'];
|
$a->page['content'] = $arr['content'];
|
||||||
if ($a->module_class) {
|
|
||||||
$arr = ['content' => call_user_func([$a->module_class, 'content'])];
|
$arr = ['content' => call_user_func([$a->module_class, 'content'])];
|
||||||
} else if (function_exists($a->module . '_content')) {
|
|
||||||
$func = $a->module . '_content';
|
|
||||||
$arr = ['content' => $func($a)];
|
|
||||||
}
|
|
||||||
Addon::callHooks($a->module . '_mod_aftercontent', $arr);
|
Addon::callHooks($a->module . '_mod_aftercontent', $arr);
|
||||||
$a->page['content'] .= $arr['content'];
|
$a->page['content'] .= $arr['content'];
|
||||||
}
|
}
|
||||||
|
|
73
src/LegacyModule.php
Normal file
73
src/LegacyModule.php
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This mock module enable class encapsulation of legacy global function modules.
|
||||||
|
* After having provided the module file name, all the methods will behave like a normal Module class.
|
||||||
|
*
|
||||||
|
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||||
|
*/
|
||||||
|
class LegacyModule extends BaseModule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The module name, which is the name of the file (without the .php suffix)
|
||||||
|
* It's used to check for existence of the module functions.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $moduleName = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The only method that needs to be called, with the module/addon file name.
|
||||||
|
*
|
||||||
|
* @param string $file_path
|
||||||
|
*/
|
||||||
|
public static function setModuleFile($file_path)
|
||||||
|
{
|
||||||
|
if (!is_readable($file_path)) {
|
||||||
|
throw new Exception(Core\L10n::t('Legacy module file not found: %s', $file_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$moduleName = basename($file_path, '.php');
|
||||||
|
|
||||||
|
require_once $file_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function init()
|
||||||
|
{
|
||||||
|
self::runModuleFunction('init');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function content()
|
||||||
|
{
|
||||||
|
return self::runModuleFunction('content');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function post()
|
||||||
|
{
|
||||||
|
self::runModuleFunction('post');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function afterpost()
|
||||||
|
{
|
||||||
|
self::runModuleFunction('afterpost');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the module function designated by the provided suffix if it exists, the BaseModule method otherwise
|
||||||
|
*
|
||||||
|
* @param string $function_suffix
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private static function runModuleFunction($function_suffix)
|
||||||
|
{
|
||||||
|
$function_name = static::$moduleName . '_' . $function_suffix;
|
||||||
|
|
||||||
|
if (\function_exists($function_name)) {
|
||||||
|
return $function_name(self::getApp());
|
||||||
|
} else {
|
||||||
|
return parent::{$function_suffix}();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue