]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #4091 from MrPetovan/task/4090-add-module-class-routing
authorMichael Vogel <icarus@dabo.de>
Sun, 17 Dec 2017 19:39:29 +0000 (20:39 +0100)
committerGitHub <noreply@github.com>
Sun, 17 Dec 2017 19:39:29 +0000 (20:39 +0100)
Add module class routing

index.php
src/App.php
src/BaseModule.php [new file with mode: 0644]

index 0290a38be7392666144dd9a4d759915b09ce7ab1..9cbbad605aefe62d85753b59d32e9991fae272f2 100644 (file)
--- a/index.php
+++ b/index.php
@@ -220,7 +220,6 @@ if ((local_user()) || (! $privateapps === "1")) {
  * so within the module init and/or post functions and then invoke killme() to terminate
  * further processing.
  */
-
 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 ((! $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";
                $a->module_loaded = true;
        }
@@ -321,7 +326,10 @@ if ($a->module_loaded) {
        $a->page['page_title'] = $a->module;
        $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);
                $func = $a->module . '_init';
                $func($a);
@@ -332,27 +340,36 @@ if ($a->module_loaded) {
                $func($a);
        }
 
-       if (($_SERVER['REQUEST_METHOD'] === 'POST') && (! $a->error)
-               && (function_exists($a->module . '_post'))
-               && (! x($_POST, 'auth-params'))
-       ) {
+       if (! $a->error && $_SERVER['REQUEST_METHOD'] === 'POST') {
                call_hooks($a->module . '_mod_post', $_POST);
-               $func = $a->module . '_post';
-               $func($a);
+               if ($a->module_class) {
+                       call_user_func([$a->module_class, 'post']);
+               } else if (function_exists($a->module . '_post')) {
+                       $func = $a->module . '_post';
+                       $func($a);
+               }
        }
 
-       if ((! $a->error) && (function_exists($a->module . '_afterpost'))) {
+       if (! $a->error) {
                call_hooks($a->module . '_mod_afterpost', $placeholder);
-               $func = $a->module . '_afterpost';
-               $func($a);
+               if ($a->module_class) {
+                       call_user_func([$a->module_class, 'afterpost']);
+               } else if (function_exists($a->module . '_afterpost')) {
+                       $func = $a->module . '_afterpost';
+                       $func($a);
+               }
        }
 
-       if ((! $a->error) && (function_exists($a->module . '_content'))) {
+       if (! $a->error) {
                $arr = array('content' => $a->page['content']);
                call_hooks($a->module . '_mod_content', $arr);
                $a->page['content'] = $arr['content'];
-               $func = $a->module . '_content';
-               $arr = array('content' => $func($a));
+               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';
+                       $arr = array('content' => $func($a));
+               }
                call_hooks($a->module . '_mod_aftercontent', $arr);
                $a->page['content'] .= $arr['content'];
        }
index 3e34d3c5a9e76c824e91f329d1c4afc69bd755a4..53abc15f12acf60d6a6a5c334e369f51ff0e1242 100644 (file)
@@ -30,6 +30,7 @@ use Exception;
 class App {
 
        public $module_loaded = false;
+       public $module_class = null;
        public $query_string;
        public $config;
        public $page;
diff --git a/src/BaseModule.php b/src/BaseModule.php
new file mode 100644 (file)
index 0000000..08f7755
--- /dev/null
@@ -0,0 +1,60 @@
+<?php\r
+\r
+namespace Friendica;\r
+\r
+/**\r
+ * All modules in Friendica should extend BaseModule, although not all modules\r
+ * need to extend all the methods described here\r
+ *\r
+ * @author Hypolite Petovan mrpetovan@gmail.com\r
+ */\r
+abstract class BaseModule extends BaseObject\r
+{\r
+       /**\r
+        * @brief Initialization method common to both content() and post()\r
+        *\r
+        * Extend this method if you need to do any shared processing before both\r
+        * content() or post()\r
+        */\r
+       public static function init()\r
+       {\r
+\r
+       }\r
+\r
+       /**\r
+        * @brief Module GET method to display any content\r
+        *\r
+        * Extend this method if the module is supposed to return any display\r
+        * through a GET request. It can be an HTML page through templating or a\r
+        * XML feed or a JSON output.\r
+        *\r
+        * @return string\r
+        */\r
+       public static function content()\r
+       {\r
+               $o = '';\r
+\r
+               return $o;\r
+       }\r
+\r
+       /**\r
+        * @brief Module POST method to process submitted data\r
+        *\r
+        * Extend this method if the module is supposed to process POST requests.\r
+        * Doesn't display any content\r
+        */\r
+       public static function post()\r
+       {\r
+               // goaway('module');\r
+       }\r
+\r
+       /**\r
+        * @brief Called after post()\r
+        *\r
+        * Unknown purpose\r
+        */\r
+       public static function afterpost()\r
+       {\r
+\r
+       }\r
+}\r