diff options
author | Simon Rettberg | 2017-03-02 16:00:32 +0100 |
---|---|---|
committer | Simon Rettberg | 2017-03-02 16:00:32 +0100 |
commit | 627d18ede199339ad3a5bc8b85c0cc71616a6868 (patch) | |
tree | 01a848080084495ee914844d78ac9b3959f19642 | |
parent | Merge branch 'statistics_reporting' (diff) | |
download | slx-admin-627d18ede199339ad3a5bc8b85c0cc71616a6868.tar.gz slx-admin-627d18ede199339ad3a5bc8b85c0cc71616a6868.tar.xz slx-admin-627d18ede199339ad3a5bc8b85c0cc71616a6868.zip |
[inc/Hook] New helper for loading hooks
-rw-r--r-- | apis/cron.inc.php | 20 | ||||
-rw-r--r-- | inc/hook.inc.php | 43 | ||||
-rw-r--r-- | modules-available/main/page.inc.php | 7 |
3 files changed, 52 insertions, 18 deletions
diff --git a/apis/cron.inc.php b/apis/cron.inc.php index 30d6d452..a0042e61 100644 --- a/apis/cron.inc.php +++ b/apis/cron.inc.php @@ -32,14 +32,9 @@ function handleModule($file) include_once $file; } -foreach (glob('modules/*/hooks/cron.inc.php', GLOB_NOSORT) as $file) { - preg_match('#^modules/([^/]+)/#', $file, $out); - $mod = Module::get($out[1]); - if ($mod === false) - continue; - $id = $mod->getIdentifier(); +foreach (Hook::load('cron') as $hook) { // Check if job is still running, or should be considered crashed - $status = getJobStatus($id); + $status = getJobStatus($hook->moduleId); if ($status !== false) { $runtime = (time() - $status['start']); if ($runtime < 0) { @@ -51,13 +46,12 @@ foreach (glob('modules/*/hooks/cron.inc.php', GLOB_NOSORT) as $file) { } else { // Consider job crashed Property::removeFromList(CRON_KEY_STATUS, $status['string']); - EventLog::failure('Cronjob for module ' . $id . ' seems to be stuck or has crashed. Check the php or web server error log.'); + EventLog::failure('Cronjob for module ' . $hook->moduleId . ' seems to be stuck or has crashed. Check the php or web server error log.'); continue; } } - $now = time(); - Property::addToList(CRON_KEY_STATUS, "$id|$now", 1800); - $mod->activate(); - handleModule($file); - Property::removeFromList(CRON_KEY_STATUS, "$id|$now"); + $value = $hook . '|' . time(); + Property::addToList(CRON_KEY_STATUS, $value, 1800); + handleModule($hook->file); + Property::removeFromList(CRON_KEY_STATUS, $value); } diff --git a/inc/hook.inc.php b/inc/hook.inc.php new file mode 100644 index 00000000..bed81aeb --- /dev/null +++ b/inc/hook.inc.php @@ -0,0 +1,43 @@ +<?php + +/** + * Generic helper for getting and executing hooks. + */ +class Hook +{ + + /** + * Get list of all known and enabled modules for given hook. + * Internally, this scans for "modules/<*>/hooks/$hookName.inc.php" + * and optionally checks if the module's dependencies are fulfilled, + * then returns a list of all matching modules. + * @param string $hookName Name of hook to search for. + * @param bool $filterBroken if true, modules that have a hook but have missing deps will not be returned + * @return \Hook[] list of modules with requested hooks + */ + public static function load($hookName, $filterBroken = true) + { + $retval = array(); + foreach (glob('modules/*/hooks/' . $hookName . '.inc.php', GLOB_NOSORT) as $file) { + preg_match('#^modules/([^/]+)/#', $file, $out); + if ($filterBroken && !Module::isAvailable($out[1])) + continue; + $retval[] = new Hook($out[1], $file); + } + return $retval; + } + + /* + * + */ + + public $moduleId; + public $file; + + private function __construct($module, $hookFile) + { + $this->moduleId = $module; + $this->file = $hookFile; + } + +} diff --git a/modules-available/main/page.inc.php b/modules-available/main/page.inc.php index bd50a5d1..08e8b5a6 100644 --- a/modules-available/main/page.inc.php +++ b/modules-available/main/page.inc.php @@ -24,11 +24,8 @@ class Page_Main extends Page // Warnings $needSetup = false; - foreach (glob('modules/*/hooks/main-warning.inc.php') as $file) { - preg_match('#^modules/([^/]+)/#', $file, $out); - if (!Module::isAvailable($out[1])) - continue; - include $file; + foreach (Hook::load('main-warning') as $hook) { + include $hook->file; } // Update warning state |