跳转到内容

Phalcon事件

注意

所有类都以前缀命名Phalcon

事件\AbstractEventsAwareAbstract

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

    • Phalcon\Events\ManagerInterface
  • 继承

  • 实现

这个抽象类提供了对事件管理器的访问

属性

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

方法

public function getEventsManager(): ManagerInterface | null;
返回内部事件管理器

public function setEventsManager( ManagerInterface $eventsManager ): void;
设置事件管理器

protected function fireManagerEvent( string $eventName, mixed $data = null, bool $cancellable = bool ): mixed | bool;
触发事件的帮助方法

事件\Event

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

  • 继承

  • 实现

    • EventInterface

此类在EventsManager中提供已触发事件的上下文信息

Phalcon\Events\Event;

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

属性

/**
 * 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;

方法

public function __construct( string $type, mixed $source = null, mixed $data = null, bool $cancelable = bool );
Phalcon\Events\Event 构造函数

public function getData(): mixed;
public function getSource(): object | null;
public function getType(): string;

public function isCancelable(): bool;
检查事件是否可取消。

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

public function isStopped(): bool;
检查事件是否当前已停止。

public function setData( mixed $data = null ): EventInterface;
设置事件数据。

public function setType( string $type ): EventInterface;
设置事件类型。

public function stop(): EventInterface;
停止事件以阻止传播。

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

事件\EventInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

  • 继承

  • 实现

Phalcon\Events\Event 类的接口

方法

public function getData(): mixed;
获取事件数据

public function getType(): mixed;
获取事件类型

public function isCancelable(): bool;
检查事件是否可取消

public function isStopped(): bool;
检查事件是否当前已停止

public function setData( mixed $data = null ): EventInterface;
设置事件数据

public function setType( string $type ): EventInterface;
设置事件类型

public function stop(): EventInterface;
停止事件以阻止传播

事件\EventsAwareInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

  • 继承

  • 实现

此接口适用于那些接受EventsManager并分发事件的类

方法

public function getEventsManager(): ManagerInterface | null;
返回内部事件管理器

public function setEventsManager( ManagerInterface $eventsManager ): void;
设置事件管理器

事件\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

  • 继承

    \Exception

  • 实现

Phalcon\Events 中抛出的异常将使用此类

事件\Manager

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

    • Closure
    • SplPriorityQueue
  • 继承

  • 实现

    • ManagerInterface

Phalcon Events Manager 提供了一种简单的方法来拦截和操作,如果需要的话,正常的操作流程。通过 EventsManager,开发人员可以创建钩子或插件,这些钩子或插件将提供数据监控、操作、条件执行等更多功能。

常量

const DEFAULT_PRIORITY = 100;

属性

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

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

/**
 * @var array
 */
protected $events;

/**
 * @var array
 */
protected $responses;

方法

public function arePrioritiesEnabled(): bool;
返回优先级是否启用

public function attach( string $eventType, mixed $handler, int $priority = static-constant-access ): void;
将监听器附加到事件管理器

public function collectResponses( bool $collect ): void;
告诉事件管理器是否需要收集所有由每个注册监听器返回的响应在一个单独的触发中

public function detach( string $eventType, mixed $handler ): void;
从事件管理器中分离监听器

public function detachAll( string $type = null ): void;
从EventsManager中移除所有事件

public function enablePriorities( bool $enablePriorities ): void;
在EventsManager中设置是否启用了优先级。

事件的优先队列是一种类似于普通事件队列的数据结构:我们也可以从中放入和提取元素。区别在于优先队列中的每个元素都与一个称为优先级的值相关联。这个值用于对队列中的元素进行排序:高优先级的元素会比低优先级的元素先被检索。

public function fire( string $eventType, object $source, mixed $data = null, bool $cancelable = bool );
在事件管理器中触发事件,导致活跃的监听器接收到该通知

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

final public function fireQueue( SplPriorityQueue $queue, EventInterface $event );
内部处理器调用事件队列

public function getListeners( string $type ): array;
返回所有特定类型已附加的监听器

public function getResponses(): array;
返回上次 'fire' 执行时每个处理程序返回的所有响应

public function hasListeners( string $type ): bool;
检查特定类型的事件是否有监听器

public function isCollecting(): bool;
检查事件管理器是否正在收集所有由每次调用返回的响应

public function isValidHandler( mixed $handler ): bool;

事件\ManagerInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Events
  • 使用

  • 继承

  • 实现

Phalcon\Events 管理器的接口。

方法

public function attach( string $eventType, mixed $handler ): void;
将监听器附加到事件管理器

public function detach( string $eventType, mixed $handler ): void;
从事件管理器中分离监听器

public function detachAll( string $type = null ): void;
从EventsManager中移除所有事件

public function fire( string $eventType, object $source, mixed $data = null, bool $cancelable = bool );
在事件管理器中触发事件,导致活跃的监听器接收到该通知

public function getListeners( string $type ): array;
返回所有特定类型已附加的监听器

public function hasListeners( string $type ): bool;
检查特定类型的事件是否有监听器

无噪 Logo
无噪文档
25 年 6 月翻译
文档源↗