Consolidate handlePageAction in plugin class, remove functions.php from plugin

This commit is contained in:
billz 2024-11-07 00:44:06 -08:00
parent 7d4e6c1eaa
commit 04a6d007fd
2 changed files with 9 additions and 31 deletions

View File

@ -1033,10 +1033,3 @@ function renderStatus($hostapd_led, $hostapd_status, $memused_led, $memused, $cp
<?php <?php
} }
// Returns a classname without its associated namespace
function getClassName($class)
{
if ($pos = strrpos($class, '\\')) return substr($class, $pos + 1);
return $pos;
}

View File

@ -5,6 +5,7 @@
* *
* @description Architecture to support user plugins for RaspAP * @description Architecture to support user plugins for RaspAP
* @author Bill Zimmerman <billzimmerman@gmail.com> * @author Bill Zimmerman <billzimmerman@gmail.com>
* Special thanks to GitHub user @assachs
* @license https://github.com/raspap/raspap-webgui/blob/master/LICENSE * @license https://github.com/raspap/raspap-webgui/blob/master/LICENSE
*/ */
@ -91,39 +92,23 @@ class PluginManager {
} }
/** /**
* Iterate over each registered plugin and calls its associated method * Iterates over registered plugins and calls its associated method
* @param string $page * @param string $page
* @return boolean
*/ */
public function handlePageAction(string $page) { public function handlePageAction(string $page): bool {
foreach ($this->getInstalledPlugins() as $pluginClass) { foreach ($this->getInstalledPlugins() as $pluginClass) {
$pluginName = (new \ReflectionClass($pluginClass))->getShortName(); $plugin = new $pluginClass($this->pluginPath, $pluginClass);
$plugin = new $pluginClass($this->pluginPath, $pluginName);
if ($plugin instanceof PluginInterface) { if ($plugin instanceof PluginInterface) {
// check if the page matches this plugin's action if ($plugin->handlePageAction($page, $this)) {
if (strpos($page, "/plugin__{$plugin->getName()}") === 0) { return true;
$functions = "{$this->pluginPath}/{$plugin->getName()}/functions.php"; } else {
return false;
if (file_exists($functions)) {
require_once $functions;
// define the namespaced function
$function = '\\' . $plugin->getName() . '\\handlePageAction';
// call the function if it exists, passing the page and PluginManager instance
if (function_exists($function)) {
$function($page, $this, $pluginName);
return true;
}
} else {
throw new \Exception("Functions file not found for plugin: {$plugin->getName()}");
}
} }
} }
} }
} }
// Returns all installed plugins with full class names // Returns all installed plugins with full class names
public function getInstalledPlugins(): array { public function getInstalledPlugins(): array {
$plugins = []; $plugins = [];