Skip to content

Phalcon Events

Updated View as Markdown

Events\AbstractEventsAware

AbstractSource on GitHub

This abstract class offers access to the events manager

Method Summary

Properties

protectedManagerInterface|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,
    bool $stopOnFalse = false
): mixed;

Helper method to fire an event

Events\Event

ClassSource 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

protectedbool$cancelable = true
protectedmixed$data = null
protectedobject|null$source = null
protectedbool$stopped = false

Is event propagation stopped?

protectedstring$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

InterfaceSource on GitHub

Phalcon\Events\EventInterface

Uses Phalcon\Contracts\Events\Event

Events\EventsAwareInterface

InterfaceSource on GitHub

Phalcon\Events\EventsAwareInterface

Uses Phalcon\Contracts\Events\EventsAware

Events\Exception

ClassSource on GitHub

Phalcon\Events\Exception

Exceptions thrown in Phalcon\Events will use this class

Events\Exceptions\EventNotCancelable

ClassSource on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Events\Exceptions\InvalidEventHandler

ClassSource on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Events\Exceptions\InvalidEventSource

ClassSource on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

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

Events\Exceptions\InvalidEventType

ClassSource on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $eventType );

Events\Exceptions\InvalidSubscriberConfiguration

ClassSource on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $eventName );

Events\Exceptions\NoListenersForEvent

ClassSource on GitHub

Uses Phalcon\Events\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $eventType );

Events\Manager

ClassSource 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

publicvoidaddSubscriber(Subscriber $subscriber)

Registers an event subscriber. The subscriber’s getSubscribedEvents()

publicboolarePrioritiesEnabled()

Returns if priorities are enabled

publicvoidattach(array|string $eventType,callable|object $handler,int $priority = self::DEFAULT_PRIORITY)

Attach a listener to the events manager

publicvoidclearSubscribers()

Removes every registered subscriber and detaches each listener they

publicvoidcollectResponses(bool $collect)

Tells the event manager if it needs to collect all the responses returned

publicvoiddetach(string $eventType,callable|object $handler)

Detach the listener from the events manager

publicvoiddetachAll(string|null $type = null)

Removes all events from the EventsManager

publicmixeddispatch(object $event,array|string|null $name = null,object|null $source = null)

Dispatches an object event to the appropriate event listeners.

publicvoidenablePriorities(bool $enablePriorities)

Set if priorities are enabled in the EventsManager.

publicmixedfire(string $eventType,object $source,mixed $data = null,bool $cancelable = true,bool|null $stopOnFalse = null)

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

publicarrayfireAll(string $eventType,object $source,mixed $data = null,bool $cancelable = true)

Fires an event and returns every listener’s return value as an indexed

publicmixedfireQueue(array $queue,EventInterface $event)

Internal handler to call a queue of events.

publicarraygetListenerMap()

Returns every event type that currently has at least one listener,

publicarraygetListeners(string $type)

Returns all the attached listeners of a certain type

publicintgetMethodExistsCacheLimit()

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

publicarraygetResponses()

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

publicarraygetSubscribers()

Returns the list of registered subscriber instances.

publicvoidhalt()

Manager-level kill switch. After halt(), every fire()/fireAll()/

publicboolhasListeners(string $type)

Check whether certain type of event has listeners

publicboolisCollecting()

Check if the events manager is collecting all the responses returned by

publicboolisHalted()

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

publicboolisStopOnFalse()

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

publicboolisStrict()

Returns whether strict mode is enabled.

publicboolisValidHandler(mixed $handler)publicvoidremoveSubscriber(Subscriber $subscriber)

Removes a previously registered subscriber. Detaches every listener the

publicvoidresume()

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

publicvoidsetMethodExistsCacheLimit(int $methodExistsCacheLimit)

Caps the number of distinct handler classes retained in the

publicvoidsetStopOnFalse(bool $flag)

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

publicvoidsetStrict(bool $strict)

Enables/disables strict mode. When true, fire()/fireAll() throw when

protectedmixedafterFire(mixed $status,string $eventType,object $source,mixed $data = null,bool $cancelable = true)

Extension seam invoked after an event has been dispatched to its

protectedboolbeforeFire(string $eventType,object $source,mixed $data = null,bool $cancelable = true)

Extension seam invoked before an event is dispatched. The base

Properties

protectedbool$collect = false
protectedbool$enablePriorities = false
protectedarray$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]

protectedarray$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.)

protectedint$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.

protectedbool$halted = false

Manager-level kill switch. When true, every fire()/fireAll()/ fireQueue() call returns immediately without dispatching. Cleared by resume().

protectedarray$methodExistsCache = []

Memoized method_exists() results for the plain-object dispatch path. Keyed by handlerClass => [methodName => bool].

protectedint$methodExistsCacheLimit = 0

Maximum number of distinct handler classes retained in methodExistsCache. 0 (default) keeps the unbounded behavior.

protectedarray<array-key, mixed>$responses = []
protectedbool$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.

protectedbool$strict = false

When true, fire()/fireAll() throw on dispatch of an event that has zero matching listeners. Default off.

protectedarray$subscriberEventsCache = []

Memoized getSubscribedEvents() maps keyed by Subscriber class name.

protectedarray$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,
    bool|null $stopOnFalse = null
): 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

InterfaceSource on GitHub

Phalcon\Events\ManagerInterface

Uses Phalcon\Contracts\Events\Manager

Events\PsrEventInterface

InterfaceSource on GitHub
  • Phalcon\Events\PsrEventInterface

Events\Traits\EventsAwareTrait

TraitSource on GitHub
  • Phalcon\Events\Traits\EventsAwareTrait

Uses Phalcon\Events\Exception · Phalcon\Events\Manager · 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\DataMapper\Pdo\ConnectionLocator · Phalcon\DataMapper\Pdo\Connection\AbstractConnection · 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

protectedManagerInterface|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,
    bool $stopOnFalse = false
);

Helper method to fire an event

firePsrEvent()

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

Type to search…

↑↓ navigate↵ selectEsc close