Merge pull request #4091 from MrPetovan/task/4090-add-module-class-routing

Add module class routing
This commit is contained in:
Michael Vogel 2017-12-17 20:39:29 +01:00 committed by GitHub
commit 6b622fa9e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 15 deletions

View file

@ -220,7 +220,6 @@ if ((local_user()) || (! $privateapps === "1")) {
* so within the module init and/or post functions and then invoke killme() to terminate * so within the module init and/or post functions and then invoke killme() to terminate
* further processing. * further processing.
*/ */
if (strlen($a->module)) { if (strlen($a->module)) {
/** /**
@ -252,11 +251,17 @@ if (strlen($a->module)) {
} }
} }
// Controller class routing
if (! $a->module_loaded && class_exists('Friendica\\Module\\' . ucfirst($a->module))) {
$a->module_class = 'Friendica\\Module\\' . ucfirst($a->module);
$a->module_loaded = true;
}
/** /**
* 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
*/ */
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"; include_once "mod/{$a->module}.php";
$a->module_loaded = true; $a->module_loaded = true;
} }
@ -321,7 +326,10 @@ if ($a->module_loaded) {
$a->page['page_title'] = $a->module; $a->page['page_title'] = $a->module;
$placeholder = ''; $placeholder = '';
if (function_exists($a->module . '_init')) { if ($a->module_class) {
call_hooks($a->module . '_mod_init', $placeholder);
call_user_func([$a->module_class, 'init']);
} else if (function_exists($a->module . '_init')) {
call_hooks($a->module . '_mod_init', $placeholder); call_hooks($a->module . '_mod_init', $placeholder);
$func = $a->module . '_init'; $func = $a->module . '_init';
$func($a); $func($a);
@ -332,27 +340,36 @@ if ($a->module_loaded) {
$func($a); $func($a);
} }
if (($_SERVER['REQUEST_METHOD'] === 'POST') && (! $a->error) if (! $a->error && $_SERVER['REQUEST_METHOD'] === 'POST') {
&& (function_exists($a->module . '_post'))
&& (! x($_POST, 'auth-params'))
) {
call_hooks($a->module . '_mod_post', $_POST); call_hooks($a->module . '_mod_post', $_POST);
if ($a->module_class) {
call_user_func([$a->module_class, 'post']);
} else if (function_exists($a->module . '_post')) {
$func = $a->module . '_post'; $func = $a->module . '_post';
$func($a); $func($a);
} }
}
if ((! $a->error) && (function_exists($a->module . '_afterpost'))) { if (! $a->error) {
call_hooks($a->module . '_mod_afterpost', $placeholder); call_hooks($a->module . '_mod_afterpost', $placeholder);
if ($a->module_class) {
call_user_func([$a->module_class, 'afterpost']);
} else if (function_exists($a->module . '_afterpost')) {
$func = $a->module . '_afterpost'; $func = $a->module . '_afterpost';
$func($a); $func($a);
} }
}
if ((! $a->error) && (function_exists($a->module . '_content'))) { if (! $a->error) {
$arr = array('content' => $a->page['content']); $arr = array('content' => $a->page['content']);
call_hooks($a->module . '_mod_content', $arr); call_hooks($a->module . '_mod_content', $arr);
$a->page['content'] = $arr['content']; $a->page['content'] = $arr['content'];
if ($a->module_class) {
$arr = array('content' => call_user_func([$a->module_class, 'content']));
} else if (function_exists($a->module . '_content')) {
$func = $a->module . '_content'; $func = $a->module . '_content';
$arr = array('content' => $func($a)); $arr = array('content' => $func($a));
}
call_hooks($a->module . '_mod_aftercontent', $arr); call_hooks($a->module . '_mod_aftercontent', $arr);
$a->page['content'] .= $arr['content']; $a->page['content'] .= $arr['content'];
} }

View file

@ -30,6 +30,7 @@ use Exception;
class App { class App {
public $module_loaded = false; public $module_loaded = false;
public $module_class = null;
public $query_string; public $query_string;
public $config; public $config;
public $page; public $page;

60
src/BaseModule.php Normal file
View file

@ -0,0 +1,60 @@
<?php
namespace Friendica;
/**
* All modules in Friendica should extend BaseModule, although not all modules
* need to extend all the methods described here
*
* @author Hypolite Petovan mrpetovan@gmail.com
*/
abstract class BaseModule extends BaseObject
{
/**
* @brief Initialization method common to both content() and post()
*
* Extend this method if you need to do any shared processing before both
* content() or post()
*/
public static function init()
{
}
/**
* @brief Module GET method to display any content
*
* Extend this method if the module is supposed to return any display
* through a GET request. It can be an HTML page through templating or a
* XML feed or a JSON output.
*
* @return string
*/
public static function content()
{
$o = '';
return $o;
}
/**
* @brief Module POST method to process submitted data
*
* Extend this method if the module is supposed to process POST requests.
* Doesn't display any content
*/
public static function post()
{
// goaway('module');
}
/**
* @brief Called after post()
*
* Unknown purpose
*/
public static function afterpost()
{
}
}