mc-cms-namelessmc/core/classes/Core/Module.php

233 lines
6.4 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Module base class as well as management class.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.2.0
* @license MIT
*/
abstract class Module
{
/**
* @var Module[] Array of all modules
*/
2021-10-29 22:00:05 -07:00
private static iterable $_modules = [];
2021-10-07 14:02:46 -07:00
private string $_name;
private string $_author;
private string $_version;
private string $_nameless_version;
private array $_load_before;
private array $_load_after;
2020-12-13 20:42:04 -08:00
public function __construct(
Module $module,
string $name,
string $author,
string $version,
string $nameless_version,
array $load_before = [],
array $load_after = []
) {
2020-12-13 19:38:04 +01:00
self::$_modules[] = $module;
$this->_name = $name;
$this->_author = $author;
$this->_version = $version;
$this->_nameless_version = $nameless_version;
2020-12-29 22:00:53 +00:00
// All modules should load after core
if ($name !== 'Core') {
2020-12-29 22:00:53 +00:00
$load_after[] = 'Core';
}
2020-12-29 22:00:53 +00:00
$this->_load_before = $load_before;
$this->_load_after = $load_after;
2020-12-13 19:38:04 +01:00
}
/**
* Call `onPageLoad()` function for all registered modules.
2021-12-07 22:14:12 -08:00
*
* @param User $user User viewing the page.
* @param Pages $pages Instance of pages class.
* @param Cache $cache Instance of cache to pass.
* @param FakeSmarty|Smarty|null $smarty Instance of Smarty to pass
* @param Navigation[] $navs Array of loaded navigation menus.
* @param Widgets $widgets Instance of widget class to pass.
* @param TemplateBase $template Template to pass.
*/
public static function loadPage(User $user, Pages $pages, Cache $cache, $smarty, iterable $navs, Widgets $widgets, TemplateBase $template): void
{
2021-10-07 14:02:46 -07:00
foreach (self::getModules() as $module) {
2020-12-13 19:38:04 +01:00
$module->onPageLoad($user, $pages, $cache, $smarty, $navs, $widgets, $template);
}
}
/** @return Module[] */
public static function getModules(): iterable
{
2020-12-13 19:38:04 +01:00
return self::$_modules;
}
/**
* Handle page loading for this module.
* Often used to register permissions, sitemaps, widgets, etc.
*
* @param User $user User viewing the page.
* @param Pages $pages Instance of pages class.
* @param Cache $cache Instance of cache to pass.
* @param FakeSmarty|Smarty $smarty Instance of smarty to pass, to be removed in 2.3.0
* @param Navigation[] $navs Array of loaded navigation menus.
* @param Widgets $widgets Instance of widget class to pass.
* @param TemplateBase $template Active template to render.
*/
abstract public function onPageLoad(User $user, Pages $pages, Cache $cache, $smarty, iterable $navs, Widgets $widgets, TemplateBase $template);
2021-12-07 22:14:12 -08:00
/**
* Determine loading arrangement of modules.
*
* @return array Array with module order and any failed modules.
*/
public static function determineModuleOrder(): array
{
2021-12-07 22:14:12 -08:00
$module_order = ['Core'];
$failed = [];
2021-12-07 22:14:12 -08:00
foreach (self::getModules() as $module) {
if ($module->getName() == 'Core') {
continue;
}
2022-01-15 15:18:05 -08:00
foreach ($module_order as $n => $nValue) {
$before_after = Util::findBeforeAfter($module_order, $nValue);
2020-12-29 22:00:53 +00:00
2021-12-07 22:14:12 -08:00
if (!array_diff($module->getLoadAfter(), $before_after[0]) && !array_diff($module->getLoadBefore(), $before_after[1])) {
array_splice($module_order, $n + 1, 0, $module->getName());
continue 2;
}
}
$failed[] = $module->getName();
}
return ['modules' => $module_order, 'failed' => $failed];
2020-12-29 22:00:53 +00:00
}
public function getName(): string
{
2021-12-07 22:14:12 -08:00
return $this->_name;
2020-12-29 22:00:53 +00:00
}
/**
* Get the names of modules that this module should load after.
*
* @return array Array of module names that this module should load after.
*/
public function getLoadAfter(): array
{
2021-12-07 22:14:12 -08:00
return $this->_load_after;
}
2020-12-29 22:00:53 +00:00
/**
* Get the names of modules that this module should load before.
*
* @return array Array of module names that this module should load before.
*/
public function getLoadBefore(): array
{
2021-12-07 22:14:12 -08:00
return $this->_load_before;
}
2020-12-29 22:00:53 +00:00
2022-01-15 15:18:05 -08:00
abstract public function onInstall();
2020-12-29 22:00:53 +00:00
2022-01-15 15:18:05 -08:00
abstract public function onUninstall();
2020-12-29 22:00:53 +00:00
2022-01-15 15:18:05 -08:00
abstract public function onEnable();
2020-12-29 22:00:53 +00:00
2022-01-15 15:18:05 -08:00
abstract public function onDisable();
2021-12-07 22:14:12 -08:00
/**
* Get debug information to display on the external debug link page.
*
* @return array Debug information for this module.
*/
2022-01-15 15:18:05 -08:00
abstract public function getDebugInfo(): array;
2021-12-07 22:14:12 -08:00
/**
* Get this module's author.
*
* @return string The author of this module.
*/
public function getAuthor(): string
{
2021-12-07 22:14:12 -08:00
return $this->_author;
}
/**
* Get this module's version.
*
* @return string The version of this module.
*/
public function getVersion(): string
{
2021-12-07 22:14:12 -08:00
return $this->_version;
}
/**
* Get this module's supported NamelessMC version.
2021-12-07 22:14:12 -08:00
*
* @return string The supported NamelessMC version of this module.
2021-12-07 22:14:12 -08:00
*/
public function getNamelessVersion(): string
{
2021-12-07 22:14:12 -08:00
return $this->_nameless_version;
}
/**
* Get this module's ID.
*
* @return int The ID for the module
*/
public function getId(): int
{
return DB::getInstance()->query('SELECT `id` FROM nl2_modules WHERE `name` = ?', [$this->_name])->first()->id;
}
/**
* Get a module ID from name.
*
* @param string $name Module name
*
* @return ?int Module ID
*/
public static function getIdFromName(string $name): ?int
{
$query = DB::getInstance()->get('modules', ['name', $name]);
if ($query->count()) {
return $query->first()->id;
}
return null;
}
/**
* Get a module name from ID.
*
* @param int $id Module ID
*
* @return ?string Module name
*/
public static function getNameFromId(int $id): ?string
{
$query = DB::getInstance()->get('modules', ['id', $id]);
if ($query->count()) {
return $query->first()->name;
}
return null;
}
2020-12-13 19:38:04 +01:00
}