CMS Made Simple API

HookManager
in package

A class to manage hooks, and to call hook handlers.

This class is capable of managing a flexible list of hooks, registering handlers for those hooks, and calling the handlers and/or related events.

Tags
license

GPL

since
2.2
author

Robert Campbell calguy1000@gmail.com

Table of Contents

Constants

PRIORITY_HIGH  = 1
High priority handler
PRIORITY_LOW  = 3
Indicates a low priority handler
PRIORITY_NORMAL  = 2
Indicates a normal priority handler

Methods

add_hook()  : mixed
Add a handler to a hook
do_hook()  : mixed
Trigger a hook, progressively altering the value of the input.
do_hook_accumulate()  : array<string|int, mixed>
Trigger a hook, accumulating the results of each hook handler into an array.
do_hook_first_result()  : mixed
Trigger a hook, returning the first non empty value.
in_hook()  : bool
Test if we are currently handling a hook or not.

Constants

PRIORITY_LOW

Indicates a low priority handler

public mixed PRIORITY_LOW = 3

PRIORITY_NORMAL

Indicates a normal priority handler

public mixed PRIORITY_NORMAL = 2

Methods

add_hook()

Add a handler to a hook

public static add_hook(string $name, callable $callable[, int $priority = self::PRIORITY_NORMAL ]) : mixed
Parameters
$name : string

The hook name. If the hook does not already exist, it is added.

$callable : callable

A callable function, or a string representing a callable function. Closures are also supported.

$priority : int = self::PRIORITY_NORMAL

The priority of the handler.

do_hook()

Trigger a hook, progressively altering the value of the input.

public static do_hook() : mixed

This method accepts variable arguments. The first argument (required) is the name of the hook to execute. Further arguments will be passed to the various handlers.

If an event with the same name exists, it will be called first. Arguments will be passed as the $params array.

Return values
mixed

The output of this method depends on the hook.

do_hook_accumulate()

Trigger a hook, accumulating the results of each hook handler into an array.

public static do_hook_accumulate() : array<string|int, mixed>

This method accepts variable arguments. The first argument (required) is the name of the hook to execute. Further arguments will be passed to the various handlers.

If an event with the same name exists, it will be called first. Arguments will be passed as the $params array. The data returned in the $params array will be appended to the output array.

Return values
array<string|int, mixed>

Mixed data, as it cannot be ascertained what data is passed back from event handlers.

do_hook_first_result()

Trigger a hook, returning the first non empty value.

public static do_hook_first_result() : mixed

This method does not call event handlers with similar names.

This method accepts variable arguments. The first argument (required) is the name of the hook to execute. Further arguments will be passed to the various handlers.

This method will always pass the same input arguments to each hook handler.

Return values
mixed

The output of this method depends on the hook.

in_hook()

Test if we are currently handling a hook or not.

public static in_hook([null|string $name = null ]) : bool
Parameters
$name : null|string = null

The hook name to test for. If null is provided, the system will return true if any hook is being processed.

Return values
bool

        
On this page

Search results