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
}
// 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
* @author Bill Zimmerman <billzimmerman@gmail.com>
* Special thanks to GitHub user @assachs
* @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
* @return boolean
*/
public function handlePageAction(string $page) {
public function handlePageAction(string $page): bool {
foreach ($this->getInstalledPlugins() as $pluginClass) {
$pluginName = (new \ReflectionClass($pluginClass))->getShortName();
$plugin = new $pluginClass($this->pluginPath, $pluginName);
$plugin = new $pluginClass($this->pluginPath, $pluginClass);
if ($plugin instanceof PluginInterface) {
// check if the page matches this plugin's action
if (strpos($page, "/plugin__{$plugin->getName()}") === 0) {
$functions = "{$this->pluginPath}/{$plugin->getName()}/functions.php";
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()}");
}
if ($plugin->handlePageAction($page, $this)) {
return true;
} else {
return false;
}
}
}
}
// Returns all installed plugins with full class names
public function getInstalledPlugins(): array {
$plugins = [];