CodeIgniter v4.5 API

Events
in package

Events

Tags
see
EventsTest

Table of Contents

Constants

PRIORITY_HIGH  = 10
PRIORITY_LOW  = 200
PRIORITY_NORMAL  = 100

Properties

$files  : array<int, string>
A list of found files.
$initialized  : bool
Flag to let us know if we've read from the Config file(s) and have all of the defined events.
$listeners  : array<string|int, mixed>
The list of listeners.
$performanceLog  : array<int, array<string, float|string>>
Stores information about the events for display in the debug toolbar.
$simulate  : bool
If true, events will not actually be fired.

Methods

getFiles()  : array<int, string>
Returns the files that were found/loaded during this request.
getPerformanceLogs()  : array<int, array<string, float|string>>
Getter for the performance log records.
initialize()  : void
Ensures that we have a events file ready.
listeners()  : array<string|int, mixed>
Returns an array of listeners for a single event. They are sorted by priority.
on()  : void
Registers an action to happen on an event. The action can be any sort of callable:
removeAllListeners()  : void
Removes all listeners.
removeListener()  : bool
Removes a single listener from an event.
setFiles()  : void
Sets the path to the file that routes are read from.
simulate()  : void
Turns simulation on or off. When on, events will not be triggered, simply logged. Useful during testing when you don't actually want the tests to run.
trigger()  : bool
Runs through all subscribed methods running them one at a time, until either: a) All subscribers have finished or b) a method returns false, at which point execution of subscribers stops.

Constants

PRIORITY_HIGH

public mixed PRIORITY_HIGH = 10

PRIORITY_LOW

public mixed PRIORITY_LOW = 200

PRIORITY_NORMAL

public mixed PRIORITY_NORMAL = 100

Properties

$files

A list of found files.

protected static array<int, string> $files = []

$initialized

Flag to let us know if we've read from the Config file(s) and have all of the defined events.

protected static bool $initialized = false

$listeners

The list of listeners.

protected static array<string|int, mixed> $listeners = []

$performanceLog

Stores information about the events for display in the debug toolbar.

protected static array<int, array<string, float|string>> $performanceLog = []

$simulate

If true, events will not actually be fired.

protected static bool $simulate = false

Useful during testing.

Methods

getFiles()

Returns the files that were found/loaded during this request.

public static getFiles() : array<int, string>
Return values
array<int, string>

getPerformanceLogs()

Getter for the performance log records.

public static getPerformanceLogs() : array<int, array<string, float|string>>
Return values
array<int, array<string, float|string>>

initialize()

Ensures that we have a events file ready.

public static initialize() : void

listeners()

Returns an array of listeners for a single event. They are sorted by priority.

public static listeners(string $eventName) : array<string|int, mixed>
Parameters
$eventName : string
Return values
array<string|int, mixed>

on()

Registers an action to happen on an event. The action can be any sort of callable:

public static on(string $eventName, callable $callback[, int $priority = self::PRIORITY_NORMAL ]) : void

Events::on('create', 'myFunction'); // procedural function Events::on('create', ['myClass', 'myMethod']); // Class::method Events::on('create', [$myInstance, 'myMethod']); // Method on an existing instance Events::on('create', function() }); // Closure

Parameters
$eventName : string
$callback : callable
$priority : int = self::PRIORITY_NORMAL

removeAllListeners()

Removes all listeners.

public static removeAllListeners([string|null $eventName = null ]) : void

If the event_name is specified, only listeners for that event will be removed, otherwise all listeners for all events are removed.

Parameters
$eventName : string|null = null

removeListener()

Removes a single listener from an event.

public static removeListener(string $eventName, callable $listener) : bool

If the listener couldn't be found, returns FALSE, else TRUE if it was removed.

Parameters
$eventName : string
$listener : callable
Return values
bool

setFiles()

Sets the path to the file that routes are read from.

public static setFiles(array<string|int, mixed> $files) : void
Parameters
$files : array<string|int, mixed>

simulate()

Turns simulation on or off. When on, events will not be triggered, simply logged. Useful during testing when you don't actually want the tests to run.

public static simulate([bool $choice = true ]) : void
Parameters
$choice : bool = true

trigger()

Runs through all subscribed methods running them one at a time, until either: a) All subscribers have finished or b) a method returns false, at which point execution of subscribers stops.

public static trigger(string $eventName, mixed ...$arguments) : bool
Parameters
$eventName : string
$arguments : mixed
Return values
bool

        
On this page

Search results