]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Module.php
Merge pull request #7828 from nupplaphil/task/move_enotify
[friendica.git] / src / App / Module.php
index 9a24c55544199635cde4a956577070491ce6519f..868520c0257b23e5b77c6fa6c7ec7a24f52d1fae 100644 (file)
@@ -7,7 +7,10 @@ use Friendica\BaseObject;
 use Friendica\Core;
 use Friendica\LegacyModule;
 use Friendica\Module\Home;
-use Friendica\Module\PageNotFound;
+use Friendica\Module\HTTPException\MethodNotAllowed;
+use Friendica\Module\HTTPException\PageNotFound;
+use Friendica\Network\HTTPException\MethodNotAllowedException;
+use Friendica\Network\HTTPException\NotFoundException;
 use Psr\Log\LoggerInterface;
 
 /**
@@ -60,6 +63,11 @@ class Module
         */
        private $module_class;
 
+       /**
+        * @var array The module parameters
+        */
+       private $module_parameters;
+
        /**
         * @var bool true, if the module is a backend module
         */
@@ -86,6 +94,14 @@ class Module
                return $this->module_class;
        }
 
+       /**
+        * @return array The module parameters extracted from the route
+        */
+       public function getParameters()
+       {
+               return $this->module_parameters;
+       }
+
        /**
         * @return bool True, if the current module is a backend module
         * @see Module::BACKEND_MODULES for a list
@@ -95,10 +111,11 @@ class Module
                return $this->isBackend;
        }
 
-       public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, bool $isBackend = false, bool $printNotAllowedAddon = false)
+       public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, array $moduleParameters = [], bool $isBackend = false, bool $printNotAllowedAddon = false)
        {
                $this->module               = $module;
                $this->module_class         = $moduleClass;
+               $this->module_parameters    = $moduleParameters;
                $this->isBackend            = $isBackend;
                $this->printNotAllowedAddon = $printNotAllowedAddon;
        }
@@ -126,7 +143,7 @@ class Module
 
                $isBackend = in_array($module, Module::BACKEND_MODULES);;
 
-               return new Module($module, $this->module_class, $isBackend, $this->printNotAllowedAddon);
+               return new Module($module, $this->module_class, [], $isBackend, $this->printNotAllowedAddon);
        }
 
        /**
@@ -144,40 +161,47 @@ class Module
        {
                $printNotAllowedAddon = false;
 
+               $module_class = null;
+               $module_parameters = [];
                /**
                 * ROUTING
                 *
                 * From the request URL, routing consists of obtaining the name of a BaseModule-extending class of which the
                 * post() and/or content() static methods can be respectively called to produce a data change or an output.
                 **/
-               $module_class = $router->getModuleClass($args->getCommand());
-
-               // Then we try addon-provided modules that we wrap in the LegacyModule class
-               if (!$module_class && Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
-                       //Check if module is an app and if public access to apps is allowed or not
-                       $privateapps = $config->get('config', 'private_addons', false);
-                       if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
-                               $printNotAllowedAddon = true;
-                       } else {
-                               include_once "addon/{$this->module}/{$this->module}.php";
-                               if (function_exists($this->module . '_module')) {
-                                       LegacyModule::setModuleFile("addon/{$this->module}/{$this->module}.php");
-                                       $module_class = LegacyModule::class;
+               try {
+                       $module_class = $router->getModuleClass($args->getCommand());
+                       $module_parameters = $router->getModuleParameters();
+               } catch (MethodNotAllowedException $e) {
+                       $module_class = MethodNotAllowed::class;
+               } catch (NotFoundException $e) {
+                       // Then we try addon-provided modules that we wrap in the LegacyModule class
+                       if (Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
+                               //Check if module is an app and if public access to apps is allowed or not
+                               $privateapps = $config->get('config', 'private_addons', false);
+                               if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
+                                       $printNotAllowedAddon = true;
+                               } else {
+                                       include_once "addon/{$this->module}/{$this->module}.php";
+                                       if (function_exists($this->module . '_module')) {
+                                               LegacyModule::setModuleFile("addon/{$this->module}/{$this->module}.php");
+                                               $module_class = LegacyModule::class;
+                                       }
                                }
                        }
-               }
 
-               /* Finally, we look for a 'standard' program module in the 'mod' directory
-                * We emulate a Module class through the LegacyModule class
-                */
-               if (!$module_class && file_exists("mod/{$this->module}.php")) {
-                       LegacyModule::setModuleFile("mod/{$this->module}.php");
-                       $module_class = LegacyModule::class;
-               }
+                       /* Finally, we look for a 'standard' program module in the 'mod' directory
+                        * We emulate a Module class through the LegacyModule class
+                        */
+                       if (!$module_class && file_exists("mod/{$this->module}.php")) {
+                               LegacyModule::setModuleFile("mod/{$this->module}.php");
+                               $module_class = LegacyModule::class;
+                       }
 
-               $module_class = !isset($module_class) ? PageNotFound::class : $module_class;
+                       $module_class = $module_class ?: PageNotFound::class;
+               }
 
-               return new Module($this->module, $module_class, $this->isBackend, $printNotAllowedAddon);
+               return new Module($this->module, $module_class, $module_parameters, $this->isBackend, $printNotAllowedAddon);
        }
 
        /**
@@ -225,18 +249,18 @@ class Module
 
                Core\Hook::callAll($this->module . '_mod_init', $placeholder);
 
-               call_user_func([$this->module_class, 'init']);
+               call_user_func([$this->module_class, 'init'], $this->module_parameters);
 
                // "rawContent" is especially meant for technical endpoints.
                // This endpoint doesn't need any theme initialization or other comparable stuff.
-               call_user_func([$this->module_class, 'rawContent']);
+               call_user_func([$this->module_class, 'rawContent'], $this->module_parameters);
 
                if ($server['REQUEST_METHOD'] === 'POST') {
                        Core\Hook::callAll($this->module . '_mod_post', $post);
-                       call_user_func([$this->module_class, 'post']);
+                       call_user_func([$this->module_class, 'post'], $this->module_parameters);
                }
 
                Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
-               call_user_func([$this->module_class, 'afterpost']);
+               call_user_func([$this->module_class, 'afterpost'], $this->module_parameters);
        }
 }