Refactor App\Module to App\ModuleController and rename properties

This commit is contained in:
Philipp 2021-11-16 22:34:49 +01:00
parent 8f741c8b78
commit c95d47b0b4
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
9 changed files with 93 additions and 92 deletions

View file

@ -139,14 +139,14 @@ class Mode
/**
* Checks if the site is called via a backend process
*
* @param bool $isBackend True, if the call is from a backend script (daemon, worker, ...)
* @param Module $module The pre-loaded module (just name, not class!)
* @param array $server The $_SERVER variable
* @param MobileDetect $mobileDetect The mobile detection library
* @param bool $isBackend True, if the call is from a backend script (daemon, worker, ...)
* @param ModuleController $module The pre-loaded module (just name, not class!)
* @param array $server The $_SERVER variable
* @param MobileDetect $mobileDetect The mobile detection library
*
* @return Mode returns the determined mode
*/
public function determineRunMode(bool $isBackend, Module $module, array $server, MobileDetect $mobileDetect)
public function determineRunMode(bool $isBackend, ModuleController $module, array $server, MobileDetect $mobileDetect)
{
foreach (self::BACKEND_CONTENT_TYPES as $type) {
if (strpos(strtolower($server['HTTP_ACCEPT'] ?? ''), $type) !== false) {

View file

@ -39,7 +39,7 @@ use Psr\Log\LoggerInterface;
/**
* Holds the common context of the current, loaded module
*/
class Module
class ModuleController
{
const DEFAULT = 'home';
const DEFAULT_CLASS = Home::class;
@ -78,12 +78,12 @@ class Module
/**
* @var string The module name
*/
private $module;
private $moduleName;
/**
* @var ICanHandleRequests The module class
*/
private $module_class;
private $module;
/**
* @var bool true, if the module is a backend module
@ -99,33 +99,33 @@ class Module
* @return string
*/
public function getName()
{
return $this->moduleName;
}
/**
* @return ICanHandleRequests The base module object
*/
public function getModule(): ICanHandleRequests
{
return $this->module;
}
/**
* @return ICanHandleRequests The base class name
*/
public function getClass(): ICanHandleRequests
{
return $this->module_class;
}
/**
* @return bool True, if the current module is a backend module
* @see Module::BACKEND_MODULES for a list
* @see ModuleController::BACKEND_MODULES for a list
*/
public function isBackend()
{
return $this->isBackend;
}
public function __construct(string $module = self::DEFAULT, ICanHandleRequests $module_class = null, bool $isBackend = false, bool $printNotAllowedAddon = false)
public function __construct(string $moduleName = self::DEFAULT, ICanHandleRequests $module = null, bool $isBackend = false, bool $printNotAllowedAddon = false)
{
$defaultClass = static::DEFAULT_CLASS;
$this->module = $module;
$this->module_class = $module_class ?? new $defaultClass();
$this->moduleName = $moduleName;
$this->module = $module ?? new $defaultClass();
$this->isBackend = $isBackend;
$this->printNotAllowedAddon = $printNotAllowedAddon;
}
@ -135,9 +135,9 @@ class Module
*
* @param Arguments $args The Friendica arguments
*
* @return Module The module with the determined module
* @return ModuleController The module with the determined module
*/
public function determineModule(Arguments $args)
public function determineName(Arguments $args)
{
if ($args->getArgc() > 0) {
$module = str_replace('.', '_', $args->get(0));
@ -151,9 +151,10 @@ class Module
$module = "login";
}
$isBackend = in_array($module, Module::BACKEND_MODULES);;
$isBackend = in_array($module, ModuleController::BACKEND_MODULES);
;
return new Module($module, null, $isBackend, $this->printNotAllowedAddon);
return new ModuleController($module, null, $isBackend, $this->printNotAllowedAddon);
}
/**
@ -164,7 +165,7 @@ class Module
* @param IManageConfigValues $config The Friendica Configuration
* @param Dice $dice The Dependency Injection container
*
* @return Module The determined module of this call
* @return ModuleController The determined module of this call
*
* @throws \Exception
*/
@ -172,7 +173,7 @@ class Module
{
$printNotAllowedAddon = false;
$module_class = null;
$module_class = null;
$module_parameters = [];
/**
* ROUTING
@ -181,21 +182,21 @@ class Module
* post() and/or content() static methods can be respectively called to produce a data change or an output.
**/
try {
$module_class = $router->getModuleClass($args->getCommand());
$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")) {
if (Core\Addon::isEnabled($this->moduleName) && file_exists("addon/{$this->moduleName}/{$this->moduleName}.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) {
if ((!local_user()) && Core\Hook::isAddonApp($this->moduleName) && $privateapps) {
$printNotAllowedAddon = true;
} else {
include_once "addon/{$this->module}/{$this->module}.php";
if (function_exists($this->module . '_module')) {
$module_parameters[] = "addon/{$this->module}/{$this->module}.php";
include_once "addon/{$this->moduleName}/{$this->moduleName}.php";
if (function_exists($this->moduleName . '_module')) {
$module_parameters[] = "addon/{$this->moduleName}/{$this->moduleName}.php";
$module_class = LegacyModule::class;
}
}
@ -204,8 +205,8 @@ class Module
/* 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")) {
$module_parameters[] = "mod/{$this->module}.php";
if (!$module_class && file_exists("mod/{$this->moduleName}.php")) {
$module_parameters[] = "mod/{$this->moduleName}.php";
$module_class = LegacyModule::class;
}
@ -215,7 +216,7 @@ class Module
/** @var ICanHandleRequests $module */
$module = $dice->create($module_class, $module_parameters);
return new Module($this->module, $module, $this->isBackend, $printNotAllowedAddon);
return new ModuleController($this->moduleName, $module, $this->isBackend, $printNotAllowedAddon);
}
/**
@ -244,7 +245,7 @@ class Module
*
* Otherwise we are going to emit a 404 not found.
*/
if ($this->module_class === PageNotFound::class) {
if ($this->module === PageNotFound::class) {
$queryString = $server['QUERY_STRING'];
// Stupid browser tried to pre-fetch our Javascript img template. Don't log the event or return anything - just quietly exit.
if (!empty($queryString) && preg_match('/{[0-9]}/', $queryString) !== 0) {
@ -295,31 +296,31 @@ class Module
$profiler->set(microtime(true), 'ready');
$timestamp = microtime(true);
Core\Hook::callAll($this->module . '_mod_init', $placeholder);
Core\Hook::callAll($this->moduleName . '_mod_init', $placeholder);
$this->module_class->init();
$this->module->init();
$profiler->set(microtime(true) - $timestamp, 'init');
if ($server['REQUEST_METHOD'] === Router::DELETE) {
$this->module_class->delete();
$this->module->delete();
}
if ($server['REQUEST_METHOD'] === Router::PATCH) {
$this->module_class->patch();
$this->module->patch();
}
if ($server['REQUEST_METHOD'] === Router::POST) {
Core\Hook::callAll($this->module . '_mod_post', $post);
$this->module_class->post();
Core\Hook::callAll($this->moduleName . '_mod_post', $post);
$this->module->post();
}
if ($server['REQUEST_METHOD'] === Router::PUT) {
$this->module_class->put();
$this->module->put();
}
// "rawContent" is especially meant for technical endpoints.
// This endpoint doesn't need any theme initialization or other comparable stuff.
$this->module_class->rawContent();
$this->module->rawContent();
}
}

View file

@ -191,14 +191,14 @@ class Page implements ArrayAccess
* - head.tpl template
*
* @param App $app The Friendica App instance
* @param Module $module The loaded Friendica module
* @param ModuleController $module The loaded Friendica module
* @param L10n $l10n The l10n language instance
* @param IManageConfigValues $config The Friendica configuration
* @param IManagePersonalConfigValues $pConfig The Friendica personal configuration (for user)
*
* @throws HTTPException\InternalServerErrorException
*/
private function initHead(App $app, Module $module, L10n $l10n, IManageConfigValues $config, IManagePersonalConfigValues $pConfig)
private function initHead(App $app, ModuleController $module, L10n $l10n, IManageConfigValues $config, IManagePersonalConfigValues $pConfig)
{
$interval = ((local_user()) ? $pConfig->get(local_user(), 'system', 'update_interval') : 40000);
@ -337,17 +337,17 @@ class Page implements ArrayAccess
* - module content
* - hooks for content
*
* @param Module $module The module
* @param Mode $mode The Friendica execution mode
* @param ModuleController $module The module
* @param Mode $mode The Friendica execution mode
*
* @throws HTTPException\InternalServerErrorException
*/
private function initContent(Module $module, Mode $mode)
private function initContent(ModuleController $module, Mode $mode)
{
$content = '';
try {
$moduleClass = $module->getClass();
$moduleClass = $module->getModule();
$arr = ['content' => $content];
Hook::callAll($moduleClass->getClassName() . '_mod_content', $arr);
@ -389,14 +389,14 @@ class Page implements ArrayAccess
* @param App $app The Friendica App
* @param BaseURL $baseURL The Friendica Base URL
* @param Mode $mode The current node mode
* @param Module $module The loaded Friendica module
* @param ModuleController $module The loaded Friendica module
* @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
*/
public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, Profiler $profiler, IManageConfigValues $config, IManagePersonalConfigValues $pconfig)
public function run(App $app, BaseURL $baseURL, Mode $mode, ModuleController $module, L10n $l10n, Profiler $profiler, IManageConfigValues $config, IManagePersonalConfigValues $pconfig)
{
$moduleName = $module->getName();