diff --git a/README.md b/README.md index f7bf3eb..72cbfe4 100644 --- a/README.md +++ b/README.md @@ -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/author-@superbalist-blue.svg?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) @@ -9,7 +9,7 @@ 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. @@ -17,7 +17,7 @@ If you aren't familiar with the `laravel-pubsub` package, it's worth first takin For **Laravel 4** support, use the package https://github.com/Superbalist/laravel4-event-pubsub -## Installation +## Laravel Installation ```bash composer require superbalist/laravel-event-pubsub @@ -38,7 +38,7 @@ Register the service provider in app.php ```php 'providers' => [ // ... - Superbalist\LaravelEventPubSub\PubSubEventsServiceProvider::class, + Superbalist\LaravelEventPubSub\PubSubEventsLaravelServiceProvider::class, ] ``` @@ -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 diff --git a/changelog.md b/changelog.md index 3b337a3..0d62850 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/composer.json b/composer.json index 642c8fa..e981829 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/PubSubEventsBaseServiceProvider.php b/src/PubSubEventsBaseServiceProvider.php new file mode 100644 index 0000000..6bbd3ac --- /dev/null +++ b/src/PubSubEventsBaseServiceProvider.php @@ -0,0 +1,178 @@ +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', + ]; + } +} diff --git a/src/PubSubEventsLaravelServiceProvider.php b/src/PubSubEventsLaravelServiceProvider.php new file mode 100644 index 0000000..0d94ed9 --- /dev/null +++ b/src/PubSubEventsLaravelServiceProvider.php @@ -0,0 +1,19 @@ +mergeConfigFrom(__DIR__ . '/../config/pubsub_events.php', 'pubsub_events'); + $this->app->register(PubSubLaravelServiceProvider::class); + + Parent::register(); + } +} diff --git a/src/PubSubEventsLumenServiceProvider.php b/src/PubSubEventsLumenServiceProvider.php new file mode 100644 index 0000000..f349fed --- /dev/null +++ b/src/PubSubEventsLumenServiceProvider.php @@ -0,0 +1,27 @@ +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(); + } +} diff --git a/src/PubSubEventsServiceProvider.php b/src/PubSubEventsServiceProvider.php index ffaa55b..557db5f 100644 --- a/src/PubSubEventsServiceProvider.php +++ b/src/PubSubEventsServiceProvider.php @@ -2,183 +2,7 @@ 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\LaravelPubSub\PubSubManager; -use Superbalist\LaravelPubSub\PubSubServiceProvider; -use Superbalist\PubSub\PubSubAdapterInterface; - -class PubSubEventsServiceProvider extends ServiceProvider +class PubSubEventsServiceProvider extends PubSubEventsLaravelServiceProvider { - /** - * 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->mergeConfigFrom(__DIR__ . '/../config/pubsub_events.php', 'pubsub_events'); - - $this->app->register(PubSubServiceProvider::class); - - $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']; /* @var PubSubManager $manager */ - 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', - ]; - } + // kept for backwards compatibility }