Skip to content

Phalcon events

NOTE

All classes are prefixed with Phalcon

Events\AbstractEventsAware

Abstract Source on GitHub

This abstract class offers access to the events manager

Method Summary

Properties

protected ManagerInterface|null $eventsManager = null

Methods

Public · 2

getEventsManager()

public function getEventsManager(): ManagerInterface|null;

Returns the internal event manager

setEventsManager()

public function setEventsManager( ManagerInterface $eventsManager ): void;

Sets the events manager

Protected · 1

fireManagerEvent()

protected function fireManagerEvent(
    string $eventName,
    mixed $data = null,
    bool $cancellable = true
): mixed;

Helper method to fire an event

Events\Event

Class Source on GitHub

Phalcon\Events\Event

This class offers contextual information of a fired event in the EventsManager

Phalcon\Events\Event;

$event = new Event("db:afterQuery", $this, ["data" => "mydata"], true);
if ($event->isCancelable()) {
    $event->stop();
}

Uses Phalcon\Contracts\Events\Stoppable · Phalcon\Events\Exceptions\EventNotCancelable

Method Summary

Properties

protected bool $cancelable = true
protected mixed $data = null
protected object|null $source = null
protected bool $stopped = false Is event propagation stopped?
protected string $type

Methods

Public · 10

__construct()

public function __construct(
    string $type,
    object|null $source = null,
    mixed $data = null,
    bool $cancelable = true
);

Event constructor.

getData()

public function getData(): mixed;

getSource()

public function getSource(): object|null;

getType()

public function getType(): string;

isCancelable()

public function isCancelable(): bool;

Check whether the event is cancelable.

if ($event->isCancelable()) {
    $event->stop();
}

isPropagationStopped()

public function isPropagationStopped(): bool;

Returns whether propagation must stop. PSR-14 alias backed by the same stopped flag as isStopped(); calling stop() flips both.

isStopped()

public function isStopped(): bool;

Check whether the event is currently stopped.

setData()

public function setData( mixed $data = null ): EventInterface;

Sets event data.

setType()

public function setType( string $type ): EventInterface;

Sets event type.

stop()

public function stop(): EventInterface;

Stops the event preventing propagation.

if ($event->isCancelable()) {
    $event->stop();
}

Events\EventInterface

Interface Source on GitHub

Phalcon\Events\EventInterface

Uses Phalcon\Contracts\Events\Event

Events\EventsAwareInterface

Interface Source on GitHub

Phalcon\Events\EventsAwareInterface

Uses Phalcon\Contracts\Events\EventsAware

Events\Exception

Class Source on GitHub

Phalcon\Events\Exception

Exceptions thrown in Phalcon\Events will use this class

Events\Exceptions\EventNotCancelable

Class Source on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Events\Exceptions\InvalidEventHandler

Class Source on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Events\Exceptions\InvalidEventSource

Class Source on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $type,
    string $sourceType
);

Events\Exceptions\InvalidEventType

Class Source on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $eventType );

Events\Exceptions\InvalidSubscriberConfiguration

Class Source on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $eventName );

Events\Exceptions\NoListenersForEvent

Class Source on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $eventType );

Events\Manager

Class Source on GitHub

Phalcon Events Manager, offers an easy way to intercept and manipulate, if needed, the normal flow of operation. With the EventsManager the developer can create hooks or plugins that will offer monitoring of data, manipulation, conditional execution and much more.

Uses Closure · Phalcon\Contracts\Events\Enumerable · Phalcon\Contracts\Events\Stoppable · Phalcon\Contracts\Events\Subscriber · Phalcon\Db\Event\AbstractModelEvent · Phalcon\Db\Event\ModelEventNameEnum · Phalcon\Events\Exceptions\InvalidEventHandler · Phalcon\Events\Exceptions\InvalidEventType · Phalcon\Events\Exceptions\InvalidSubscriberConfiguration · Phalcon\Events\Exceptions\NoListenersForEvent · Psr\EventDispatcher\EventDispatcherInterface · Psr\EventDispatcher\StoppableEventInterface · Throwable

