Make BaseModule a real entity

- Add all dependencies, necessary to run the content (baseUrl, Arguments)
- Encapsulate all POST/GET/DELETE/PATCH/PUT methods as protected methods inside the BaseModule
- Return Module content ONLY per `BaseModule::run()` (including the Hook logic there as well)
This commit is contained in:
Philipp 2021-11-20 15:38:03 +01:00
parent 238613fd01
commit 8bdd90066f
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
252 changed files with 615 additions and 623 deletions

View file

@ -25,7 +25,6 @@ use ArrayAccess;
use DOMDocument;
use DOMXPath;
use Friendica\App;
use Friendica\Capabilities\ICanHandleRequests;
use Friendica\Content\Nav;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
@ -33,7 +32,6 @@ use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Theme;
use Friendica\Module\Special\HTTPException as ModuleHTTPException;
use Friendica\Network\HTTPException;
use Friendica\Util\Network;
use Friendica\Util\Strings;
@ -269,9 +267,9 @@ class Page implements ArrayAccess
if (!empty($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] == "on")) {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
} else {
@ -338,24 +336,13 @@ class Page implements ArrayAccess
* - module content
* - hooks for content
*
* @param ICanHandleRequests $module The module
* @param Mode $mode The Friendica execution mode
* @param string $content The content to print
* @param Mode $mode The Friendica execution mode
*
* @throws HTTPException\InternalServerErrorException
*/
private function initContent(ICanHandleRequests $module, Mode $mode)
private function initContent(string $content, Mode $mode)
{
$content = '';
try {
$arr = ['content' => $content];
Hook::callAll($module->getClassName() . '_mod_content', $arr);
$content = $arr['content'];
$content .= $module->content();
} catch (HTTPException $e) {
$content = (new ModuleHTTPException())->content($e);
}
// initialise content region
if ($mode->isNormal()) {
Hook::callAll('page_content_top', $this->page['content']);
@ -390,14 +377,14 @@ class Page implements ArrayAccess
* @param BaseURL $baseURL The Friendica Base URL
* @param Arguments $args The Friendica App arguments
* @param Mode $mode The current node mode
* @param ICanHandleRequests $module The loaded Friendica module
* @param string $content The content to print on frontend
* @param L10n $l10n The l10n language class
* @param IManageConfigValues $config The Configuration of this node
* @param IManagePersonalConfigValues $pconfig The personal/user configuration
*
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\InternalServerErrorException|HTTPException\ServiceUnavailableException
*/
public function run(App $app, BaseURL $baseURL, Arguments $args, Mode $mode, ICanHandleRequests $module, L10n $l10n, Profiler $profiler, IManageConfigValues $config, IManagePersonalConfigValues $pconfig)
public function run(App $app, BaseURL $baseURL, Arguments $args, Mode $mode, string $content, L10n $l10n, Profiler $profiler, IManageConfigValues $config, IManagePersonalConfigValues $pconfig)
{
$moduleName = $args->getModuleName();
@ -407,7 +394,7 @@ class Page implements ArrayAccess
* Sets the $Page->page['content'] variable
*/
$timestamp = microtime(true);
$this->initContent($module, $mode);
$this->initContent($content, $mode);
$profiler->set(microtime(true) - $timestamp, 'content');
// Load current theme info after module has been initialized as theme could have been set in module

View file

@ -39,6 +39,7 @@ use Friendica\Module\HTTPException\MethodNotAllowed;
use Friendica\Module\HTTPException\PageNotFound;
use Friendica\Network\HTTPException;
use Friendica\Network\HTTPException\MethodNotAllowedException;
use Friendica\Network\HTTPException\NoContentException;
use Friendica\Network\HTTPException\NotFoundException;
/**
@ -103,6 +104,9 @@ class Router
/** @var string */
private $baseRoutesFilepath;
/** @var array */
private $server;
/**
* @param array $server The $_SERVER variable
* @param string $baseRoutesFilepath The path to a base routes file to leverage cache, can be empty
@ -123,8 +127,17 @@ class Router
$this->args = $args;
$this->config = $config;
$this->dice = $dice;
$this->server = $server;
$httpMethod = $this->server['REQUEST_METHOD'] ?? self::GET;
// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
// @todo Check allowed methods per requested path
if ($httpMethod === static::OPTIONS) {
header('Allow: ' . implode(',', Router::ALLOWED_METHODS));
throw new NoContentException();
}
$httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
$this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
$this->routeCollector = isset($routeCollector) ?
@ -268,10 +281,9 @@ class Router
return $moduleClass;
}
public function getModule(): ICanHandleRequests
public function getModule(?string $module_class = null): ICanHandleRequests
{
$module_class = null;
$module_parameters = [];
$module_parameters = [$this->server];
/**
* ROUTING
*
@ -279,7 +291,7 @@ class Router
* post() and/or content() static methods can be respectively called to produce a data change or an output.
**/
try {
$module_class = $this->getModuleClass();
$module_class = $module_class ?? $this->getModuleClass();
$module_parameters[] = $this->parameters;
} catch (MethodNotAllowedException $e) {
$module_class = MethodNotAllowed::class;