Skip to content

Commit e904e3e

Browse files
lippserdAl2Klimov
authored andcommitted
Replace trait alias with isAlwaysRun() in HookEssentials
The `use HookEssentials { register as protected parentRegister; }` alias construct in `LoginButtonHook` and `RequestHook` is harder to read than a simple method override. A `protected static isAlwaysRun()` default on the trait lets subclasses declare their intent without redefining `register()`. A static property was not viable here: PHP forbids redefining a trait property in a using class with a different initial value.
1 parent ae082f8 commit e904e3e

3 files changed

Lines changed: 45 additions & 27 deletions

File tree

library/Icinga/Application/Hook/HookEssentials.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,26 @@ public static function first(): ?static
5252
/**
5353
* Register the hook
5454
*
55-
* @param bool $alwaysRun Whether to always run the hook, without permission check
55+
* @param ?bool $alwaysRun Whether to always run the hook without permission
56+
* check. Defaults to {@see isAlwaysRun()}.
5657
*/
57-
public static function register(bool $alwaysRun = false): void
58+
public static function register(?bool $alwaysRun = null): void
5859
{
59-
Hook::register(static::getHookName(), static::class, static::class, $alwaysRun);
60+
Hook::register(
61+
static::getHookName(),
62+
static::class,
63+
static::class,
64+
$alwaysRun ?? static::isAlwaysRun()
65+
);
66+
}
67+
68+
/**
69+
* Get whether the hook always runs without a permission check
70+
*
71+
* Override this in a hook base class to change the default.
72+
*/
73+
protected static function isAlwaysRun(): bool
74+
{
75+
return false;
6076
}
6177
}

library/Icinga/Application/Hook/LoginButtonHook.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@
1616
*/
1717
abstract class LoginButtonHook
1818
{
19-
use HookEssentials {
20-
register as protected parentRegister;
19+
use HookEssentials;
20+
21+
/**
22+
* Always runs without a permission check
23+
*
24+
* Login button hooks are intended to render on the login page, where
25+
* permissions are not applicable.
26+
*
27+
* @return bool
28+
*/
29+
protected static function isAlwaysRun(): bool
30+
{
31+
return true;
2132
}
2233

2334
final protected static function getHookName(): string
@@ -34,14 +45,4 @@ final protected static function getHookName(): string
3445
* @return LoginButton[]
3546
*/
3647
abstract public function getButtons(): array;
37-
38-
/**
39-
* Register a hook provider
40-
*
41-
* Always runs the hook, without permission check. Latter makes no sense on the login page.
42-
*/
43-
public static function register(): void
44-
{
45-
static::parentRegister(true);
46-
}
4748
}

library/Icinga/Application/Hook/RequestHook.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@
1111

1212
abstract class RequestHook
1313
{
14-
use HookEssentials {
15-
register as protected parentRegister;
14+
use HookEssentials;
15+
16+
/**
17+
* Always runs without a permission check
18+
*
19+
* Request hooks are system-level concerns that fire on every request as
20+
* part of the framework dispatch cycle, independent of user permissions.
21+
*
22+
* @return bool
23+
*/
24+
protected static function isAlwaysRun(): bool
25+
{
26+
return true;
1627
}
1728

1829
final protected static function getHookName(): string
@@ -46,14 +57,4 @@ final public static function postDispatch(Request $request): void
4657
}
4758
}
4859
}
49-
50-
/**
51-
* Register the class as a RequestHook implementation
52-
*
53-
* Call this method on your implementation during module initialization to make Icinga Web aware of your hook.
54-
*/
55-
public static function register(): void
56-
{
57-
static::parentRegister(true);
58-
}
5960
}

0 commit comments

Comments
 (0)