Skip to content

Phalcon events

NOTE

All classes are prefixed with Phalcon

Events\Event

Source on GitHub

  • Namespace

    • Phalcon\Events
  • Uses

  • Extends

  • Implements

    • EventInterface

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();
}

Properties

/**
 * Is event cancelable?
 *
 * @var bool
 */
protected $cancelable;

/**
 * Event data
 *
 * @var mixed
 */
protected $data;

/**
 * Event source
 *
 * @var object|null
 */
protected $source;

/**
 * Is event propagation stopped?
 *
 * @var bool
 */
protected $stopped = false;

/**
 * Event type
 *
 * @var string
 */
protected $type;

Methods

public function __construct( string $type, object $source, mixed $data = null, bool $cancelable = bool );
Phalcon\Events\Event constructor

public function getData(): mixed
public function getSource(): object
public function getType(): string

public function isCancelable(): bool;
Check whether the event is cancelable.

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

public function isStopped(): bool;
Check whether the event is currently stopped.

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

public function setType( string $type ): EventInterface;
Sets event type.

public function stop(): EventInterface;
Stops the event preventing propagation.

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

Events\EventInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Events
  • Uses

  • Extends

  • Implements

Interface for Phalcon\Events\Event class

Methods

public function getData(): mixed;
Gets event data

public function getType(): mixed;
Gets event type

public function isCancelable(): bool;
Check whether the event is cancelable

public function isStopped(): bool;
Check whether the event is currently stopped

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

public function setType( string $type ): EventInterface;
Sets event type

public function stop(): EventInterface;
Stops the event preventing propagation

Events\EventsAwareInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Events
  • Uses

  • Extends

  • Implements

This interface must for those classes that accept an EventsManager and dispatch events

Methods

public function getEventsManager(): ManagerInterface | null;
Returns the internal event manager

public function setEventsManager( ManagerInterface $eventsManager ): void;
Sets the events manager

Events\Exception

Source on GitHub

  • Namespace

    • Phalcon\Events
  • Uses

  • Extends

    \Exception

  • Implements

Exceptions thrown in Phalcon\Events will use this class

Events\Manager

Source on GitHub

  • Namespace

    • Phalcon\Events
  • Uses

    • Closure
    • SplPriorityQueue
  • Extends

  • Implements

    • ManagerInterface

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.

Constants

const DEFAULT_PRIORITY = 100;

Properties

/**
 * @var bool
 */
protected $collect = false;

/**
 * @var bool
 */
protected $enablePriorities = false;

//
protected $events;

//
protected $responses;

Methods

public function arePrioritiesEnabled(): bool;
Returns if priorities are enabled

public function attach( string $eventType, mixed $handler, int $priority = static-constant-access ): void;
Attach a listener to the events manager

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

public function detach( string $eventType, mixed $handler ): void;
Detach the listener from the events manager

public function detachAll( string $type = null ): void;
Removes all events from the EventsManager

public function enablePriorities( bool $enablePriorities ): void;
Set if priorities are enabled in the EventsManager.

A priority queue of events is a data structure similar to a regular queue of events: we can also put and extract elements from it. The difference is that each element in a priority queue is associated with a value called priority. This value is used to order elements of a queue: elements with higher priority are retrieved before the elements with lower priority.

public function fire( string $eventType, object $source, mixed $data = null, bool $cancelable = bool );
Fires an event in the events manager causing the active listeners to be notified about it

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

final public function fireQueue( SplPriorityQueue $queue, EventInterface $event );
Internal handler to call a queue of events

public function getListeners( string $type ): array;
Returns all the attached listeners of a certain type

public function getResponses(): array;
Returns all the responses returned by every handler executed by the last 'fire' executed

public function hasListeners( string $type ): bool;
Check whether certain type of event has listeners

public function isCollecting(): bool;
Check if the events manager is collecting all the responses returned by every registered listener in a single fire

public function isValidHandler( mixed $handler ): bool;

Events\ManagerInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Events
  • Uses

  • Extends

  • Implements

Interface for Phalcon\Events managers.

Methods

public function attach( string $eventType, mixed $handler ): void;
Attach a listener to the events manager

public function detach( string $eventType, mixed $handler ): void;
Detach the listener from the events manager

public function detachAll( string $type = null ): void;
Removes all events from the EventsManager

public function fire( string $eventType, object $source, mixed $data = null, bool $cancelable = bool );
Fires an event in the events manager causing the active listeners to be notified about it

public function getListeners( string $type ): array;
Returns all the attached listeners of a certain type

public function hasListeners( string $type ): bool;
Check whether certain type of event has listeners