Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Lumen support #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# laravel-event-pubsub

An event protocol and implementation over pub/sub for Laravel.
An event protocol and implementation over pub/sub for Laravel and Lumen.

[![Author](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/superbalist)
[![Build Status](https://img.shields.io/travis/Superbalist/laravel-event-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/laravel-event-pubsub)
Expand All @@ -9,15 +9,15 @@ An event protocol and implementation over pub/sub for Laravel.
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/laravel-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-event-pubsub)
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/laravel-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-event-pubsub)

This package is a wrapper bridging [php-event-pubsub](https://github.com/Superbalist/php-event-pubsub) into Laravel.
This package is a wrapper bridging [php-event-pubsub](https://github.com/Superbalist/php-event-pubsub) into Laravel and Lumen.
It builds on top of the existing [laravel-pubsub](https://github.com/Superbalist/laravel-pubsub) package adding support
for publishing and subscribing to events over pub/sub.

If you aren't familiar with the `laravel-pubsub` package, it's worth first taking a look at their [documentation](https://github.com/Superbalist/laravel-pubsub).

For **Laravel 4** support, use the package https://github.com/Superbalist/laravel4-event-pubsub

## Installation
## Laravel Installation

```bash
composer require superbalist/laravel-event-pubsub
Expand All @@ -38,7 +38,7 @@ Register the service provider in app.php
```php
'providers' => [
// ...
Superbalist\LaravelEventPubSub\PubSubEventsServiceProvider::class,
Superbalist\LaravelEventPubSub\PubSubEventsLaravelServiceProvider::class,
]
```

Expand All @@ -52,11 +52,47 @@ Register the facade in app.php

To customize the configuration file, publish the package configuration using Artisan.
```bash
php artisan vendor:publish --provider="Superbalist\LaravelEventPubSub\PubSubEventsServiceProvider"
php artisan vendor:publish --provider="Superbalist\LaravelEventPubSub\PubSubEventsLaravelServiceProvider"
```

You can then edit the generated config at `app/config/pubsub_events.php`.

## Lumen Installation

```bash
composer require superbalist/laravel-event-pubsub
```

The package has a default configuration which uses the following environment variables.
```
PUBSUB_EVENTS_CONNECTION=null
PUBSUB_EVENTS_TRANSLATOR=pubsub.events.translators.simple
PUBSUB_EVENTS_VALIDATOR=null
PUBSUB_EVENTS_THROW_VALIDATION_EXCEPTIONS_ON_DISPATCH=true
```

If the `PUBSUB_EVENTS_CONNECTION` environment variable or `pubsub_events.default` config value is left blank, the
default connection will be taken from the `laravel-pubsub` package config.

Register the service provider in app.php
```php
$app->register(Superbalist\LaravelEventPubSub\PubSubEventsLumenServiceProvider::class);
```

Register the facade in app.php
```php
if (!class_exists('PubSubEvents')) {
class_alias('Superbalist\LaravelEventPubSub\PubSubEventsFacade', 'PubSubEvents');
}
```

To customize the configuration file, copy the package configuration.
```bash
cp vendor/superbalist/laravel-event-pubsub/config/pubsub_events.php config/pubsub_events.php
```

You can then edit the generated config at `config/pubsub_events.php`.

## Usage

### Simple Events
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.0.2 - 2017-12-01

* Added Lumen support

## 3.0.1 - 2017-07-25

* Allow for superbalist/laravel-pubsub ^3.0
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"php": ">=5.6.0",
"superbalist/php-event-pubsub": "^4.0",
"superbalist/laravel-pubsub": "^2.0|^3.0",
"superbalist/laravel-pubsub": "^3.0.1",
"illuminate/support": "^5.3",
"illuminate/events": "^5.3"
},
Expand Down
178 changes: 178 additions & 0 deletions src/PubSubEventsBaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace Superbalist\LaravelEventPubSub;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use League\JsonGuard\Dereferencer;
use League\JsonGuard\Loader;
use League\JsonGuard\Loaders\ArrayLoader;
use Superbalist\EventPubSub\EventManager;
use Superbalist\EventPubSub\EventValidatorInterface;
use Superbalist\EventPubSub\MessageTranslatorInterface;
use Superbalist\EventPubSub\Translators\SchemaEventMessageTranslator;
use Superbalist\EventPubSub\Translators\SimpleEventMessageTranslator;
use Superbalist\EventPubSub\Translators\TopicEventMessageTranslator;
use Superbalist\EventPubSub\Validators\JSONSchemaEventValidator;
use Superbalist\PubSub\PubSubAdapterInterface;

class PubSubEventsBaseServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/pubsub_events.php' => config_path('pubsub_events.php'),
]);
}

/**
* Register bindings in the container.
*/
public function register()
{
$this->app->bind('pubsub.events.connection', function ($app) {
// we'll use the connection name configured in the 'default' config setting from the 'pubsub_events'
// config
// if this value isn't set, we'll default to that from the 'pubsub' package config
$config = $app['config']['pubsub_events'];
$manager = $app['pubsub'];
return $manager->connection($config['default']);
});

$this->app->bind('pubsub.events.translator', MessageTranslatorInterface::class);

$this->app->bind(MessageTranslatorInterface::class, function ($app) {
$config = $app['config']['pubsub_events'];
$binding = $config['translator'];
return $app[$binding];
});

$this->app->bind('pubsub.events.validator', EventValidatorInterface::class);

$this->app->bind(EventValidatorInterface::class, function ($app) {
$config = $app['config']['pubsub_events'];
$binding = $config['validator'];
// a validator is optional
// if nothing is set, we don't try resolve it
return $binding === null ? null : $app[$binding];
});

$this->registerTranslators();
$this->registerValidators();

$this->app->bind('pubsub.events', EventManager::class);

$this->app->bind(EventManager::class, function ($app) {
$adapter = $app['pubsub.events.connection']; /** @var PubSubAdapterInterface $connection */
$translator = $app['pubsub.events.translator']; /** @var MessageTranslatorInterface $translator */
$validator = $app['pubsub.events.validator']; /** @var EventValidatorInterface $validator */
$injectors = [];
$config = $app['config']['pubsub_events'];
foreach ($config['attribute_injectors'] as $binding) {
if (is_callable($binding)) {
$injectors[] = $binding;
} else {
// resolve binding from container
$injectors[] = $app[$binding];
}
}

$manager = new EventManager(
$adapter,
$translator,
$validator,
$injectors,
$config['translate_fail_handler'],
$config['listen_expr_fail_handler'],
$config['validation_fail_handler']
);
$manager->throwValidationExceptionsOnDispatch($config['throw_validation_exceptions_on_dispatch']);

return $manager;
});
}

/**
* Register translators in the container.
*/
protected function registerTranslators()
{
$this->app->bind('pubsub.events.translators.simple', function () {
return new SimpleEventMessageTranslator();
});

$this->app->bind('pubsub.events.translators.topic', function () {
return new TopicEventMessageTranslator();
});

$this->app->bind('pubsub.events.translators.schema', function () {
return new SchemaEventMessageTranslator();
});
}

/**
* Register validators in the container.
*/
protected function registerValidators()
{
$this->app->singleton('pubsub.events.validators.json_schema.loaders.array.schemas', function ($app) {
$config = $app['config']['pubsub_events'];
$schemas = $config['validators']['json_schema']['loaders']['array']['schemas'];
return collect($schemas);
});

$this->app->bind('pubsub.events.validators.json_schema.loaders.array', function ($app) {
$schemas = $app['pubsub.events.validators.json_schema.loaders.array.schemas']; /* @var Collection $schemas */
return new ArrayLoader($schemas->all());
});

$this->app->bind('pubsub.events.validators.json_schema', function ($app) {
$dereferencer = $app['pubsub.events.validators.json_schema.dereferencer']; /* @var Dereferencer $dereferencer */
return new JSONSchemaEventValidator($dereferencer);
});

$this->app->bind('pubsub.events.validators.json_schema.dereferencer', function ($app) {
$dereferencer = new Dereferencer();

$config = $app['config']['pubsub_events'];

foreach ($config['validators']['json_schema']['loaders'] as $name => $params) {
$name = array_get($params, 'binding', $name);
$binding = sprintf('pubsub.events.validators.json_schema.loaders.%s', $name);

$prefix = array_get($params, 'prefix', $name);

$loader = $app[$binding]; /* @var Loader $loader */

$dereferencer->registerLoader($loader, $prefix);
}

return $dereferencer;
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'pubsub.events',
'pubsub.events.connection',
'pubsub.events.translator',
'pubsub.events.translators.simple',
'pubsub.events.translators.topic',
'pubsub.events.translators.schema',
'pubsub.events.validator',
'pubsub.events.validators.json_schema',
'pubsub.events.validators.json_schema.dereferencer',
'pubsub.events.validators.json_schema.loaders.array',
'pubsub.events.validators.json_schema.loaders.array.schemas',
];
}
}
19 changes: 19 additions & 0 deletions src/PubSubEventsLaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Superbalist\LaravelEventPubSub;

use Superbalist\LaravelPubSub\PubSubLaravelServiceProvider;

class PubSubEventsLaravelServiceProvider extends PubSubEventsBaseServiceProvider
{
/**
* Register bindings in the container.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/pubsub_events.php', 'pubsub_events');
$this->app->register(PubSubLaravelServiceProvider::class);

Parent::register();
}
}
27 changes: 27 additions & 0 deletions src/PubSubEventsLumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Superbalist\LaravelEventPubSub;

use Superbalist\LaravelPubSub\PubSubLumenServiceProvider;

class PubSubEventsLumenServiceProvider extends PubSubEventsBaseServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
$this->app->configure('pubsub_events');
}

/**
* Register bindings in the container.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/pubsub_events.php', 'pubsub_events');
$this->app->register(PubSubLumenServiceProvider::class);

Parent::register();
}
}
Loading