Method Summary

public void addSubscriber( Subscriber $subscriber ) Registers an event subscriber. The subscriber's getSubscribedEvents() public bool arePrioritiesEnabled() Returns if priorities are enabled public void attach(array|string $eventType,callable|object $handler,int $priority = self::DEFAULT_PRIORITY) Attach a listener to the events manager public void clearSubscribers() Removes every registered subscriber and detaches each listener they public void collectResponses( bool $collect ) Tells the event manager if it needs to collect all the responses returned public void detach(string $eventType,callable|object $handler) Detach the listener from the events manager public void detachAll( string|null $type = null ) Removes all events from the EventsManager public mixed dispatch(object $event,array|string|null $name = null,object|null $source = null) Dispatches an object event to the appropriate event listeners. public void enablePriorities( bool $enablePriorities ) Set if priorities are enabled in the EventsManager. public mixed fire(string $eventType,object $source,mixed $data = null,bool $cancelable = true) Fires an event in the events manager causing the active listeners to be public array fireAll(string $eventType,object $source,mixed $data = null,bool $cancelable = true) Fires an event and returns every listener's return value as an indexed public mixed fireQueue(array $queue,EventInterface $event) Internal handler to call a queue of events. public array getListenerMap() Returns every event type that currently has at least one listener, public array getListeners( string $type ) Returns all the attached listeners of a certain type public int getMethodExistsCacheLimit() Returns the configured method_exists-cache cap (0 = unlimited). public array getResponses() Returns all the responses returned by every handler executed by the last public array getSubscribers() Returns the list of registered subscriber instances. public void halt() Manager-level kill switch. After halt(), every fire()/fireAll()/ public bool hasListeners( string $type ) Check whether certain type of event has listeners public bool isCollecting() Check if the events manager is collecting all the responses returned by public bool isHalted() Returns whether the manager-level kill switch is engaged. See halt(). public bool isStopOnFalse() Returns whether the stop-on-false short-circuit is enabled. public bool isStrict() Returns whether strict mode is enabled. public bool isValidHandler( mixed $handler ) public void removeSubscriber( Subscriber $subscriber ) Removes a previously registered subscriber. Detaches every listener the public void resume() Clears the manager-level kill switch set by halt(). public void setMethodExistsCacheLimit( int $methodExistsCacheLimit ) Caps the number of distinct handler classes retained in the public void setStopOnFalse( bool $flag ) Enables/disables the stop-on-false short-circuit. Default off. public void setStrict( bool $strict ) Enables/disables strict mode. When true, fire()/fireAll() throw when protected mixed afterFire(mixed $status,string $eventType,object $source,mixed $data = null,bool $cancelable = true) Extension seam invoked after an event has been dispatched to its protected bool beforeFire(string $eventType,object $source,mixed $data = null,bool $cancelable = true) Extension seam invoked before an event is dispatched. The base

Properties

