定制 numesia/laravel-events 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

numesia/laravel-events

最新稳定版本:0.0.13

Composer 安装命令:

composer require numesia/laravel-events

包简介

Laravel package that helps to send events across components

README 文档

README

Laravel package that helps to send events across components

This laravel event package is a private package so we can't just require it using composer, that's why we have to add a vcs repository to tell composer from which url the package must be loaded.

"repositories": [
    {
        "type": "vcs",
        "url":  "git@github.com:NUMESIA/NU_EVENT_LaravelPackage.git"
    }
],
"require": {
    "NUMESIA/laravel-events": "0.0.*"
},

Once this has finished, you will need to add the service provider to the providers array in your app.php config as follows:

Numesia\NuEvent\Providers\NuEventServiceProvider::class,

Next, also in the app.php config file, under the aliases array, you may want to add NuEvent facades.

'NuEvent' => Numesia\NuEvent\Facades\NuEvent::class,

Then publish configs

php artisan vendor:publish --provider="Numesia\NuEvent\Providers\NuEventServiceProvider" --tag=config

Finally, you will want to change your NUEVENT_TOKEN key from .env file:

NUEVENT_TOKEN=YourEventSecretKey

How to use ?

NuEvent comes with a global function nuEvent() which help you te send easily events to other components

    /**
     * Send an event to components
     *
     * @param      <string>     $eventName   The event name
     * @param      <string>     $eventData   The event data
     * @param      <mixed>      $components  The components
     * @param      <closure>    $callback    The callback
     */
    function nuEvent($eventName, $eventData, $components = null, $callback)

$components can be a string (bpm), an array ['bpm', 'cms'] or nothing to broadcast to all components

You can Also use the Facade NuEvent if you want to perform the same task

/**
 * Emit event to component name
 *
 * @param      <string>  $componentName  The component name
 * @param      <string>  $eventName      The event name
 * @param      <string>  $eventData      The event data
 */
NuEvent::emit($componentName, $eventName, $eventData)

/**
 * Broadcast event to all components
 *
 * @param      <string>  $eventName   The event name
 * @param      <string>  $eventData   The event data
 */
NuEvent::broadcast($eventName, $eventData)

/**
 * Dispatch event to selected components
 *
 * @param      <string>  $eventName   The event name
 * @param      <string>  $eventData   The event data
 * @param      <mixed>   $components  The components
 */
NuEvent::dispatch($eventName, $eventData, $components = null)

/!\ When you send an event, NuEvent add automatically a suffix 'nuEvents:' to your event name

Example

In this example we will try to send an event from book to bpm

BOOK Component

All we have to do is adding this command somewhere

nuEvent('book.ping', "Hello World", "bpm");

BPM Component

We should create a listener (don't forget to add it into your EventServiceProvider)

  • EventServiceProvider
//EventServiceProvider
protected $subscribe = [
    'App\Listeners\NuEventSubscriber',
];
  • NuEventSubscriber
// App\Listeners\NuEventSubscriber
<?php

namespace App\Listeners;

use App\Jobs\SendNotification;

class NuEventSubscriber
{
    /**
     * Listening ActivityWasAborted event
     */
    public function onBookPing($event)
    {
        \Log::info($event);
    }

    /**
     * Register listeners for the subscriber
     *
     * @param Illuminate\Events\Dispatcher $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'nuEvents:book.ping',
            'App\Listeners\NuEventSubscriber@onBookPing'
        );
    }
}

统计信息

  • 总下载量: 828
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-03