phalcon/incubator-events

Phalcon Incubator Events

v1.0.1 2021-10-06 07:37 UTC

This package is auto-updated.

Last update: 2024-09-19 22:49:52 UTC


README

功能使用示例在此

管理器

经典事件处理

经典事件处理需要有一个类,包含与事件名称相同的方法。

如果您使用多个处理程序处理单个事件,则需要多个 $eventsManager->attach() 调用

class DispatchEventsHandler
{
    public function beforeCallActionMethod(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do some stuff
    }

    public function beforeDoSomeMistakes(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do some right stuff
    }
}

class DispatchEventsHandlerTwo
{
    public function beforeCallActionMethod(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do another stuff in same event
    }
}

$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach('dispatch:beforeCallActionMethod',new DispatchEventsHandler(), 100);
$eventsManager->attach('dispatch:beforeCallActionMethod',new DispatchEventsHandlerTwo(), 101);
$eventsManager->attach('dispatch:beforeDoSomeMistakes',new DispatchEventsHandler());
//or global way
$eventsManager->attach('dispatch',new DispatchEventsHandler(), 100);
$eventsManager->attach('dispatch',new DispatchEventsHandlerTwo(), 101);

特色事件处理

此管理器版本通过使用 __invoke 提供了通过配置实现单个责任事件处理程序的功能。

创建您的处理程序类

class BeforeCallActionMethod
{
    public function __invoke(
        Phalcon\Events\EventInterface $event, 
        Phalcon\Dispatcher\DispatcherInterface $dispatcher
    ): void {
        //do some stuff
    }
}

在配置文件中定义配置

$config = new Phalcon\Config([
  'handlers' => [
      'dispatch:beforeCallActionMethod' => BeforeCallActionMethod::class,
  ],
]);

Phalcon\Incubator\Events\Manager 定义为容器中的 eventsManager 服务,使用处理程序的配置

$handlers = $config->get('handlers')->toArray();

$eventsManager = new Phalcon\Incubator\Events\Manager();
$eventsManager->loadHandlers($handlers);

定义处理程序配置的方法有多种

平面类名使用,如果您的处理程序是可调用的

$flat = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => BeforeCallActionMethod::class,
    ],
]);

相同,但更详细,并且可选地包含优先级

$verbose = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => [
            'class' => BeforeCallActionMethod::class,
            'priority' => 100, //optional
        ],
    ],
]);

使用可调用构造

$callable = new Phalcon\Config([
    'handlers' => [
        //you can use any other public method name instead of method, for example run()
        'dispatch:beforeCallActionMethod' => [new BeforeCallActionMethod(), 'run'],
        //or
        'dispatch:beforeCallActionMethod' => [BeforeCallActionMethod::class, 'staticRun'],
    ],
]);

为单个事件分组多个处理程序

$grouped = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => [
            BeforeCallActionMethod::class,
            BeforeCallActionMethodAnother::class,
            BeforeCallActionMethodThird::class,
        ],
    ],
]);

$groupedVerbose = new Phalcon\Config([
    'handlers' => [
        'dispatch:beforeCallActionMethod' => [
            [
                'class' => BeforeCallActionMethod::class,
                'priority' => 100, //optional
            ],
            [
                'class' => BeforeCallActionMethodAnother::class,
                'priority' => 101, //optional
            ],
            [
                'class' => BeforeCallActionMethodThird::class,
                'priority' => 101, //optional
            ],
        ],
    ],
]);