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

Commit

Permalink
integrate new php-event-pubsub version (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgoslett authored Jul 17, 2017
1 parent 07aba29 commit ed023cc
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 13 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ php:
- 5.6
- 7.0
- 7.1
- hhvm
- nightly

before_script:
Expand Down
47 changes: 39 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@ The package has a default configuration which uses the following environment var
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.

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

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

Register the service provider in app.php
```php
'providers' => [
Expand All @@ -56,6 +50,13 @@ 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"
```

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

## Usage

### Simple Events
Expand Down Expand Up @@ -196,4 +197,34 @@ $event = new \Superbalist\EventPubSub\Events\SchemaEvent(
$manager->dispatch('events', $event);

// the listen expressions are the same as those used for TopicEvents.
```
```

## Error Handling

The library supports error handlers for when event translation fails, listen expression fails and validation fails.

These are configurable as callables in the translate_fail_handler, listen_expr_fail_handler and validation_fail_handler
config options.

The config contains default callables which will turn the callbacks into Laravel events.

You can register listeners in the EventServiceProvider for the following events:

```
\Superbalist\LaravelEventPubSub\Events\TranslationFailureEvent(
$message
)
```

```
\Superbalist\LaravelEventPubSub\Events\ListenExprFailureEvent(
\Superbalist\EventPubSub\EventInterface $event,
$expr
)
```

```
\Superbalist\LaravelEventPubSub\Events\ValidationFailureEvent(
\Superbalist\EventPubSub\ValidationResult $result
)
```
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 3.0.0 - 2017-07-17

* Bump up to superbalist/php-event-pubsub ^4.0
* Add container binding for EventManager::class
* Change 'pubsub.events' binding to no longer be a singleton and to alias to EventManager::class
* Add new 'throw_validation_exceptions_on_dispatch' config option and PUBSUB_EVENTS_THROW_VALIDATION_EXCEPTIONS_ON_DISPATCH env var
* Add new 'translate_fail_handler' config option and default callable to dispatch a TranslationFailureEvent event
* Add new 'listen_expr_fail_handler' config option and default callable to dispatch a ListenExprFailureEvent event
* Add new 'validation_fail_handler' config option and default callable to dispatch a ValidationFailureEvent event

## 2.0.1 - 2017-05-16

* Allow for php-event-pubsub ^3.0
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
],
"require": {
"php": ">=5.6.0",
"superbalist/php-event-pubsub": "^2.0|^3.0",
"superbalist/php-event-pubsub": "^4.0",
"superbalist/laravel-pubsub": "^2.0",
"illuminate/support": "^5.3"
"illuminate/support": "^5.3",
"illuminate/events": "^5.3"
},
"autoload": {
"psr-4": {
Expand Down
59 changes: 59 additions & 0 deletions config/pubsub_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,63 @@ function () {

],

/*
|--------------------------------------------------------------------------
| Throw Validation Exceptions on Dispatch?
|--------------------------------------------------------------------------
|
| If true, and a validator is set, if an event fails validation on dispatch,
| a ValidationException will be thrown.
|
*/

'throw_validation_exceptions_on_dispatch' => env('PUBSUB_EVENTS_THROW_VALIDATION_EXCEPTIONS_ON_DISPATCH', true),

/*
|--------------------------------------------------------------------------
| Translate Fail Handler
|--------------------------------------------------------------------------
|
| The translate fail handler is a callable which is called when a message
| is received but fails to translate to an event.
|
*/

'translate_fail_handler' => function ($message) {
$dispatcher = app('events'); /* @var \Illuminate\Events\Dispatcher $dispatcher */
$dispatcher->fire(new \Superbalist\LaravelEventPubSub\Events\TranslationFailureEvent($message));
},

/*
|--------------------------------------------------------------------------
| Listen Expr Fail Handler
|--------------------------------------------------------------------------
|
| The listen expr fail handler is a callable which is called when an event
| is received but doesn't match the listen expression.
|
| This isn't really an error, but can be useful for debugging.
|
*/

'listen_expr_fail_handler' => function (\Superbalist\EventPubSub\EventInterface $event, $expr) {
$dispatcher = app('events'); /* @var \Illuminate\Events\Dispatcher $dispatcher */
$dispatcher->fire(new \Superbalist\LaravelEventPubSub\Events\ListenExprFailureEvent($event, $expr));
},

/*
|--------------------------------------------------------------------------
| Validation Fail Handler
|--------------------------------------------------------------------------
|
| The validation handler is a callable which is called when an event
| is dispatched or received but fails validation.
|
*/

'validation_fail_handler' => function (\Superbalist\EventPubSub\ValidationResult $result) {
$dispatcher = app('events'); /* @var \Illuminate\Events\Dispatcher $dispatcher */
$dispatcher->fire(new \Superbalist\LaravelEventPubSub\Events\ValidationFailureEvent($result));
},

];
28 changes: 28 additions & 0 deletions src/Events/ListenExprFailureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Superbalist\LaravelEventPubSub\Events;

use Superbalist\EventPubSub\EventInterface;

class ListenExprFailureEvent
{
/**
* @var EventInterface
*/
public $event;

/**
* @var
*/
public $expr;

/**
* @param EventInterface $event
* @param string $expr
*/
public function __construct(EventInterface $event, $expr)
{
$this->event = $event;
$this->expr = $expr;
}
}
19 changes: 19 additions & 0 deletions src/Events/TranslationFailureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Superbalist\LaravelEventPubSub\Events;

class TranslationFailureEvent
{
/**
* @var string
*/
public $message;

/**
* @param string $message
*/
public function __construct($message)
{
$this->message = $message;
}
}
21 changes: 21 additions & 0 deletions src/Events/ValidationFailureEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Superbalist\LaravelEventPubSub\Events;

use Superbalist\EventPubSub\ValidationResult;

class ValidationFailureEvent
{
/**
* @var ValidationResult
*/
public $result;

/**
* @param ValidationResult $result
*/
public function __construct(ValidationResult $result)
{
$this->result = $result;
}
}
17 changes: 15 additions & 2 deletions src/PubSubEventsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public function register()
$this->registerTranslators();
$this->registerValidators();

$this->app->singleton('pubsub.events', function ($app) {
$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 */
Expand All @@ -84,7 +86,18 @@ public function register()
}
}

return new EventManager($adapter, $translator, $validator, $injectors);
$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;
});
}

Expand Down

0 comments on commit ed023cc

Please sign in to comment.