Skip to content

Commit a486b04

Browse files
lippserdAl2Klimov
authored andcommitted
Add getModule() to HookEssentials
Ported from `Hook\Common\HookUtils` in Icinga DB Web, generalized to return null when the hook is not registered under a module namespace.
1 parent e904e3e commit a486b04

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

library/Icinga/Application/Hook/HookEssentials.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
namespace Icinga\Application\Hook;
77

8+
use Icinga\Application\ClassLoader;
89
use Icinga\Application\Hook;
10+
use Icinga\Application\Icinga;
11+
use Icinga\Application\Modules\Module;
912

1013
/**
1114
* Provides the mechanism for hook registration and retrieval
@@ -49,6 +52,24 @@ public static function first(): ?static
4952
return Hook::first(static::getHookName());
5053
}
5154

55+
/**
56+
* Get the module this hook belongs to, if any
57+
*
58+
* Returns null when the hook class is not part of a module namespace.
59+
*
60+
* @return ?Module
61+
*/
62+
public function getModule(): ?Module
63+
{
64+
if (! ClassLoader::classBelongsToModule(static::class)) {
65+
return null;
66+
}
67+
68+
$moduleName = ClassLoader::extractModuleName(static::class);
69+
70+
return Icinga::app()->getModuleManager()->getModule($moduleName);
71+
}
72+
5273
/**
5374
* Register the hook
5475
*

0 commit comments

Comments
 (0)