Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/Icinga/Application/ApplicationBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ public function hasLocales()
*/
protected function registerApplicationHooks(): self
{
Hook::register('DbMigration', DbMigration::class, DbMigration::class);
DbMigration::register();

return $this;
}
Expand Down
8 changes: 8 additions & 0 deletions library/Icinga/Application/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static function clean()
* @param string $name One of the predefined hook names
*
* @return bool
*
* @deprecated Use {@link Hook\HookEssentials::isRegistered} instead
*/
public static function has($name)
{
Expand Down Expand Up @@ -267,6 +269,8 @@ private static function assertValidHook($instance, $name)
* @param string $name One of the predefined hook names
*
* @return array
*
* @deprecated Use {@link Hook\HookEssentials::all} instead
*/
public static function all($name): array
{
Expand All @@ -291,6 +295,8 @@ public static function all($name): array
* @param string $name One of the predefined hook names
*
* @return null|mixed
*
* @deprecated Use {@link Hook\HookEssentials::first} instead
*/
public static function first($name)
{
Expand All @@ -314,6 +320,8 @@ public static function first($name)
* @param string $class Your class name, must inherit one of the
* classes in the Icinga/Application/Hook folder
* @param bool $alwaysRun To run the hook always (e.g. without permission check)
*
* @deprecated Use {@link Hook\HookEssentials::register} instead
*/
public static function register($name, $key, $class, $alwaysRun = false)
{
Expand Down
13 changes: 9 additions & 4 deletions library/Icinga/Application/Hook/ApplicationStateHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@

namespace Icinga\Application\Hook;

use Icinga\Application\Hook;
use Icinga\Application\Logger;

/**
* Application state hook base class
*/
abstract class ApplicationStateHook
{
use HookEssentials;

const ERROR = 'error';

private $messages = [];

final protected static function getHookName(): string
{
return 'ApplicationState';
}

final public function hasMessages()
{
return ! empty($this->messages);
Expand Down Expand Up @@ -66,12 +72,11 @@ final public static function getAllMessages()
{
$messages = [];

if (! Hook::has('ApplicationState')) {
if (! static::isRegistered()) {
return $messages;
}

foreach (Hook::all('ApplicationState') as $hook) {
/** @var self $hook */
foreach (static::all() as $hook) {
try {
$hook->collectMessages();
} catch (\Exception $e) {
Expand Down
13 changes: 9 additions & 4 deletions library/Icinga/Application/Hook/AuditHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
use Exception;
use InvalidArgumentException;
use Icinga\Authentication\Auth;
use Icinga\Application\Hook;
use Icinga\Application\Logger;

abstract class AuditHook
{
use HookEssentials;

final protected static function getHookName(): string
{
return 'audit';
}

/**
* Log an activity to the audit log
*
Expand All @@ -27,7 +33,7 @@ abstract class AuditHook
*/
public static function logActivity($type, $message, ?array $data = null, $identity = null, $time = null)
{
if (! Hook::has('audit')) {
if (! static::isRegistered()) {
return;
}

Expand All @@ -39,8 +45,7 @@ public static function logActivity($type, $message, ?array $data = null, $identi
$time = time();
}

foreach (Hook::all('audit') as $hook) {
/** @var self $hook */
foreach (static::all() as $hook) {
try {
$formattedMessage = $message;
if ($data !== null) {
Expand Down
17 changes: 10 additions & 7 deletions library/Icinga/Application/Hook/AuthenticationHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Icinga\Application\Hook;

use Icinga\User;
use Icinga\Web\Hook;
use Icinga\Application\Logger;
use Throwable;

Expand All @@ -18,11 +17,18 @@
*/
abstract class AuthenticationHook
{
use HookEssentials;

/**
* Name of the hook
*/
const NAME = 'authentication';

final protected static function getHookName(): string
{
return static::NAME;
}

/**
* Triggered after login in Icinga Web and when calling login action
*
Expand Down Expand Up @@ -57,8 +63,7 @@ public function onLogout(User $user)
*/
public static function triggerAuthFromSession(User $user): void
{
/** @var static $hook */
foreach (Hook::all(self::NAME) as $hook) {
foreach (static::all() as $hook) {
try {
$hook->onAuthFromSession($user);
} catch (Throwable $e) {
Expand All @@ -75,8 +80,7 @@ public static function triggerAuthFromSession(User $user): void
*/
public static function triggerLogin(User $user)
{
/** @var AuthenticationHook $hook */
foreach (Hook::all(self::NAME) as $hook) {
foreach (static::all() as $hook) {
try {
$hook->onLogin($user);
} catch (\Exception $e) {
Expand All @@ -93,8 +97,7 @@ public static function triggerLogin(User $user)
*/
public static function triggerLogout(User $user)
{
/** @var AuthenticationHook $hook */
foreach (Hook::all(self::NAME) as $hook) {
foreach (static::all() as $hook) {
try {
$hook->onLogout($user);
} catch (\Exception $e) {
Expand Down
13 changes: 9 additions & 4 deletions library/Icinga/Application/Hook/ConfigFormEventsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Icinga\Application\Hook;

use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
use Icinga\Web\Form;
Expand All @@ -15,9 +14,16 @@
*/
abstract class ConfigFormEventsHook
{
use HookEssentials;

/** @var array Array of errors found while processing the form event hooks */
private static $lastErrors = [];

final protected static function getHookName(): string
{
return 'ConfigFormEvents';
}

/**
* Get whether the hook applies to the given config form
*
Expand Down Expand Up @@ -96,14 +102,13 @@ private static function runEventMethod($eventMethod, Form $form)
{
self::$lastErrors = [];

if (! Hook::has('ConfigFormEvents')) {
if (! static::isRegistered()) {
return true;
}

$success = true;

foreach (Hook::all('ConfigFormEvents') as $hook) {
/** @var self $hook */
foreach (static::all() as $hook) {
if (! $hook->runAppliesTo($form)) {
continue;
}
Expand Down
6 changes: 6 additions & 0 deletions library/Icinga/Application/Hook/DbMigrationHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
abstract class DbMigrationHook implements Countable
{
use HookEssentials;
use Translation;

public const MYSQL_UPGRADE_DIR = 'schema/mysql-upgrades';
Expand All @@ -55,6 +56,11 @@ abstract class DbMigrationHook implements Countable
/** @var ?string The current version of this hook */
protected $version;

final protected static function getHookName(): string
{
return 'DbMigration';
}

/**
* Get whether the specified table exists in the given database
*
Expand Down
7 changes: 7 additions & 0 deletions library/Icinga/Application/Hook/GrapherHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
abstract class GrapherHook extends WebBaseHook
{
use HookEssentials;

/**
* Whether this grapher provides previews
*
Expand All @@ -30,6 +32,11 @@ abstract class GrapherHook extends WebBaseHook
*/
protected $hasTinyPreviews = false;

final protected static function getHookName(): string
{
return 'grapher';
}

/**
* Constructor must live without arguments right now
*
Expand Down
12 changes: 8 additions & 4 deletions library/Icinga/Application/Hook/HealthHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Icinga\Application\Hook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use Icinga\Data\DataArray\ArrayDatasource;
use Icinga\Exception\IcingaException;
Expand All @@ -15,6 +14,8 @@

abstract class HealthHook
{
use HookEssentials;

/** @var int */
const STATE_OK = 0;

Expand All @@ -39,6 +40,11 @@ abstract class HealthHook
/** @var Url Url to a graphical representation of the available metrics */
protected $url;

final protected static function getHookName(): string
{
return 'health';
}

/**
* Get overall state
*
Expand Down Expand Up @@ -143,9 +149,7 @@ public function setUrl(Url $url)
final public static function collectHealthData()
{
$checks = [];
foreach (Hook::all('health') as $hook) {
/** @var self $hook */

foreach (static::all() as $hook) {
try {
$hook->checkHealth();
$url = $hook->getUrl();
Expand Down
Loading
Loading