定制 entanet/pub-sub-laravel 二次开发

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

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

entanet/pub-sub-laravel

Composer 安装命令:

composer require entanet/pub-sub-laravel

包简介

Wrapper for laravel events so that an event can be published without using a listener and a subscriber command

README 文档

README

Pub-sub-laravel streamlines using the pub-sub pattern in Laravel. It's based on superbalist/laravel-pubsub.

Publishing to a topic overview

Pub-sub-laravel allows you to publish a message to an external queue via an event without having to add a listener to publish the message. It uses the PubSubEvent via alias/facade or a bit of tinkering with the Laravel global helper.

Subscribing to a topic overview

You can subscribe to a queue using the PubSub2ListenerCommand so that you don't need to create an event to trigger the target listeners.

Prerequisities

Make sure you've included the laravel-pubsub service provider in config/app.php:

    'providers' => [
    ...
    Superbalist\LaravelPubSub\PubSubServiceProvider::class,
    ...

Installing and setting up

Run the below in your Laravel project to install the library:

    composer require entanet/pub-sub-laravel

Registering the service providers

To register the PubSubEventProvider and PubSub2ListenerProvider add the below line into the providers array in config/app.php in Laravel:

    'providers' => [
    ...
    
    \Entanet\PubSubLaravel\PubSubEventServiceProvider::class,
    \Entanet\PubSubLaravel\PubSub2ListenerProvider::class
    
    ...

Add an alias to the PubSubEventFacade

To add an alias to the facade add the following to the alias array in config/app.php in Laravel:

    'aliases' => [
    ...
    'PubSubEvent' => \Entanet\PubSubLaravel\PubSubEventFacade::class,
    ...
    

Also you can alter the current event alias so that any current Event calls use the PubSubEvent:

Change:

    'aliases' => [
    ...
    'Event' =>  Illuminate\Support\Facades\Event::class,
    ...
    

To:

    'aliases' => [
    ...
    'Event' => \Entanet\PubSubLaravel\PubSubEventFacade::class,
    ...
    

Overriding the Laravel global helper event

Laravel comes with a global helper event which dispatches the given event so you don't have to use the facade. If you want to override that helper with the PubSubEvent you need to require the PubSubEventHelper.php file before the vendor/autoload.php. Here is an example, altering the public/index.php file in Laravel 5.6:

   // PubSubEventHelper.php is used to override the laravel global event helper.
   require __DIR__.'/../vendor/entanet/pub-sub-laravel/src/PubSubEventHelper.php';
   require __DIR__.'/../vendor/autoload.php';
     

Set up the listeners for this topic for PubSub2ListenerCommand

For each topic you are consuming add a mapping in Providers/EventServiceProvider.php

    protected $listen = [
    ...
         'test_topic' => [
            TestListener::class
        ]
   ...
   ]

Using pub-sub-laravel

Using PubSubEvent via the Alias/facade

Call dispatch from the facade and supply a relevant event containing the event data and topic (new \App\Events\PubEvent($data, 'topic_name'))

    PubSubEvent::dispatch(new \App\Events\PubEvent($data), 'topic_name'); 

Or if you have altered the existing Event alias:

    Event::dispatch(new \App\Events\PubEvent($data), 'topic_name'); 

Using PubSubEvent via event global helper

If you overridden the global helper:

    event(new \App\Events\PubEvent($data), 'topic_name');

Using a PubSub2ListenerCommand to listen to a topic

run the following artisan command appended with the topic name, in this instance the topic is test_topic:

    php artisan pubsub:consumer test_topic

Using PubSubTest

PubSubTest sets a mock of the PubSubInterface for you to use in your unit tests and also includes a handy invokeMethod which can be used to test protected/private functions. So If you had a privateMethod method in a TricksyClass you'd do something like this:

   $tricksyInstance = new TricksyClass();
   $response = $this->invokeMethod($tricksyInstance, 'privateMethod');

If it took two variables $var1 and $var2:

    $var1 = 5;
    $var2 = 'Impossible!';
    $tricksyInstance = new TricksyClass();
    $response = $this->invokeMethod($tricksyInstance, 'privateMethod', [$var1, $var2]);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固