Skip to content

Commit

Permalink
Make rabbitevents:install command (#41)
Browse files Browse the repository at this point in the history
* Make `rabbitevents:install` command
  • Loading branch information
masterjus authored Jan 3, 2020
1 parent fff7b77 commit d446744
Show file tree
Hide file tree
Showing 16 changed files with 352 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ language: php
php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'

install:
- sudo add-apt-repository ppa:ondrej/php -y
Expand Down
23 changes: 19 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"name": "nuwber/rabbitevents",
"description": "Laravel back-to-back broadcasting events. It uses RabbitMQ as the transport.",
"keywords": ["queue", "laravel", "rabbitmq", "events", "event bus", "bus"],
"keywords": [
"queue",
"laravel",
"rabbitmq",
"events",
"event bus",
"bus"
],
"type": "library",
"license": "MIT",
"authors": [
Expand All @@ -15,14 +22,15 @@
"ext-bcmath": "*",
"ext-pcntl": "*",
"ext-json": "*",
"illuminate/events": "^5.5|^6.0",
"illuminate/queue": "^5.5|^6.0",
"illuminate/events": "~5.7.0|~5.8.0|^6.0",
"illuminate/queue": "~5.7.0|~5.8.0|^6.0",
"illuminate/support": "~5.7.0|~5.8.0|^6.0",
"enqueue/amqp-lib": "^0.9.12",
"queue-interop/amqp-interop": "^0.8.1"
},
"require-dev": {
"mockery/mockery": "^1.2",
"phpunit/phpunit": "^7"
"phpunit/phpunit": "^7.0|^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -32,6 +40,13 @@
"src/helpers.php"
]
},
"extra": {
"laravel": {
"providers": [
"Nuwber\\Events\\RabbitEventsServiceProvider"
]
}
},
"autoload-dev": {
"psr-4": {
"Nuwber\\Events\\Tests\\": "tests/"
Expand Down
34 changes: 34 additions & 0 deletions config/rabbitevents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| RabbitEvents Connection Configuration
|--------------------------------------------------------------------------
|
| Here you may define the RabbitMQ connection settings that should be used
| by RabbitEvents. Note that `vhost` should be created by you manually.
| @see https://www.rabbitmq.com/vhosts.html for more info
|
*/

[
'default' => env('RABBITEVENTS_CONNECTION', 'rabbitmq'),
'connections' => [
'rabbitmq' => [
'driver' => 'rabbitmq',
'exchange' => env('RABBITEVENTS_EXCHANGE', 'events'),
'host' => env('RABBITEVENTS_HOST', 'localhost'),
'port' => env('RABBITEVENTS_PORT', 5672),
'user' => env('RABBITEVENTS_USER', 'guest'),
'pass' => env('RABBITEVENTS_PASSWORD', 'guest'),
'vhost' => env('RABBITEVENTS_VHOST', 'events'),
'logging' => [
'enabled' => env('RABBITEVENTS_LOG_ENABLED', false),
'level' => env('RABBITEVENTS_LOG_LEVEL', 'info'),
],
],
],
],
];
85 changes: 45 additions & 40 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
# Table of contents
1. [Introduction](#introduction)
2. [Installation](#installation)
- [Register seervice providers](#register-sp)
- [Configure RabbitMQ connection](#rabbitmq-connection-config)
- [Configuration](#configuration)
3. [Events & Listeners](#events-listeners)
- [Register a Listener](#register-regular-listener)
- [Wildcard Listeners](#register-wildcard-listeners)
Expand All @@ -25,60 +24,64 @@

## Introduction <a name="introduction"></a>

Nuwber's broadcasting events provides a simple observer implementation, allowing you to listen for various events that occur in your current and another applications. For example if you need to react to some event fired from another API.
Nuwber's broadcasting events provides a simple observer implementation, allowing you to listen for various events that occur in your current and another applications. For example if you need to react to some event published from another API.

Do not confuse this package with Laravel's broadcast. This package was made to communicate in backend to backend way.

Generally, this is compilation of Laravel's [events](https://laravel.com/docs/events) and [queues](https://laravel.com/docs/queues).

Listener classes are typically stored in the `app/Listeners` folder. You may use Laravel's artisan command to generate them as it described in the [official documentation](https://laravel.com/docs/events).


All RabbitMQ calls are done by using [Laravel queue package](https://github.com/php-enqueue/laravel-queue). So for better understanding read their documentation first.
All RabbitMQ calls are done by using [Laravel queue package](https://github.com/php-enqueue/laravel-queue) as an example. So for better understanding read their documentation first.

## Installation <a name="installation"></a>
Add this library to your composer.json
You may use Composer to install RabbitEvents into your Laravel project:

```bash
$ composer require nuwber/rabbitevents
```

### Register service providers <a name="register-sp"></a>
After installing RabbitEvents, publish its config and a service provider using the `rabbitevents:install` Artisan command:

First of all you need to create a service provider which is extends `Nuwber\Events\BroadcastEventServiceProvider`
and register it in your `config/app.php` in `providers` section.
```bash
$ php artisan rabbitevents:install
```

### Configure RabbitMQ connection <a name="rabbitmq-connection-config"></a>
### Configuration <a name="configuration"></a>
After publishing assets, its primary configuration file will be located at `config/rabbitevents.php`.
This configuration file allows you to configure your connection and logging options.

The library uses internal Laravel's queue system. To configure connection you should make changes in `config/queue.php`:
It's very similar to queue connection but now you'll never be confused if you have another connection to RabbitMQ.

```php
<?php
[
'default' => env('QUEUE_CONNECTION', 'rabbitmq'),
'connections' => [
'rabbitmq' => [
'driver' => 'rabbitmq',
'exchange' => env('RABBITMQ_EXCHENGE', 'events'),
'host' => env('RABBITMQ_HOST', 'localhost'),
'port' => env('RABBITMQ_PORT', 5672),
'user' => env('RABBITMQ_USER', 'guest'),
'pass' => env('RABBITMQ_PASSWORD', 'guest'),
'vhost' => env('RABBITMQ_VHOST', 'events'),
'logging' => [
'enabled' => env('RABBITEVENTS_LOG_ENABLED', false),
'level' => env('RABBITEVENTS_LOG_LEVEL', 'info'),
]
return [
[
'default' => env('RABBITEVENTS_CONNECTION', 'rabbitmq'),
'connections' => [
'rabbitmq' => [
'driver' => 'rabbitmq',
'exchange' => env('RABBITEVENTS_EXCHANGE', 'events'),
'host' => env('RABBITEVENTS_HOST', 'localhost'),
'port' => env('RABBITEVENTS_PORT', 5672),
'user' => env('RABBITEVENTS_USER', 'guest'),
'pass' => env('RABBITEVENTS_PASSWORD', 'guest'),
'vhost' => env('RABBITEVENTS_VHOST', 'events'),
'logging' => [
'enabled' => env('RABBITEVENTS_LOG_ENABLED', false),
'level' => env('RABBITEVENTS_LOG_LEVEL', 'info'),
],
],
],
],
]
];
```

## Events & Listeners <a name="events-listeners"></a>

### Register a Listener <a name="register-regular-listener"></a>

The `listen` property of `BroadcastEventServiceProvider` contains an array of all events (keys) and their listeners (values). Of course, you may add as many events to this array as your application requires.
The `listen` property of `RabbitEventsServiceProvider` contains an array of all events (keys) and their listeners (values). Of course, you may add as many events to this array as your application requires.

```php
<?php
Expand All @@ -90,6 +93,7 @@ The `listen` property of `BroadcastEventServiceProvider` contains an array of al
protected $listen = [
'item.created' => [
'App\Listeners\SendItemCreatedNotification',
'App\Listeners\ChangeUserRole',
],
];
```
Expand Down Expand Up @@ -189,6 +193,7 @@ This is the example how to publish your event and payload:
$model = new SomeModel(['name' => 'Jonh Doe', 'email' => 'email@example.com']);
$someArray = ['key' => 'item'];
$someString = 'Hello!';

// Example 1. Old way to publish your data. Will be deprecated it next versions.
// Remember: You MUST pass array of arguments
fire('something.happened', [$model->toArray(), $someArray, $someString]);
Expand Down Expand Up @@ -248,13 +253,13 @@ $ php artisan rabbitevents:list

### Command `rabbitevents:make:observer` <a name='command-make-observer'></a>

Sometimes you may with to send a broadcast event to each change of a model. Observers classes have method names which reflect the Eloquent events you wish to listen for. Each of these methods receives the model as their only argument. The difference from Laravel's command is that `rabbitevents:make:observer` creates an observer class with stubbed `fire` function call in each method.
Sometimes you may with to publish an event to each change of a model. Observers classes have method names which reflect the Eloquent events you wish to listen for. Each of these methods receives the model as their only argument. The difference from Laravel's command is that `rabbitevents:make:observer` creates an observer class with stubbed `fire` function call in each method.

```bash
$ php artisan rabbitevents:make:observer UserObserver --model=User
```

This command will place the new observer in your App/Observers directory. If this directory does not exist, Artisan will create it for you. Your fresh observer will look like the following:
This command will place the new observer in your `App/Observers` directory. If this directory does not exist, Artisan will create it for you. Your fresh observer will look like the following:

```php
<?php
Expand All @@ -273,7 +278,7 @@ class UserObserver
*/
public function created(User $user)
{
fire('User.created', [$user->toArray()]);
publish('User.created', [$user->toArray()]);
}

/**
Expand All @@ -284,7 +289,7 @@ class UserObserver
*/
public function updated(User $user)
{
fire('User.updated', [$user->toArray()]);
publish('User.updated', [$user->toArray()]);
}

/**
Expand All @@ -295,7 +300,7 @@ class UserObserver
*/
public function deleted(User $user)
{
fire('User.deleted', [$user->toArray()]);
publish('User.deleted', [$user->toArray()]);
}

/**
Expand All @@ -306,7 +311,7 @@ class UserObserver
*/
public function restored(User $user)
{
fire('User.restored', [$user->toArray()]);
publish('User.restored', [$user->toArray()]);
}

/**
Expand All @@ -317,7 +322,7 @@ class UserObserver
*/
public function forceDeleted(User $user)
{
fire('User.forceDeleted', [$user->toArray()]);
publish('User.forceDeleted', [$user->toArray()]);
}
}
```
Expand Down Expand Up @@ -361,7 +366,7 @@ class AppServiceProvider extends ServiceProvider

The package provides 2 ways to see what happens on your listener. By default it writes `processing`, `processed` and `failed` messages to php output. Message includes service, event and listener name. If you want to turn this feature off, just run listener with `--quiet` option.

The package also supports your application logger. To use it set config value `connection.rabbitmq.logging.enabled` to `true` and choose log level.
The package also supports your application logger. To use it set config value `rabbitevents.connection.rabbitmq.logging.enabled` to `true` and choose log level.

## Handling Examples <a name=examples></a>
### Single event
Expand All @@ -380,15 +385,15 @@ class UserAuthenticated
}
```

**app/Providers/BroadcastEventServiceProvider.php**
**app/Providers/RabbitEventsServiceProvider.php**
```php
<?php

namespace App\Providers;

use App\Listeners\UserAuthenticated;

class BroadcastEventServiceProvider extends \Nuwber\Events\BroadcastEventServiceProvider
class RabbitEventsServiceProvider extends \Nuwber\Events\RabbitEventsServiceProvider
{
protected $listen = [
'user.authenticated' => [
Expand Down Expand Up @@ -419,15 +424,15 @@ class UserAuthenticated
}
```

**app/Providers/BroadcastEventServiceProvider.php**
**app/Providers/RabbitEventsServiceProvider.php**
```php
<?php

namespace App\Providers;

use App\Listeners\UserAuthenticated;

class BroadcastEventServiceProvider extends \Nuwber\Events\BroadcastEventServiceProvider
class RabbitEventsServiceProvider extends \Nuwber\Events\RabbitEventsServiceProvider
{
protected $listen = [
'user.*' => [
Expand Down
Loading

0 comments on commit d446744

Please sign in to comment.