feat(extensions): Add extension enable/disable toggle and loading logic
The settings view now renders a toggle switch for each extension to enable or disable it. The `Plugi` class includes a new method to load extensions based on their enabled state. Tests verify the activation logic and settings persistence.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
use Plugi\Core\DB;
|
||||
use Plugi\Extensions;
|
||||
use Plugi\Plugi;
|
||||
use Plugi\Setting;
|
||||
|
||||
chdir(dirname(__DIR__, 2));
|
||||
|
||||
require_once __DIR__ . '/../src/Extensions.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingContract.php';
|
||||
require_once __DIR__ . '/../src/Contracts/SettingRepositoryContract.php';
|
||||
require_once __DIR__ . '/../src/Setting.php';
|
||||
require_once __DIR__ . '/../src/Core/DB.php';
|
||||
require_once __DIR__ . '/../src/Repositories/BaseRepository.php';
|
||||
require_once __DIR__ . '/../src/Repositories/SettingRepository.php';
|
||||
require_once __DIR__ . '/../src/Plugi.php';
|
||||
|
||||
function phpb_config(string $key)
|
||||
{
|
||||
$value = $GLOBALS['phpb_config'];
|
||||
foreach (explode('.', $key) as $part) {
|
||||
if (! is_array($value) || ! array_key_exists($part, $value)) {
|
||||
return null;
|
||||
}
|
||||
$value = $value[$part];
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function phpb_instance(string $name, array $params = []): ?string
|
||||
{
|
||||
return $name === 'setting' ? Setting::class : null;
|
||||
}
|
||||
|
||||
function phpb_current_language(): string
|
||||
{
|
||||
return 'en';
|
||||
}
|
||||
|
||||
function phpb_remove_non_alpha_numeric(string $value): string
|
||||
{
|
||||
return preg_replace('/[^a-zA-Z0-9]/', '', $value);
|
||||
}
|
||||
|
||||
class ExtensionActivationTestDatabase extends DB
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->pdo = new PDO('sqlite::memory:');
|
||||
$this->pdo->exec('CREATE TABLE settings (id INTEGER PRIMARY KEY AUTOINCREMENT, setting TEXT UNIQUE, value TEXT, is_array INTEGER)');
|
||||
$this->databaseName = 'extension_activation_test';
|
||||
}
|
||||
|
||||
public function setActivation(?string $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
$statement = $this->pdo->prepare('DELETE FROM settings WHERE setting = ?');
|
||||
$statement->execute(['extensions--form--enabled']);
|
||||
return;
|
||||
}
|
||||
|
||||
$statement = $this->pdo->prepare('INSERT OR REPLACE INTO settings (setting, value, is_array) VALUES (?, ?, 0)');
|
||||
$statement->execute(['extensions--form--enabled', $value]);
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionActivationTestPlugi extends Plugi
|
||||
{
|
||||
public function setDatabaseConnection()
|
||||
{
|
||||
global $phpb_db;
|
||||
$phpb_db = $GLOBALS['extensionActivationTestDatabase'];
|
||||
}
|
||||
|
||||
public function checkDatabaseDefinition()
|
||||
{
|
||||
}
|
||||
|
||||
public function loadTranslations($language)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
$database = new ExtensionActivationTestDatabase();
|
||||
$GLOBALS['extensionActivationTestDatabase'] = $database;
|
||||
$_ENV['DATABASE_PREFIX'] = '';
|
||||
$settingValues = new ReflectionProperty(Setting::class, 'settings');
|
||||
$registryDefaults = [
|
||||
'settings' => [],
|
||||
'configs' => [],
|
||||
'backends' => [],
|
||||
'routes' => [],
|
||||
'blocks' => [],
|
||||
'layouts' => [],
|
||||
'assets' => ['header' => [], 'footer' => [], 'admin-header' => [], 'admin-footer' => []],
|
||||
'htmlBlocks' => ['head' => [], 'body-start' => [], 'body-end' => [], 'admin-head' => [], 'admin-body-start' => [], 'admin-body-end' => []],
|
||||
];
|
||||
$config = [
|
||||
'general' => ['language' => 'en'],
|
||||
'storage' => ['use_database' => true],
|
||||
'auth' => ['use_login' => false],
|
||||
'website_manager' => ['use_website_manager' => false],
|
||||
'theme' => ['folder' => dirname(__DIR__, 2) . '/themes', 'active_theme' => 'default'],
|
||||
];
|
||||
|
||||
$boot = static function (?string $activationState) use ($database, $registryDefaults, $settingValues, $config): void {
|
||||
$database->setActivation($activationState);
|
||||
$settingValues->setValue(null, null);
|
||||
foreach ($registryDefaults as $property => $value) {
|
||||
(new ReflectionProperty(Extensions::class, $property))->setValue(null, $value);
|
||||
}
|
||||
new ExtensionActivationTestPlugi($config);
|
||||
};
|
||||
|
||||
$boot('1');
|
||||
if (! isset(Extensions::getRoutes()['/ext--form/submit'])
|
||||
|| ! isset(Extensions::getBackends()['ext--form'])
|
||||
|| ! isset(Extensions::getBlocks()['form-wrapper'])) {
|
||||
throw new RuntimeException('A persisted enabled state should register the extension during bootstrap.');
|
||||
}
|
||||
if (! str_contains(Extensions::getHeaderAssets(), '/extensions/plugi-ext-form/assets/dist.css')
|
||||
|| ! str_contains(Extensions::getFooterAssets(), '/extensions/plugi-ext-form/assets/dist.js')) {
|
||||
throw new RuntimeException('A persisted enabled state should register config-declared assets during bootstrap.');
|
||||
}
|
||||
|
||||
$boot('0');
|
||||
if (Extensions::getRoutes() !== [] || Extensions::getBackends() !== [] || Extensions::getBlocks() !== []) {
|
||||
throw new RuntimeException('A persisted disabled state should not register runtime features during bootstrap.');
|
||||
}
|
||||
if (Extensions::getHeaderAssets() !== '' || Extensions::getFooterAssets() !== '' || Extensions::getHtmlBlocks('head') !== '') {
|
||||
throw new RuntimeException('A persisted disabled state should not register assets or HTML hooks during bootstrap.');
|
||||
}
|
||||
$configs = Extensions::getConfigs();
|
||||
if (! isset($configs['form']['settings'][0]['name']) || $configs['form']['settings'][0]['name'] !== 'email') {
|
||||
throw new RuntimeException('Disabled extensions should retain their settings metadata.');
|
||||
}
|
||||
$db = (new ReflectionClass(DB::class))->newInstanceWithoutConstructor();
|
||||
if (! isset($db->getDBDefinition()['ext__form_entries'])) {
|
||||
throw new RuntimeException('Disabled extensions should retain their schema definitions.');
|
||||
}
|
||||
|
||||
$boot(null);
|
||||
if (Extensions::getRoutes() !== [] || Extensions::getBackends() !== [] || Extensions::getBlocks() !== []) {
|
||||
throw new RuntimeException('An absent activation state should remain disabled during bootstrap.');
|
||||
}
|
||||
|
||||
fwrite(STDOUT, "PASS: bootstrap loads only extensions with a persisted enabled state.\n");
|
||||
Reference in New Issue
Block a user