Event registration

Swoft provides easy event management and use

annotation

Listener

Event listener class annotation tag @Listener

  • Annotation class: Swoft\Event\Annotation\Mapping\Listener
  • Scope: CLASS
  • Have attributes:
    • event string The name of the event to listen to
    • priority int The priority this listener. The larger the value, the earlier the call is called.

Note that your class must implement the interface: Swoft\Event\EventHandlerInterface

Subscriber

The event listener class annotation tag @Subscriber , unlike @Listener , allows multiple events to be handled in the same class.

  • Annotation class: Swoft\Event\Annotation\Mapping\Subscriber
  • Scope: CLASS

Note that your class must implement the interface: Swoft\Event\EventSubscriberInterface

Example

Listener example

 <?php declare(strict_types=1);

namespace SwoftTest\Event\Testing;

use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;

/**
 * Class TestHandler
 * @Listener("test.evt")
 */
class TestHandler implements EventHandlerInterface
{
    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        $pos = __METHOD__;
        echo "handle the event '{$event->getName()}' on the: $pos\n";
    }
} 

Subscriber example

 <?php declare(strict_types=1);

namespace SwoftTest\Event\Testing;

use Swoft\Event\Annotation\Mapping\Subscriber;
use Swoft\Event\EventInterface;
use Swoft\Event\EventSubscriberInterface;
use Swoft\Event\Listener\ListenerPriority;

/**
 * Class TestSubscriber
 * @Subscriber()
 */
class TestSubscriber implements EventSubscriberInterface
{
    public const EVENT_ONE = 'test.event1';
    public const EVENT_TWO = 'test.event2';

    /**
     * Configure events and corresponding processing methods (you can configure the priority)
     * @return array
     * [
     *  'event name' => 'handler method'
     *  'event name' => ['handler method', priority]
     * ]
     */
    public static function getSubscribedEvents(): array
    {
        return [
            self::EVENT_ONE => 'handleEvent1',
            self::EVENT_TWO => ['handleEvent2', ListenerPriority::HIGH],
        ];
    }

    public function handleEvent1(EventInterface $evt): void
    {
        $evt->setParams(['msg' => 'handle the event: test.event1 position: TestSubscriber.handleEvent1()']);
    }

    public function handleEvent2(EventInterface $evt): void
    {
        $evt->setParams(['msg' => 'handle the event: test.event2 position: TestSubscriber.handleEvent2()']);
    }
} 

trigger event

Event name management is recommended to be placed in a separate class of constants for easy management and maintenance

method one

Easy to use. However, multiple parameters are placed in order, so you need to get them according to the index when you get them.

  • Swoft::trigger('event name', mixd $target, $args...)
 \Swoft::trigger('event name', 'target', $arg0, $arg1); 

Get the event parameters:

 $target = $event->getTarget();

$arg0 = $event->getParam(0);
$arg1 = $event->getParam(1); 

Way two

It is a bit more troublesome to use. However, multiple parameters are loaded by kv, and can be obtained according to the key when acquiring.

  • Swoft::triggerByArray('event name', mixd $target, array $args)
 \Swoft::triggerByArray('event name', 'target', [
    'arg0' => $arg0,
    'arg0' => $arg1
]); 

Get the event parameters:

 $target = $event->getTarget();

$arg0 = $event->getParam('arg0');
$arg1 = $event->getParam('arg1'); 
/docs/2.x/en/event/usage.html
progress-bar