84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
// Plugin System - manages global and page-level plugins
|
||
|
|
|
||
|
|
class PluginManager {
|
||
|
|
private array $loadedPlugins = [];
|
||
|
|
private array $globalPlugins = [];
|
||
|
|
|
||
|
|
public function loadGlobalPlugins(array $config): void {
|
||
|
|
if (!isset($config['plugins']['enabled'])) return;
|
||
|
|
|
||
|
|
$plugins = array_map('trim', explode(',', $config['plugins']['enabled']));
|
||
|
|
foreach ($plugins as $pluginName) {
|
||
|
|
if (!empty($pluginName)) {
|
||
|
|
$this->loadPlugin($pluginName, 'global');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function loadPagePlugins(?array $metadata): void {
|
||
|
|
if (!$metadata || !isset($metadata['plugins'])) return;
|
||
|
|
|
||
|
|
$plugins = array_map('trim', explode(',', $metadata['plugins']));
|
||
|
|
foreach ($plugins as $pluginName) {
|
||
|
|
if (!empty($pluginName)) {
|
||
|
|
$this->loadPlugin($pluginName, 'page');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function loadPlugin(string $pluginName, string $scope): bool {
|
||
|
|
if (isset($this->loadedPlugins[$pluginName])) return true;
|
||
|
|
|
||
|
|
$pluginName = basename($pluginName);
|
||
|
|
|
||
|
|
$customPluginPath = __DIR__ . "/../custom/plugins/{$scope}/{$pluginName}.php";
|
||
|
|
$appPluginPath = __DIR__ . "/plugins/{$scope}/{$pluginName}.php";
|
||
|
|
|
||
|
|
$pluginPath = file_exists($customPluginPath) ? $customPluginPath
|
||
|
|
: (file_exists($appPluginPath) ? $appPluginPath : null);
|
||
|
|
|
||
|
|
if (!$pluginPath) {
|
||
|
|
error_log("Plugin not found: {$pluginName} (scope: {$scope})");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
require_once $pluginPath;
|
||
|
|
|
||
|
|
$this->loadedPlugins[$pluginName] = [
|
||
|
|
'path' => $pluginPath,
|
||
|
|
'scope' => $scope,
|
||
|
|
'loaded_at' => microtime(true)
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($scope === 'global') {
|
||
|
|
$this->globalPlugins[] = $pluginName;
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getLoadedPlugins(): array {
|
||
|
|
return array_keys($this->loadedPlugins);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getGlobalPlugins(): array {
|
||
|
|
return $this->globalPlugins;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isLoaded(string $pluginName): bool {
|
||
|
|
return isset($this->loadedPlugins[$pluginName]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getPluginInfo(string $pluginName): ?array {
|
||
|
|
return $this->loadedPlugins[$pluginName] ?? null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$GLOBALS['pluginManager'] = new PluginManager();
|
||
|
|
|
||
|
|
function getPluginManager(): PluginManager {
|
||
|
|
return $GLOBALS['pluginManager'];
|
||
|
|
}
|