protected bool $collect = false
protected bool $enablePriorities = false
protected array $eventNameCache = [] Parsed-eventType cache. Memoizes the strpos + substr work done in fire() so the same event name fired repeatedly collapses to a single hash lookup. Shape: eventNameCache[$eventType] = [typePrefix, eventName]
protected array $events = [] Listener storage. Shape: events[$eventType] = [ [handler, type, priority] // types 0, 1, 3 [handler, type, priority, className] // type 2 carries // resolved class name ... ] type is classified once at attach() time so the dispatch loop can route via a simple branch: 0 - Closure 1 - [obj, method] array callable 2 - plain object: method named after the event 3 - generic callable (string fn name, invokable object, etc.)
protected int $fireDepth = 0 Re-entrancy depth of fire()/fireAll(). 0 means no fire is in progress. Used to keep nested fire() calls from clobbering the outer caller's $this->responses accumulator.
protected bool $halted = false Manager-level kill switch. When true, every fire()/fireAll()/ fireQueue() call returns immediately without dispatching. Cleared by resume().
protected array $methodExistsCache = [] Memoized method_exists() results for the plain-object dispatch path. Keyed by handlerClass => [methodName => bool].
protected int $methodExistsCacheLimit = 0 Maximum number of distinct handler classes retained in methodExistsCache. 0 (default) keeps the unbounded behavior.
protected array<array-key, mixed> $responses = []
protected bool $stopOnFalse = false When true, a listener returning literal false (with the event's cancelable flag on) short-circuits the dispatch loop and pins the fire() return as false. Default off.
protected bool $strict = false When true, fire()/fireAll() throw on dispatch of an event that has zero matching listeners. Default off.
protected array $subscriberEventsCache = [] Memoized getSubscribedEvents() maps keyed by Subscriber class name.
protected array $subscribers = []

Methods

Public · 29

addSubscriber()

public function addSubscriber( Subscriber $subscriber ): void;

Registers an event subscriber. The subscriber's getSubscribedEvents() map is parsed and each entry is attached through the regular listener pipeline.

arePrioritiesEnabled()

public function arePrioritiesEnabled(): bool;

Returns if priorities are enabled

attach()

final public function attach(
    array|string $eventType,
    callable|object $handler,
    int $priority = self::DEFAULT_PRIORITY
): void;

Attach a listener to the events manager

clearSubscribers()

public function clearSubscribers(): void;

Removes every registered subscriber and detaches each listener they contributed. Listeners attached via attach() are untouched.

collectResponses()

public function collectResponses( bool $collect ): void;

Tells the event manager if it needs to collect all the responses returned by every registered listener in a single fire

detach()

public function detach(
    string $eventType,
    callable|object $handler
): void;

Detach the listener from the events manager

detachAll()

public function detachAll( string|null $type = null ): void;

Removes all events from the EventsManager

dispatch()

public function dispatch(
    object $event,
    array|string|null $name = null,
    object|null $source = null
): mixed;

Dispatches an object event to the appropriate event listeners.

PSR-14 shaped: listeners receive the (possibly mutated) event object. Propagation stops when the event implements {@see StoppableEventInterface} and reports it is stopped.

enablePriorities()

public function enablePriorities( bool $enablePriorities ): void;

Set if priorities are enabled in the EventsManager.

fire()

public function fire(
    string $eventType,
    object $source,
    mixed $data = null,
    bool $cancelable = true
): mixed;

Fires an event in the events manager causing the active listeners to be notified about it

$eventsManager->fire("db", $connection);

fireAll()

public function fireAll(
    string $eventType,
    object $source,
    mixed $data = null,
    bool $cancelable = true
): array;

Fires an event and returns every listener's return value as an indexed array. Independent of collectResponses(); the caller's collected state on $this->responses is preserved (stashed and restored).

fireQueue()

final public function fireQueue(
    array $queue,
    EventInterface $event
): mixed;

Internal handler to call a queue of events.

Kept as a thin BC wrapper around the private dispatch loop.

getListenerMap()

public function getListenerMap(): array;

Returns every event type that currently has at least one listener, mapped to that type's listeners. Types contributed by subscribers are included, because addSubscriber() attaches through the regular listener pipeline.

Unwrapping is delegated to getListeners() so the internal shape of $this->events is read in exactly one place.

getListeners()

public function getListeners( string $type ): array;

Returns all the attached listeners of a certain type

getMethodExistsCacheLimit()

public function getMethodExistsCacheLimit(): int;

Returns the configured method_exists-cache cap (0 = unlimited).

getResponses()

public function getResponses(): array;

Returns all the responses returned by every handler executed by the last 'fire' executed

getSubscribers()

public function getSubscribers(): array;

Returns the list of registered subscriber instances.

halt()

public function halt(): void;

Manager-level kill switch. After halt(), every fire()/fireAll()/ fireQueue() call returns immediately without dispatching, until resume() is called.

hasListeners()

public function hasListeners( string $type ): bool;

Check whether certain type of event has listeners

isCollecting()

public function isCollecting(): bool;

Check if the events manager is collecting all the responses returned by every registered listener in a single fire

isHalted()

public function isHalted(): bool;

Returns whether the manager-level kill switch is engaged. See halt().

isStopOnFalse()

public function isStopOnFalse(): bool;

Returns whether the stop-on-false short-circuit is enabled.

isStrict()

public function isStrict(): bool;

Returns whether strict mode is enabled.

isValidHandler()

public function isValidHandler( mixed $handler ): bool;

removeSubscriber()

public function removeSubscriber( Subscriber $subscriber ): void;

Removes a previously registered subscriber. Detaches every listener the subscriber declared via getSubscribedEvents(). Idempotent.

resume()

public function resume(): void;

Clears the manager-level kill switch set by halt().

setMethodExistsCacheLimit()

public function setMethodExistsCacheLimit( int $methodExistsCacheLimit ): void;

Caps the number of distinct handler classes retained in the method_exists memoization cache. 0 disables the cap.

setStopOnFalse()

public function setStopOnFalse( bool $flag ): void;

Enables/disables the stop-on-false short-circuit. Default off.

setStrict()

public function setStrict( bool $strict ): void;

Enables/disables strict mode. When true, fire()/fireAll() throw when dispatching an event with zero matching listeners.

Protected · 2

afterFire()

protected function afterFire(
    mixed $status,
    string $eventType,
    object $source,
    mixed $data = null,
    bool $cancelable = true
): mixed;

Extension seam invoked after an event has been dispatched to its listener queues. The base implementation returns status unchanged.

beforeFire()

protected function beforeFire(
    string $eventType,
    object $source,
    mixed $data = null,
    bool $cancelable = true
): bool;

Extension seam invoked before an event is dispatched. The base implementation returns true, so dispatch proceeds. Returning false aborts the dispatch entirely.

Events\ManagerInterface

Interface Source on GitHub

Phalcon\Events\ManagerInterface

Uses Phalcon\Contracts\Events\Manager

Events\PsrEventInterface

Interface Source on GitHub

  • Phalcon\Events\PsrEventInterface

Events\Traits\EventsAwareTrait

Trait Source on GitHub

  • Phalcon\Events\Traits\EventsAwareTrait

Uses Phalcon\Events\Exception · Phalcon\Events\ManagerInterface · Phalcon\Events\PsrEventInterface

Used by Phalcon\Application\AbstractApplication · Phalcon\Auth\Guard\AbstractGuard · Phalcon\Autoload\Loader · Phalcon\Cache\AbstractCache · Phalcon\Cli\Task · Phalcon\Db\Adapter\AbstractAdapter · Phalcon\Di\Di · Phalcon\Dispatcher\AbstractDispatcher · Phalcon\Http\Request · Phalcon\Http\Response · Phalcon\Mvc\Controller · Phalcon\Mvc\Dispatcher · Phalcon\Mvc\Micro · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Router · Phalcon\Mvc\View · Phalcon\Mvc\View\Engine\AbstractEngine · Phalcon\Mvc\View\Simple · Phalcon\Storage\Adapter\AbstractAdapter

Method Summary

Properties

protected ManagerInterface|null $eventsManager = null

Methods

Public · 2

getEventsManager()

public function getEventsManager(): ManagerInterface|null;

Returns the internal event manager

setEventsManager()

public function setEventsManager( ManagerInterface $eventsManager ): void;

Sets the events manager

Protected · 2

fireManagerEvent()

protected function fireManagerEvent(
    string $eventName,
    mixed $data = null,
    bool $cancellable = true
);

Helper method to fire an event

firePsrEvent()

protected function firePsrEvent(
    PsrEventInterface $event,
    string|null $name = null
): mixed;