mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-07-29 08:07:05 -04:00
Refactor manage plugin configuration and settings
- Add column to indicate control setting (installed, enabled). - Add column to indicate plugin. - Rework business logic to read the status properly. - Renamed the migration to properly reflect which version it's released in. Signed-off-by: objec <objecttothis@gmail.com>
This commit is contained in:
@@ -10,7 +10,7 @@ class PluginConfigTableCreate extends Migration
|
||||
{
|
||||
log_message('info', 'Migrating plugin_config table started');
|
||||
|
||||
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.1_PluginConfigTableCreate.sql');
|
||||
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.5.0_PluginConfigTableCreate.sql');
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
@@ -1,9 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS `ospos_plugin_config` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`plugin_id` varchar(100) NOT NULL,
|
||||
`key` varchar(100) NOT NULL,
|
||||
`value` text NOT NULL,
|
||||
`is_control` tinyint(1) NOT NULL DEFAULT 0,
|
||||
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
|
||||
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`key`)
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uq_plugin_key` (`plugin_id`, `key`),
|
||||
KEY `idx_plugin_id` (`plugin_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
INSERT IGNORE INTO `ospos_modules` (`name_lang_key`, `desc_lang_key`, `sort`, `module_id`) VALUES
|
||||
@@ -25,13 +25,13 @@ 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';
|
||||
}
|
||||
|
||||
protected function getSetting(string $key, mixed $default = null): mixed
|
||||
{
|
||||
$value = $this->configModel->getValue("{$this->getPluginId()}_{$key}");
|
||||
$value = $this->configModel->getValue($this->getPluginId(), $key);
|
||||
return $value ?? $default;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ abstract class BasePlugin implements PluginInterface
|
||||
? json_encode($value)
|
||||
: (string)$value;
|
||||
|
||||
return $this->configModel->setValue("{$this->getPluginId()}_{$key}", $stringValue);
|
||||
return $this->configModel->setValue($this->getPluginId(), $key, $stringValue);
|
||||
}
|
||||
|
||||
public function getSettings(): array
|
||||
@@ -51,16 +51,14 @@ abstract class BasePlugin implements PluginInterface
|
||||
|
||||
public function saveSettings(array $settings): bool
|
||||
{
|
||||
$prefixedSettings = [];
|
||||
$normalized = [];
|
||||
foreach ($settings as $key => $value) {
|
||||
if (is_array($value) || is_object($value)) {
|
||||
$prefixedSettings["{$this->getPluginId()}_{$key}"] = json_encode($value);
|
||||
} else {
|
||||
$prefixedSettings["{$this->getPluginId()}_{$key}"] = (string)$value;
|
||||
}
|
||||
$normalized[$key] = is_array($value) || is_object($value)
|
||||
? json_encode($value)
|
||||
: (string)$value;
|
||||
}
|
||||
|
||||
return $this->configModel->batchSave($prefixedSettings);
|
||||
return $this->configModel->batchSave($this->getPluginId(), $normalized);
|
||||
}
|
||||
|
||||
public function getConfigViewData(): array
|
||||
|
||||
@@ -110,7 +110,7 @@ class PluginManager
|
||||
|
||||
public function isPluginEnabled(string $pluginId): bool
|
||||
{
|
||||
$enabled = $this->configModel->getValue($this->getEnabledKey($pluginId));
|
||||
$enabled = $this->configModel->getValue($pluginId, 'enabled');
|
||||
return $enabled === '1' || $enabled === 'true';
|
||||
}
|
||||
|
||||
@@ -128,15 +128,15 @@ class PluginManager
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->configModel->exists($this->getInstalledKey($pluginId))) {
|
||||
if (!$this->configModel->exists($pluginId, 'installed')) {
|
||||
if (!$plugin->install()) {
|
||||
log_message('error', "Failed to install plugin: {$pluginId}");
|
||||
return false;
|
||||
}
|
||||
$this->configModel->setValue($this->getInstalledKey($pluginId), '1');
|
||||
$this->configModel->setValue($pluginId, 'installed', '1', true);
|
||||
}
|
||||
|
||||
$this->configModel->setValue($this->getEnabledKey($pluginId), '1');
|
||||
$this->configModel->setValue($pluginId, 'enabled', '1', true);
|
||||
|
||||
$this->registerNamespace($pluginId);
|
||||
|
||||
@@ -152,7 +152,7 @@ class PluginManager
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->configModel->setValue($this->getEnabledKey($pluginId), '0');
|
||||
$this->configModel->setValue($pluginId, 'enabled', '0', true);
|
||||
log_message('info', "Plugin disabled: {$pluginId}");
|
||||
|
||||
return true;
|
||||
@@ -173,19 +173,19 @@ class PluginManager
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->configModel->deleteAllStartingWith($pluginId . '_');
|
||||
$this->configModel->deleteAllForPlugin($pluginId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getSetting(string $pluginId, string $key, mixed $default = null): mixed
|
||||
{
|
||||
return $this->configModel->getValue("{$pluginId}_{$key}") ?? $default;
|
||||
return $this->configModel->getValue($pluginId, $key) ?? $default;
|
||||
}
|
||||
|
||||
public function setSetting(string $pluginId, string $key, mixed $value): bool
|
||||
{
|
||||
return $this->configModel->setValue("{$pluginId}_{$key}", $value);
|
||||
return $this->configModel->setValue($pluginId, $key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,14 +241,4 @@ class PluginManager
|
||||
log_message('debug', "Registered namespace for plugin dir: {$pluginDirName}");
|
||||
}
|
||||
}
|
||||
|
||||
private function getEnabledKey(string $pluginId): string
|
||||
{
|
||||
return "{$pluginId}__enabled";
|
||||
}
|
||||
|
||||
private function getInstalledKey(string $pluginId): string
|
||||
{
|
||||
return "{$pluginId}__installed";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,26 +7,28 @@ use CodeIgniter\Model;
|
||||
class PluginConfig extends Model
|
||||
{
|
||||
protected $table = 'plugin_config';
|
||||
protected $primaryKey = 'key';
|
||||
protected $useAutoIncrement = false;
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $useSoftDeletes = false;
|
||||
protected $allowedFields = [
|
||||
'plugin_id',
|
||||
'key',
|
||||
'value'
|
||||
'value',
|
||||
'is_control',
|
||||
];
|
||||
|
||||
public function exists(string $key): bool
|
||||
public function exists(string $pluginId, string $key): bool
|
||||
{
|
||||
$builder = $this->db->table('plugin_config');
|
||||
$builder->where('key', $key);
|
||||
$builder->where('plugin_id', $pluginId)->where('key', $key);
|
||||
|
||||
return ($builder->get()->getNumRows() === 1);
|
||||
}
|
||||
|
||||
public function getValue(string $key): ?string
|
||||
public function getValue(string $pluginId, string $key): ?string
|
||||
{
|
||||
$builder = $this->db->table('plugin_config');
|
||||
$query = $builder->getWhere(['key' => $key], 1);
|
||||
$query = $builder->getWhere(['plugin_id' => $pluginId, 'key' => $key], 1);
|
||||
|
||||
if ($query->getNumRows() === 1) {
|
||||
return $query->getRow()->value;
|
||||
@@ -35,55 +37,57 @@ class PluginConfig extends Model
|
||||
return null;
|
||||
}
|
||||
|
||||
public function setValue(string $key, string $value): bool
|
||||
public function setValue(string $pluginId, string $key, string $value, bool $isControl = false): bool
|
||||
{
|
||||
$builder = $this->db->table('plugin_config');
|
||||
|
||||
if ($this->exists($key)) {
|
||||
return $builder->update(['value' => $value], ['key' => $key]);
|
||||
if ($this->exists($pluginId, $key)) {
|
||||
return $builder->update(
|
||||
['value' => $value],
|
||||
['plugin_id' => $pluginId, 'key' => $key]
|
||||
);
|
||||
}
|
||||
|
||||
return $builder->insert(['key' => $key, 'value' => $value]);
|
||||
return $builder->insert([
|
||||
'plugin_id' => $pluginId,
|
||||
'key' => $key,
|
||||
'value' => $value,
|
||||
'is_control' => $isControl ? 1 : 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getPluginSettings(string $pluginId): array
|
||||
{
|
||||
$builder = $this->db->table('plugin_config');
|
||||
$builder->like('key', $pluginId . '_', 'after')
|
||||
->notLike('key', $pluginId . '__', 'after');
|
||||
$builder->where('plugin_id', $pluginId)->where('is_control', 0);
|
||||
$query = $builder->get();
|
||||
|
||||
$settings = [];
|
||||
$prefix = $pluginId . '_';
|
||||
foreach ($query->getResult() as $row) {
|
||||
$key = str_starts_with($row->key, $prefix)
|
||||
? substr($row->key, strlen($prefix))
|
||||
: $row->key;
|
||||
$settings[$key] = $row->value;
|
||||
$settings[$row->key] = $row->value;
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
public function deleteKey(string $key): bool
|
||||
public function deleteKey(string $pluginId, string $key): bool
|
||||
{
|
||||
$builder = $this->db->table('plugin_config');
|
||||
return $builder->delete(['key' => $key]);
|
||||
return $builder->delete(['plugin_id' => $pluginId, 'key' => $key]);
|
||||
}
|
||||
|
||||
public function deleteAllStartingWith(string $prefix): bool
|
||||
public function deleteAllForPlugin(string $pluginId): bool
|
||||
{
|
||||
$builder = $this->db->table('plugin_config');
|
||||
$builder->like('key', $prefix, 'after');
|
||||
return $builder->delete();
|
||||
return $builder->delete(['plugin_id' => $pluginId]);
|
||||
}
|
||||
|
||||
public function batchSave(array $data): bool
|
||||
public function batchSave(string $pluginId, array $data): bool
|
||||
{
|
||||
$this->db->transBegin();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (!$this->setValue($key, $value)) {
|
||||
if (!$this->setValue($pluginId, $key, $value)) {
|
||||
$this->db->transRollback();
|
||||
return false;
|
||||
}
|
||||
@@ -100,7 +104,7 @@ class PluginConfig extends Model
|
||||
|
||||
$configs = [];
|
||||
foreach ($query->getResult() as $row) {
|
||||
$configs[$row->key] = $row->value;
|
||||
$configs[$row->plugin_id][$row->key] = $row->value;
|
||||
}
|
||||
|
||||
return $configs;
|
||||
|
||||
Reference in New Issue
Block a user