PHP code example of guirong / php-event

1. Go to this page and download the library: Download guirong/php-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

guirong / php-event example snippets




use Guirong\Event\Event;

class EventService extends Event
{
    /**
     * 应用程序的事件监听器映射
     *
     * @var array
     */
    protected $listen = [
        'App\Events\OrderPayed' => [
            'App\Listeners\NotifyBuyerListener',
            'App\Listeners\NotifyShopListener',
        ],
    ];

    /**
     * 应用程序的事件,自动注册
     */
    protected function register()
    {
        parent::register();
    }
}



namespace App\Events;

class OrderPayed
{

    /**
     * 订单数据集合
     * 
     * @var array 
     */
    public $order;

    /**
     * 创建一个事件实例。
     *
     * @param  array  $order
     * @return void
     */
    public function __construct(array $order)
    {
        $this->order = $order;
    }
}



namespace App\Listeners;

use App\Events\OrderPayed;

class NotifyBuyerListener
{

    /**
     * 创建事件监听器。
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * 处理事件。
     *
     * @param  \App\Events\OrderPayed  $event
     * @return void
     */
    public function handle(OrderPayed $event)
    {
        // 使用 $event->order 来访问 order, 处理业务 ...
        $order = $event->order;
    }
}



namespace App\Listeners;

use App\Events\OrderPayed;

class NotifyShopListener
{

    /**
     * 创建事件监听器。
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * 处理事件。
     *
     * @param  \App\Events\OrderPayed  $event
     * @return void
     */
    public function handle(OrderPayed $event)
    {
        // 使用 $event->order 来访问 order, 处理业务 ...
        $order = $event->order;
    }
}



namespace App\Controllers;

use App\Events\OrderPayed;
use Guirong\Event\Event\EventService;

class Order
{
    /**
     * 将付款成功的订单,发送消息通知
     *
     * @param  string  $orderNo
     */
    public function payed($orderNo)
    {
        $order = [
            'orderNo' => $orderNo,
            'shopId' => 'shop_3',
            'buyerId' => 'buyer_6',
        ];

        // 订单付款成功,消息通知买家和卖家逻辑 ...

        \Guirong\Event\trigger(
            EventService::class, 
            new OrderPayed($order)
        );
    }
}



if (!file_exists('event')) {
    /**
     * 事件分发助手函数
     * 
     * @param mixed $event
     * @param array $payload
     * @return void
     */
    function event($event, array $payload = [])
    {
        return \Guirong\Event\trigger(
            EventService::class,
            $event,
            $payload
        );
    }
}



namespace App\Listeners;

use App\Events\UserRegister;
use App\Events\UserDestory;

class UserEventSubscriber
{
    /**
     * 处理用户注册账户事件。
     */
    public function onUserRegister($event) {
        // 使用 $event->user 来访问用户信息, 处理业务 ...
        $user = $event->user;
    }

    /**
     * 处理用户注销账户事件。
     */
    public function onUserDestory($event) {
        // 使用 $event->user 来访问用户信息, 处理业务 ...
        $user = $event->user;
    }

    /**
     * 为订阅者注册监听器
     *
     * @param  \Guirong\Event\Dispatcher\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            UserRegister::class,
            'App\Listeners\UserEventSubscriber@onUserRegister'
        );

        $events->listen(
            UserDestory::class,
            'App\Listeners\UserEventSubscriber@onUserDestory'
        );
    }
}



namespace App\Events;

class UserRegister
{

    /**
     * 用户信息集合
     * 
     * @var array 
     */
    public $user;

    /**
     * 创建一个事件实例。
     *
     * @param  array  $user
     * @return void
     */
    public function __construct(array $user)
    {
        $this->user = $user;
    }
}



namespace App\Events;

class UserDestory
{

    /**
     * 用户信息集合
     * 
     * @var array 
     */
    public $user;

    /**
     * 创建一个事件实例。
     *
     * @param  array  $user
     * @return void
     */
    public function __construct(array $user)
    {
        $this->user = $user;
    }
}



use Guirong\Event\Event;
use App\Listeners\UserEventSubscriber;

class EventService extends Event
{
    /**
     * 应用程序的事件监听器映射
     *
     * @var array
     */
    protected $listen = [
        //
    ];

    /**
     * 需要注册的订阅者类
     *
     * @var array
     */
    protected $subscribe = [
        UserEventSubscriber::class
    ];
}



namespace App\Controllers;

use App\Events\UserRegister;
use App\Events\UserDestory;

class User
{
    /**
     * 将注册成功的用户消息,站内信通知给后台
     *
     * @param  array  $user
     */
    public function register($user)
    {
        // 触发用户注册成功的事件
        event(new UserRegister($user));
    }

    /**
     * 将注销账号的用户信息,站内信通知给后台
     *
     * @param  array  $user
     */
    public function destory($user)
    {
        // 触发用户注销的事件
        event(new UserDestory($user));
    }
}



use Guirong\Event\Event;
use App\Listeners\UserEventSubscriber;

class EventService extends Event
{
    /**
     * 应用程序的事件监听器映射
     *
     * @var array
     */
    protected $listen = [
        'App\Events\OrderPayed' => [
            'App\Listeners\NotifyBuyerListener',
            'App\Listeners\NotifyShopListener',
        ],
    ];

    /**
     * 需要注册的订阅者类
     *
     * @var array
     */
    protected $subscribe = [
        UserEventSubscriber::class
    ];

    /**
     * 注册应用中的其它事件
     */
    protected function register()
    {
        parent::register();

        // 用户登录事件
        \Guirong\Event\listen('userLogin', function ($userId, $userName) {
            // 使用 payload 中传递来的 userId, userName 参数, 处理业务 ...
            
        });

        // 用户退出登录事件
        \Guirong\Event\listen('userLogout', function ($userId, $userName) {
            // 使用 payload 中传递来的 userId, userName 参数, 处理业务 ...

        });
    }
}




$userId = 'user_01';

$userName = 'jery';

// 触发用户登录的事件
event('userLogin', [$userId, $userName]);

// 触发用户登录的事件
event('userLogout', [$userId, $userName]);

bash
composer