Add possibility to let hooks always run

refs #3772
This commit is contained in:
Loei Petrus Marogi 2019-07-11 13:32:45 +02:00
parent c28155408f
commit 617f1b35b3
2 changed files with 17 additions and 13 deletions

View File

@ -76,7 +76,8 @@ class Hook
} }
foreach (self::$hooks[$name] as $hook) { foreach (self::$hooks[$name] as $hook) {
if (self::hasPermission($hook)) { list($class, $alwaysRun) = $hook;
if ($alwaysRun || self::hasPermission($class)) {
return true; return true;
} }
} }
@ -120,7 +121,7 @@ class Hook
return self::$instances[$name][$key]; return self::$instances[$name][$key];
} }
$class = self::$hooks[$name][$key]; $class = self::$hooks[$name][$key][0];
if (! class_exists($class)) { if (! class_exists($class)) {
throw new ProgrammingError( throw new ProgrammingError(
@ -273,7 +274,8 @@ class Hook
} }
foreach (self::$hooks[$name] as $key => $hook) { foreach (self::$hooks[$name] as $key => $hook) {
if (self::hasPermission($hook)) { list($class, $alwaysRun) = $hook;
if ($alwaysRun || self::hasPermission($class)) {
if (self::createInstance($name, $key) === null) { if (self::createInstance($name, $key) === null) {
return array(); return array();
} }
@ -296,7 +298,8 @@ class Hook
if (self::has($name)) { if (self::has($name)) {
foreach (self::$hooks[$name] as $key => $hook) { foreach (self::$hooks[$name] as $key => $hook) {
if (self::hasPermission($hook)) { list($class, $alwaysRun) = $hook;
if ($alwaysRun || self::hasPermission($class)) {
return self::createInstance($name, $key); return self::createInstance($name, $key);
} }
} }
@ -306,12 +309,13 @@ class Hook
/** /**
* Register a class * Register a class
* *
* @param string $name One of the predefined hook names * @param string $name One of the predefined hook names
* @param string $key The identifier of a specific subtype * @param string $key The identifier of a specific subtype
* @param string $class Your class name, must inherit one of the * @param string $class Your class name, must inherit one of the
* classes in the Icinga/Application/Hook folder * classes in the Icinga/Application/Hook folder
* @param bool $alwaysRun To run the hook always (e.g. without permission check)
*/ */
public static function register($name, $key, $class) public static function register($name, $key, $class, $alwaysRun = false)
{ {
$name = self::normalizeHookName($name); $name = self::normalizeHookName($name);
@ -321,6 +325,6 @@ class Hook
$class = ltrim($class, ClassLoader::NAMESPACE_SEPARATOR); $class = ltrim($class, ClassLoader::NAMESPACE_SEPARATOR);
self::$hooks[$name][$key] = $class; self::$hooks[$name][$key] = [$class, $alwaysRun];
} }
} }

View File

@ -1278,11 +1278,11 @@ class Module
* @param string $implementation Fully qualified name of the class providing the hook implementation. * @param string $implementation Fully qualified name of the class providing the hook implementation.
* Defaults to the module's ProvidedHook namespace plus the hook's name for the * Defaults to the module's ProvidedHook namespace plus the hook's name for the
* class name * class name
* @param string $deprecated DEPRECATED - No-op arg for compatibility reasons * @param bool $alwaysRun To run the hook always (e.g. without permission check)
* *
* @return $this * @return $this
*/ */
protected function provideHook($name, $implementation = null, $deprecated = null) protected function provideHook($name, $implementation = null, $alwaysRun = false)
{ {
if ($implementation === null) { if ($implementation === null) {
$implementation = $name; $implementation = $name;
@ -1296,7 +1296,7 @@ class Module
$class = $implementation; $class = $implementation;
} }
Hook::register($name, $class, $class); Hook::register($name, $class, $class, $alwaysRun);
return $this; return $this;
} }