From d10a3a96e0538b8347472d6c2d350dc2bee86501 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Sep 2019 15:25:32 +0200 Subject: [serversetup-bwlp-ipxe] --- inc/hook.inc.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'inc/hook.inc.php') diff --git a/inc/hook.inc.php b/inc/hook.inc.php index bed81aeb..05078f72 100644 --- a/inc/hook.inc.php +++ b/inc/hook.inc.php @@ -27,6 +27,26 @@ class Hook return $retval; } + /** + * Load given hook for a specific module only. + * + * @param string $moduleName Module + * @param string $hookName Hook + * @param bool $filterBroken return false if the module has missing deps + * @return Hook|false hook instance, false on error or if module doesn't have given hook + */ + public static function loadSingle($moduleName, $hookName, $filterBroken = true) + { + if (Module::get($moduleName) === false) // No such module + return false; + if ($filterBroken && !Module::isAvailable($moduleName)) // Broken + return false; + $file = 'modules/' . $moduleName . '/hooks/' . $hookName . '.inc.php'; + if (!file_exists($file)) // No hook + return false; + return new Hook($moduleName, $file); + } + /* * */ @@ -40,4 +60,21 @@ class Hook $this->file = $hookFile; } + /** + * Run the hook's code. The include is expected to return a + * value, which will in turn be the return value of this + * method. + * + * @return mixed The return value of the include file, or false on error + */ + public function run() + { + try { + return (include $this->file); + } catch (Exception $e) { + error_log($e); + return false; + } + } + } -- cgit v1.2.3-55-g7522