Remove myaac_hooks table, load info directly from .json files and cache them

This commit is contained in:
slawkens 2020-01-13 20:19:34 +01:00
parent aaaba5cc84
commit e08557e5ae
7 changed files with 62 additions and 54 deletions

View File

@ -27,7 +27,7 @@ session_start();
define('MYAAC', true);
define('MYAAC_VERSION', '0.8-dev');
define('DATABASE_VERSION', 27);
define('DATABASE_VERSION', 28);
define('TABLE_PREFIX', 'myaac_');
define('START_TIME', microtime(true));
define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX'));

View File

@ -120,17 +120,6 @@ CREATE TABLE `myaac_forum`
KEY `section` (`section`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
CREATE TABLE `myaac_hooks`
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL DEFAULT '',
`type` INT(2) NOT NULL DEFAULT 0,
`file` VARCHAR(100) NOT NULL,
`ordering` INT(11) NOT NULL DEFAULT 0,
`enabled` INT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
CREATE TABLE `myaac_menu`
(
`id` INT(11) NOT NULL AUTO_INCREMENT,

View File

@ -1,4 +1,5 @@
{
"enabled": 0,
"name": "Example Plugin",
"description": "This is just an example of a Plugin for MyAAC.",
"version": "1.0",

View File

@ -27,6 +27,7 @@ define('HOOK_LOGOUT', 15);
define('HOOK_FIRST', HOOK_STARTUP);
define('HOOK_LAST', HOOK_LOGOUT);
require_once LIBS . 'plugins.php';
class Hook
{
private $_name, $_type, $_file;
@ -90,27 +91,8 @@ class Hooks
public function load()
{
global $db;
$cache = Cache::getInstance();
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('hooks', $tmp)) {
$hooks = unserialize($tmp);
}
}
if (!isset($hooks)) {
$hooks = $db->query('SELECT `name`, `type`, `file` FROM `' . TABLE_PREFIX . 'hooks` WHERE `enabled` = 1 ORDER BY `ordering`;')->fetchAll();
if ($cache->enabled()) {
$cache->set('hooks', serialize($hooks), 600);
}
}
foreach($hooks as $hook) {
foreach(Plugins::getHooks() as $hook) {
$this->register($hook['name'], $hook['type'], $hook['file']);
}
}
}
?>

View File

@ -73,6 +73,51 @@ class Plugins {
private static $error = null;
private static $plugin = array();
public static function getHooks()
{
$cache = Cache::getInstance();
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('hooks', $tmp)) {
return unserialize($tmp);
}
}
$hooks = [];
foreach(get_plugins() as $filename) {
$string = file_get_contents(PLUGINS . $filename . '.json');
$string = self::removeComments($string);
$plugin = json_decode($string, true);
self::$plugin = $plugin;
if ($plugin == null) {
self::$warnings[] = 'Cannot load ' . $filename . '.json. File might be not a valid json code.';
continue;
}
if(isset($plugin['enabled']) && $plugin['enabled'] === 0) {
self::$warnings[] = 'Skipping ' . $filename . '... The plugin is disabled.';
continue;
}
if (isset($plugin['hooks'])) {
foreach ($plugin['hooks'] as $_name => $info) {
if (defined('HOOK_'. $info['type'])) {
$hook = constant('HOOK_'. $info['type']);
$hooks[] = ['name' => $_name, 'type' => $hook, 'file' => $info['file']];
} else {
self::$warnings[] = 'Plugin: ' . $filename . '. Unknown event type: ' . $info['type'];
}
}
}
}
if ($cache->enabled()) {
$cache->set('hooks', serialize($hooks), 600);
}
return $hooks;
}
public static function install($file) {
global $db;
@ -97,7 +142,7 @@ class Plugins {
}
else {
$string = file_get_contents($file_name);
$string = Plugins::removeComments($string);
$string = self::removeComments($string);
$plugin = json_decode($string, true);
self::$plugin = $plugin;
if ($plugin == null) {
@ -243,22 +288,6 @@ class Plugins {
self::$warnings[] = 'Cannot load install script. Your plugin might be not working correctly.';
}
if (isset($plugin['hooks'])) {
foreach ($plugin['hooks'] as $_name => $info) {
if (defined('HOOK_'. $info['type'])) {
$hook = constant('HOOK_'. $info['type']);
$query = $db->query('SELECT `id` FROM `' . TABLE_PREFIX . 'hooks` WHERE `name` = ' . $db->quote($_name) . ';');
if ($query->rowCount() == 1) { // found something
$query = $query->fetch();
$db->update(TABLE_PREFIX . 'hooks', array('type' => $hook, 'file' => $info['file']), array('id' => (int)$query['id']));
} else {
$db->insert(TABLE_PREFIX . 'hooks', array('id' => null, 'name' => $_name, 'type' => $hook, 'file' => $info['file']));
}
} else
self::$warnings[] = 'Unknown event type: ' . $info['type'];
}
}
$cache = Cache::getInstance();
if($cache->enabled()) {
$cache->delete('templates');

8
system/migrations/28.php Normal file
View File

@ -0,0 +1,8 @@
<?php
$db->exec('DROP TABLE IF EXISTS `' . TABLE_PREFIX . 'hooks`;');
$cache = Cache::getInstance();
if($cache->enabled()) {
$cache->delete('hooks');
}

View File

@ -10,7 +10,7 @@
defined('MYAAC') or die('Direct access not allowed!');
$title = 'Plugin manager';
require LIBS . 'plugins.php';
require_once LIBS . 'plugins.php';
$twig->display('admin.plugins.form.html.twig');
@ -111,4 +111,3 @@ foreach (get_plugins() as $plugin) {
$twig->display('admin.plugins.html.twig', array(
'plugins' => $plugins
));
?>