mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-05-30 11:14:59 -04:00
Fix persistence problem with plugin registration.
- Move the PluginManager creation to a service. - Move plugin discovery to creation. - Create static discovery and namespaces variables in the PluginManager.php library - Refactor persistent namespace declarations - Refactor redundant code to private function. - Remove whitespace - Remove enable setting from MailchimpPlugin. That is handled by the PluginManager.php - Update Events.php to call the pluginManager service - Correct typo in enabled setting for BasePlugin to accurately reflect the database naming. Signed-off-by: objec <objecttothis@gmail.com>
This commit is contained in:
@@ -8,7 +8,6 @@ use CodeIgniter\HotReloader\HotReloader;
|
||||
use App\Events\Db_log;
|
||||
use App\Events\Load_config;
|
||||
use App\Events\Method;
|
||||
use App\Libraries\Plugins\PluginManager;
|
||||
|
||||
/*
|
||||
* --------------------------------------------------------------------
|
||||
@@ -57,14 +56,7 @@ Events::on('pre_system', static function (): void {
|
||||
});
|
||||
|
||||
Events::on('post_controller_constructor', static function (): void {
|
||||
$pluginManager = new PluginManager();
|
||||
|
||||
if ($pluginManager->canLoadPlugins()) {
|
||||
$pluginManager->discoverPlugins();
|
||||
$pluginManager->registerPluginEvents();
|
||||
} else {
|
||||
log_message('debug', 'Plugin loading is disabled until after migration has been run.');
|
||||
}
|
||||
service('pluginManager');
|
||||
}, 10);
|
||||
|
||||
$config = new Load_config();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Config;
|
||||
|
||||
use App\Libraries\MY_Language;
|
||||
use App\Libraries\Plugins\PluginManager;
|
||||
use Locale;
|
||||
use HTMLPurifier;
|
||||
use HTMLPurifier_Config;
|
||||
@@ -61,6 +62,24 @@ class Services extends BaseService
|
||||
return new MY_Language($locale);
|
||||
}
|
||||
|
||||
public static function pluginManager(bool $getShared = true): PluginManager
|
||||
{
|
||||
if ($getShared) {
|
||||
return static::getSharedInstance('pluginManager');
|
||||
}
|
||||
|
||||
$manager = new PluginManager();
|
||||
|
||||
if ($manager->canLoadPlugins()) {
|
||||
$manager->discoverPlugins();
|
||||
$manager->registerPluginEvents();
|
||||
} else {
|
||||
log_message('debug', 'PluginManager: skipping init, plugin_config table not found.');
|
||||
}
|
||||
|
||||
return $manager;
|
||||
}
|
||||
|
||||
private static HTMLPurifier $htmlPurifier;
|
||||
|
||||
public static function htmlPurifier($getShared = true): object
|
||||
|
||||
@@ -12,8 +12,7 @@ class Plugins extends Secure_Controller
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('plugins');
|
||||
$this->pluginManager = new PluginManager();
|
||||
$this->pluginManager->discoverPlugins();
|
||||
$this->pluginManager = service('pluginManager');
|
||||
}
|
||||
|
||||
public function getIndex(): string
|
||||
|
||||
@@ -25,7 +25,7 @@ abstract class BasePlugin implements PluginInterface
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
$enabled = $this->configModel->getValue("{$this->getPluginId()}_enabled");
|
||||
$enabled = $this->configModel->getValue("{$this->getPluginId()}__enabled");
|
||||
return $enabled === '1' || $enabled === 'true';
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ class PluginManager
|
||||
private PluginConfig $configModel;
|
||||
private string $pluginsPath;
|
||||
private bool $eventsRegistered = false;
|
||||
private static bool $discovered = false;
|
||||
private static array $registeredNamespaces = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -25,6 +27,11 @@ class PluginManager
|
||||
|
||||
public function discoverPlugins(): void
|
||||
{
|
||||
if (self::$discovered) {
|
||||
log_message('debug', 'Plugin discovery already completed, skipping');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_dir($this->pluginsPath)) {
|
||||
log_message('debug', 'Plugins directory does not exist: ' . $this->pluginsPath);
|
||||
return;
|
||||
@@ -59,15 +66,14 @@ class PluginManager
|
||||
$this->plugins[$plugin->getPluginId()] = $plugin;
|
||||
|
||||
if ($this->isPluginEnabled($plugin->getPluginId())) {
|
||||
$loader = Services::autoloader();
|
||||
$loader->addNamespace(
|
||||
"App\\Plugins\\{$plugin->getPluginId()}",
|
||||
APPPATH . "Plugins/{$plugin->getPluginId()}"
|
||||
);
|
||||
$this->registerNamespace($plugin->getPluginId());
|
||||
}
|
||||
|
||||
log_message('debug', "Discovered plugin: {$plugin->getPluginName()}");
|
||||
}
|
||||
|
||||
self::$discovered = true;
|
||||
log_message('debug', 'Plugin discovery completed');
|
||||
}
|
||||
|
||||
private function getClassNameFromFile(string $pathname): ?string
|
||||
@@ -141,11 +147,7 @@ class PluginManager
|
||||
|
||||
$this->configModel->setValue($this->getEnabledKey($pluginId), '1');
|
||||
|
||||
$loader = Services::autoloader();
|
||||
$loader->addNamespace(
|
||||
"App\\Plugins\\{$pluginId}",
|
||||
APPPATH . "Plugins/{$pluginId}"
|
||||
);
|
||||
$this->registerNamespace($pluginId);
|
||||
|
||||
log_message('info', "Plugin enabled: {$pluginId}");
|
||||
|
||||
@@ -193,6 +195,24 @@ class PluginManager
|
||||
return $this->configModel->setValue("{$pluginId}_{$key}", $value);
|
||||
}
|
||||
|
||||
public static function resetStatic(): void
|
||||
{
|
||||
self::$discovered = false;
|
||||
self::$registeredNamespaces = [];
|
||||
}
|
||||
|
||||
private function registerNamespace(string $pluginId): void
|
||||
{
|
||||
$namespace = "App\\Plugins\\{$pluginId}";
|
||||
|
||||
if (!in_array($namespace, self::$registeredNamespaces, true)) {
|
||||
$loader = Services::autoloader();
|
||||
$loader->addNamespace($namespace, APPPATH . "Plugins/{$pluginId}");
|
||||
self::$registeredNamespaces[] = $namespace;
|
||||
log_message('debug', "Registered namespace for plugin: {$pluginId}");
|
||||
}
|
||||
}
|
||||
|
||||
private function getEnabledKey(string $pluginId): string
|
||||
{
|
||||
return "{$pluginId}__enabled";
|
||||
|
||||
@@ -60,7 +60,6 @@ class MailchimpPlugin extends BasePlugin
|
||||
$this->setSetting('api_key', '');
|
||||
$this->setSetting('list_id', '');
|
||||
$this->setSetting('sync_on_save', '1');
|
||||
$this->setSetting('enabled', '0');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
<span class="glyphicon glyphicon-play"></span> <?= lang('Plugins.enable') ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
|
||||
|
||||
<?php if ($plugin['has_config'] && $plugin['enabled']): ?>
|
||||
<button class="btn btn-primary btn-xs plugin-config"
|
||||
data-plugin-id="<?= esc($pluginId) ?>">
|
||||
@@ -95,8 +95,8 @@ $(document).ready(function() {
|
||||
var btn = $(this);
|
||||
var action = btn.data('action');
|
||||
var pluginId = btn.data('plugin-id');
|
||||
|
||||
$.post('plugins/manage/' + action + '/' + pluginId, {
|
||||
|
||||
$.post('plugins/' + action + '/' + pluginId, {
|
||||
<?= esc(csrf_token()) ?>: '<?= esc(csrf_hash()) ?>'
|
||||
}, function(response) {
|
||||
if (response.success) {
|
||||
@@ -107,13 +107,13 @@ $(document).ready(function() {
|
||||
}
|
||||
}, 'json');
|
||||
});
|
||||
|
||||
|
||||
$('.plugin-config').on('click', function() {
|
||||
var pluginId = $(this).data('plugin-id');
|
||||
$('#plugin-config-content').load('plugins/manage/config/' + pluginId);
|
||||
$('#plugin-config-content').load('plugins/config/' + pluginId);
|
||||
$('#plugin-config-modal').modal('show');
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<?= view('partial/footer') ?>
|
||||
<?= view('partial/footer') ?>
|
||||
|
||||
Reference in New Issue
Block a user