Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading modules via composer #3548

Draft
wants to merge 24 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Dockerfile.phpdoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ RUN mkdir /target && \
phpdoc run \
-d 'core/classes' \
-d 'modules/Core/classes' \
-d 'modules/Discord Integration/classes' \
-d 'modules/Cookie Consent/classes' \
-d 'modules/Forum/classes' \
-d 'modules/Members/classes' \
-i vendor \
-t /target

Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
"joypixels/emoji-toolkit": "^7.0",
"geoip2/geoip2": "^2.13",
"jenssegers/agent": "^2.6",
"php-di/php-di": "^6.4"
"illuminate/container": "^8.0",
"namelessmc/members-module": "dev-main",
"namelessmc/cookie-consent-module": "dev-main",
"namelessmc/discord-integration-module": "dev-main"
},
"require-dev": {
"phpstan/phpstan": "1.6.9",
Expand All @@ -44,17 +47,12 @@
"autoload": {
"classmap": [
"core/classes",
"modules/Cookie Consent/classes",
"modules/Core/classes",
"modules/Core/hooks",
"modules/Core/widgets",
"modules/Discord Integration/classes",
"modules/Discord Integration/hooks",
"modules/Discord Integration/widgets",
"modules/Forum/classes",
"modules/Forum/hooks",
"modules/Forum/widgets",
"modules/Members/classes"
"modules/Forum/widgets"
]
}
}
12 changes: 12 additions & 0 deletions core/classes/Core/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ public function retrieveAll(bool $meta = false): array
return $results;
}

public function fetch(string $key, callable $callback, int $expiration = 0)
{
if ($this->isCached($key)) {
return $this->retrieve($key);
}

$data = $callback();
$this->store($key, $data, $expiration);

return $data;
}

/**
* Erase cached entry by its key.
*
Expand Down
65 changes: 65 additions & 0 deletions core/classes/Core/ComposerModuleDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
class ComposerModuleDiscovery
{
private static array $_modules = [];

/**
* @return ComposerModuleWrapper[]
*/
public static function discoverModules(): array
{
// Avoid registering multiple instances of same modules if this is called twice in a
// single request; such as on the panel modules page
if (!empty(self::$_modules)) {
return self::$_modules;
}

$modules = [];
$packages = json_decode(file_get_contents(ROOT_PATH . '/vendor/composer/installed.json'), true)['packages'];
foreach ($packages as $package) {
if ($package['type'] === 'nameless-module') {
$modules[] = self::fromPackage($package);
}
}

return self::$_modules = $modules;
}

/**
* @param array $allEnabledModules
* @param ComposerModuleWrapper[] $composerModules
*/
public static function bootModules(\Illuminate\Container\Container $container, array $allEnabledModules, array $composerModules): void
{
foreach ($composerModules as $composerModule) {
if (!in_array($composerModule->getName(), array_column($allEnabledModules, 'name'))) {
continue;
}

self::bootModule($container, $composerModule);
}
}

public static function bootModule(\Illuminate\Container\Container $container, ComposerModuleWrapper $composerModule): void
{
/** @var NamelessMC\Framework\Extend\BaseExtender[] $extenders */
$extenders = require_once ROOT_PATH . '/vendor/' . $composerModule->getPackageName() . '/module.php';
foreach ($extenders as $extender) {
$extender->setModule($composerModule)->extend($container);
}
}

