Files
opensourcepos/app/Libraries/Plugins/BasePlugin.php
Ollama 896ed87797 fix: Address CodeRabbit AI review comments
- Move plugin discovery to pre_system in Events.php (allows events to be registered before they fire)
- Add plugin existence check in disablePlugin()
- Add is_subclass_of check before instantiating plugin classes
- Fix str_replace prefix removal in getPluginSettings using str_starts_with + substr
- Add down() migration to drop table on rollback
- Fix saveSettings to JSON-encode arrays/objects
- Update README to use MailchimpPlugin as reference implementation
- Remove CasposPlugin examples from documentation
2026-03-22 19:47:09 +00:00

70 lines
1.8 KiB
PHP

<?php
namespace App\Libraries\Plugins;
use App\Models\PluginConfig;
abstract class BasePlugin implements PluginInterface
{
protected PluginConfig $configModel;
public function __construct()
{
$this->configModel = new PluginConfig();
}
public function install(): bool
{
return true;
}
public function uninstall(): bool
{
return true;
}
public function isEnabled(): bool
{
$enabled = $this->configModel->get("{$this->getPluginId()}_enabled");
return $enabled === '1' || $enabled === 'true';
}
protected function getSetting(string $key, mixed $default = null): mixed
{
$value = $this->configModel->get("{$this->getPluginId()}_{$key}");
return $value ?? $default;
}
protected function setSetting(string $key, mixed $value): bool
{
$stringValue = is_array($value) || is_object($value)
? json_encode($value)
: (string)$value;
return $this->configModel->set("{$this->getPluginId()}_{$key}", $stringValue);
}
public function getSettings(): array
{
return $this->configModel->getPluginSettings($this->getPluginId());
}
public function saveSettings(array $settings): bool
{
$prefixedSettings = [];
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;
}
}
return $this->configModel->batchSave($prefixedSettings);
}
protected function log(string $level, string $message): void
{
log_message($level, "[Plugin:{$this->getPluginName()}] {$message}");
}
}