国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

webman 事件庫(kù) webman-event

Tinywan

webman 事件庫(kù) webman-event

事件相比較中間件的優(yōu)勢(shì)是事件比中間件更加精準(zhǔn)定位(或者說(shuō)粒度更細(xì)),并且更適合一些業(yè)務(wù)場(chǎng)景的擴(kuò)展。

例如,我們通常會(huì)遇到用戶注冊(cè)或者登錄后需要做一系列操作,通過(guò)事件系統(tǒng)可以做到不侵入原有代碼完成登錄的操作擴(kuò)展,降低系統(tǒng)的耦合性的同時(shí),也降低了BUG的可能性。

項(xiàng)目地址

https://github.com/Tinywan/webman-event

安裝

composer require tinywan/webman-event

配置

事件配置文件 config/event.php

return [
    // 事件監(jiān)聽(tīng)
    'listener'    => [],

    // 事件訂閱器
    'subscriber' => [],
];

進(jìn)程啟動(dòng)配置

打開(kāi) config/bootstrap.php,加入如下配置:

return [
    // 這里省略了其它配置 ...
    webman\event\EventManager::class,
];

快速開(kāi)始

定義事件

事件類 LogErrorWriteEvent.php

declare(strict_types=1);

namespace extend\event;

use Symfony\Contracts\EventDispatcher\Event;

class LogErrorWriteEvent extends Event
{
    const NAME = 'log.error.write';  // 事件名,事件的唯一標(biāo)識(shí)

    /** @var array */
    public array $log;

    public function __construct(array $log)
    {
        $this->log = $log;
    }

    public function handle()
    {
        return $this->log;
    }
}

監(jiān)聽(tīng)事件

return [
    // 事件監(jiān)聽(tīng)
    'listener'    => [
        \extend\event\LogErrorWriteEvent::NAME  => \extend\event\LogErrorWriteEvent::class,
    ],
];

訂閱事件

訂閱類 LoggerSubscriber.php

namespace extend\event\subscriber;

use extend\event\LogErrorWriteEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LoggerSubscriber implements EventSubscriberInterface
{
    /**
     * @desc: 方法描述
     * @return array|string[]
     */
    public static function getSubscribedEvents()
    {
        return [
            LogErrorWriteEvent::NAME => 'onLogErrorWrite',
        ];
    }

    /**
     * @desc: 觸發(fā)事件
     * @param LogErrorWriteEvent $event
     */
    public function onLogErrorWrite(LogErrorWriteEvent $event)
    {
        // 一些具體的業(yè)務(wù)邏輯
        var_dump($event->handle());
    }
}

事件訂閱

return [
    // 事件訂閱
    'subscriber' => [
        \extend\event\subscriber\LoggerSubscriber::class,
    ],
];

事件觸發(fā)器

觸發(fā) LogErrorWriteEvent 事件。

$error = [
    'errorMessage' => '錯(cuò)誤消息',
    'errorCode' => 500
];
EventManager::trigger(new LogErrorWriteEvent($error),LogErrorWriteEvent::NAME);

執(zhí)行結(jié)果

打印結(jié)果

License

This project is licensed under the Apache 2.0 license.

4153 4 0
4個(gè)評(píng)論

walkor

感謝分享。
項(xiàng)目地址好像錯(cuò)了

小杰

老大,大佬,事件非常好用易用,要是再有個(gè)可以開(kāi)發(fā)插件用hook就齊全了。

  • 暫無(wú)評(píng)論
yzh52521

我使用的laravel

namespace support;

use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

/**
 *  class Event
 * @package support
 *
 *  Strings methods
 * @method static \Illuminate\Events\Dispatcher dispatch($event)
 */
class Event
{
    /**
     * @var Dispatcher
     */
    protected static $instance=null;