public static function fromPackage(array $composerPackage): ComposerModuleWrapper
{
return new ComposerModuleWrapper(
$composerPackage['name'],
$composerPackage['extra']['nameless_module']['name'],
$composerPackage['extra']['nameless_module']['display_name'],
$composerPackage['authors'][0]['name'],
$composerPackage['authors'][0]['homepage'],
$composerPackage['extra']['nameless_module']['version'],
$composerPackage['extra']['nameless_module']['nameless_version'],
$composerPackage['source']['url'],
);
}
}
180 changes: 180 additions & 0 deletions core/classes/Core/ComposerModuleWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
use Illuminate\Container\Container;
class ComposerModuleWrapper extends Module
{
private string $_packageName;
private string $_privateName;
private string $_authorName;
private string $_authorHomepage;
private string $_repositoryUrl;

// Lifecycle callbacks
private array $_onInstall = [];
private array $_onEnable = [];
private array $_onDisable = [];
private array $_onUninstall = [];

// Misc
private string $_debugInfoProvider;

public function __construct(
string $packageName,
string $privateName,
string $displayName,
string $authorName,
string $authorHomepage,
string $moduleVersion,
string $namelessVersion,
string $repositoryUrl
) {
$this->_packageName = $packageName;
$this->_privateName = $privateName;
$this->_authorName = $authorName;
$this->_authorHomepage = $authorHomepage;
$this->_repositoryUrl = $repositoryUrl;

parent::__construct($this, $displayName, $authorName, $moduleVersion, $namelessVersion);
}

public function getPackageName(): string
{
return $this->_packageName;
}

public function getPrivateName(): string
{
return $this->_privateName;
}

public function getRepositoryUrl(): string
{
return $this->_repositoryUrl;
}

public function getAuthor(): string
{
return "<a href='{$this->_authorHomepage}' target='_blank' rel='nofollow noopener'>{$this->_authorName}</a>";
}

public function setOnInstall(array $callbacks): void
{
$this->_onInstall = $callbacks;
}

public function setOnEnable(array $callbacks): void
{
$this->_onEnable = $callbacks;
}

public function setOnDisable(array $callbacks): void
{
$this->_onDisable = $callbacks;
}

public function setOnUninstall(array $callbacks): void
{
$this->_onUninstall = $callbacks;
}

public function setDebugInfoProvider(string $provider): void
{
$this->_debugInfoProvider = $provider;
}

public function onPageLoad(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, ?TemplateBase $template)
{
// ...
}

public function onInstall()
{
$this->runMigrations();
$this->callLifecycleHooks($this->_onInstall);
}

public function onEnable()
{
$this->callLifecycleHooks($this->_onEnable);
}

public function onDisable()
{
$this->callLifecycleHooks($this->_onDisable);
}

public function onUninstall()
{
// TODO, should this be before or after
$this->rollbackMigrations();
$this->callLifecycleHooks($this->_onUninstall);
}

public function getDebugInfo(): array
{
if (!$this->_debugInfoProvider) {
return [];
}

/** @var \NamelessMC\Framework\Debugging\DebugInfoProvider */
$provider = Container::getInstance()->make($this->_debugInfoProvider);

return $provider->provide();
}

public function frontendViewsPath(): string
{
return ROOT_PATH . '/vendor/' . $this->_packageName . '/views';
}

public function hasFrontendViews(): bool
{
return file_exists($this->frontendViewsPath());
}

public function panelViewsPath(): string
{
return ROOT_PATH . '/vendor/' . $this->_packageName . '/panel_views';
}

public function hasPanelViews(): bool
{
return file_exists($this->panelViewsPath());
}

private function runMigrations(): void
{
if (!$this->hasMigrations()) {
return;
}

PhinxAdapter::migrate($this->getPrivateName(), $this->migrationsPath());
}

private function rollbackMigrations(): void
{
if (!$this->hasMigrations()) {
return;
}

PhinxAdapter::rollback($this->getPrivateName(), $this->migrationsPath());
}

private function migrationsPath(): string
{
return ROOT_PATH . '/vendor/' . $this->_packageName . '/migrations';
}

private function hasMigrations(): bool
{
return file_exists($this->migrationsPath());
}

private function callLifecycleHooks(array $hooks): void
{
foreach ($hooks as $callback) {
/** @var \NamelessMC\Framework\ModuleLifecycle\Hook $hook */
$hook = Container::getInstance()->make($callback);
$hook->execute();
}
}
}
55 changes: 33 additions & 22 deletions core/classes/Core/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Navigation
*/
private bool $_panel;

private array $_preloaded_dropdowns = [];

public function __construct(bool $panel = false)
{
$this->_panel = $panel;
Expand All @@ -49,19 +51,6 @@ public function add(
float $order = 10,
?string $icon = ''
): void {
if ($this->_panel && $location == 'top') {
// Discard order
// TODO: only a temporary solution to the link conflict issue in the StaffCP
if (count($this->_topNavbar)) {
$key = array_keys($this->_topNavbar)[count($this->_topNavbar) - 1];
$previous_order = $this->_topNavbar[$key]['order'];
} else {
$previous_order = 0;
}

$order = $previous_order + 1;
}

// Add the link to the navigation
if ($location === 'top') {
// Add to top navbar
Expand Down Expand Up @@ -143,15 +132,28 @@ public function addDropdown(string $name, string $title, string $location = 'top
public function addItemToDropdown(string $dropdown, string $name, string $title, string $link, string $location = 'top', string $target = null, string $icon = '', int $order = 10): void
{
// Add the item
if ($location == 'top' && isset($this->_topNavbar[$dropdown])) {
// Navbar
$this->_topNavbar[$dropdown]['items'][$name] = [
'title' => $title,
'link' => $link,
'target' => $target,
'icon' => $icon,
'order' => $order,
];
if ($location == 'top') {
if (isset($this->_topNavbar[$dropdown])) {
// Navbar
$this->_topNavbar[$dropdown]['items'][$name] = [
'title' => $title,
'link' => $link,
'target' => $target,
'icon' => $icon,
'order' => $order,
];
} else {
// Dropdown not found
if (!isset($this->_preloaded_dropdowns[$dropdown])) {
$this->_preloaded_dropdowns[$dropdown]['items'][$name] = [
'title' => $title,
'link' => $link,
'items' => [],
'icon' => $icon,
'order' => $order,
];
}
}
} elseif (isset($this->_footerNav[$dropdown])) {
// Footer
$this->_footerNav[$dropdown]['items'][$name] = [
Expand All @@ -172,6 +174,15 @@ public function addItemToDropdown(string $dropdown, string $name, string $title,
*/
public function returnNav(string $location = 'top'): array
{
// merge preloaded dropdowns
foreach ($this->_preloaded_dropdowns as $key => $dropdown) {
if ($location == 'top') {
if (isset($this->_topNavbar[$key])) {
$this->_topNavbar[$key]['items'] = array_merge($this->_topNavbar[$key]['items'], $dropdown['items']);
}
}
}

$return = []; // String to return
if ($location == 'top') {
if (count($this->_topNavbar)) {
Expand Down
Loading
Loading