    /**
     * @return Dispatcher|null
     */
    public static function instance()
    {
        if (!static::$instance) {
            $container        = new Container;
            static::$instance = new Dispatcher($container);
            $eventsList       = config('events');
            if (isset($eventsList['listener']) && !empty($eventsList['listener'])) {
                foreach ($eventsList['listener'] as $event => $listener) {
                    if (is_string($listener)) {
                        $listener = implode(',', $listener);
                    }
                    foreach ($listener as $l) {
                        static::$instance->listen($event, $l);
                    }
                }
            }
            if (isset($eventsList['subscribe']) && !empty($eventsList['subscribe'])) {
                foreach ($eventsList['subscribe'] as  $subscribe) {
                    static::$instance->subscribe($subscribe);
                }
            }
        }
        return static::$instance;
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        return self::instance()->{$name}(... $arguments);
    }

}

配置
config/events.php

return [
    'listener'  => [
        app\events\Test::class => [
            \app\listeners\TestListeners::class,
        ],
    ],
    'subscribe' => [
        \app\subscribes\TestSubscribe::class,
    ],
];

事件類:Test

namespace app\events;

class Test
{
    public  $data = [];

    public function __construct($data)
    {
        $this->data = $data;
    }
}

監(jiān)聽(tīng)類

namespace app\listeners;

use app\events\Test;

class TestListeners
{
    public function __construct()
    {
    }

    /**
     * 處理事件
     * @return void
     */
    public function handle(Test $event)
    {
        // 控制臺(tái)打印
        var_dump('listener');
        var_dump($event->data);
    }
}

訂閱類

namespace app\subscribes;

use app\events\Test;

class TestSubscribe
{
    public function handleTest(Test $event)
    {
        var_dump('subscribe');
        var_dump($event);
    }

    public function subscribe($events)
    {
        $events->listen(
            Test::class,
            [TestSubscribe::class, 'handleTest']
        );
    }

helpers.php 增加

/**
 * 事件
 * @param $event
 */
function event($event)
{
    Event::dispatch($event);
}

調(diào)用觸發(fā)事件

event(new Test('event data'));

  • 小杰 2021-12-20

    這個(gè)也容易,還有定義助手函數(shù)

  • Tinywan 2021-12-20

    我不用 laravel 的,向symfony看齊

  • 大好時(shí)光 2022-02-21

    使用這個(gè)出現(xiàn)A facade root has not been set.

= - =

在 eloquent model 里面已經(jīng)有集成 laravel 的 event dispatcher,可以通過(guò) model::created(closure $func) 監(jiān)聽(tīng),是否可以將 event 的集成方式也加入 webman 官方文檔呢?

  • Tinywan 2022-01-31

    webman 只做最基礎(chǔ)的

  • = - = 2022-03-17

    棒,可以提交到插件中心嗎

  • yzh52521 2022-03-17

    已提交 在審核中

  • = - = 2022-03-17

    好的,感謝大佬貢獻(xiàn)

  • = - = 2022-03-17

    @yzh52521 為啥事件沒(méi)有基于 illuminate/events:^9.0 。看到版本鎖定為 ^8.0,無(wú)法使用最新的。

    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - Root composer.json requires yzh52521/webman-event ^1.0 -> satisfiable by yzh52521/webman-event[1.0.0].
        - yzh52521/webman-event 1.0.0 requires illuminate/events ^8.0 -> found illuminate/events[v8.0.0, ..., v8.83.5] but it conflicts with your root composer.json require (^9.5).
    
    Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
    You can also try re-running composer require with an explicit version constraint, e.g. "composer require yzh52521/webman-event:*" to figure out if any version is installable, or "composer require yzh52521/webman-event:^2.1" if you know which you need.
    
    Installation failed, reverting ./composer.json and ./composer.lock to their original content.
  • = - = 2022-03-17

    http://m.wtbis.cn/plugin/28 @yzh52521 我包想降個(gè)版本安裝你的包,還要降低 php 8.0 版本到 7.3 版本。┭┮﹏┭┮

  • yzh52521 2022-03-18

    php8.0是可以使用的 現(xiàn)在已經(jīng)兼容php8.1 illuminate/events 9.X

  • = - = 2022-03-18

    好的。謝謝大佬

年代過(guò)于久遠(yuǎn),無(wú)法發(fā)表評(píng)論

Tinywan

13420
積分
0
獲贊數(shù)
0
粉絲數(shù)
2020-01-14 加入
??