|
| 1 | +# laravel4-event-pubsub |
| 2 | + |
| 3 | +An event protocol and implementation over pub/sub for Laravel 4. |
| 4 | + |
| 5 | +[](https://twitter.com/superbalist) |
| 6 | +[](https://travis-ci.org/Superbalist/laravel4-event-pubsub) |
| 7 | +[](LICENSE) |
| 8 | +[](https://packagist.org/packages/superbalist/laravel4-event-pubsub) |
| 9 | +[](https://packagist.org/packages/superbalist/laravel4-event-pubsub) |
| 10 | + |
| 11 | +This package is a wrapper bridging [php-event-pubsub](https://github.com/Superbalist/php-event-pubsub) into Laravel. |
| 12 | +It builds on top of the existing [laravel4-pubsub](https://github.com/Superbalist/laravel4-pubsub) package adding support |
| 13 | +for publishing and subscribing to events over pub/sub. |
| 14 | + |
| 15 | +If you aren't familiar with the `laravel4-pubsub` package, it's worth first taking a look at their [documentation](https://github.com/Superbalist/laravel4-pubsub). |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +composer require superbalist/laravel4-event-pubsub |
| 21 | +``` |
| 22 | + |
| 23 | +The package has a default configuration built-in. |
| 24 | + |
| 25 | +To customize the configuration file, publish the package configuration using Artisan. |
| 26 | +```bash |
| 27 | +php artisan config:publish superbalist/laravel4-event-pubsub |
| 28 | +``` |
| 29 | + |
| 30 | +You can then edit the generated config at `app/config/packages/superbalist/laravel4-event-pubsub/config.php`. |
| 31 | + |
| 32 | +Register the service provider in app.php |
| 33 | +```php |
| 34 | +'providers' => [ |
| 35 | + // ... |
| 36 | + 'Superbalist\Laravel4EventPubSub\PubSubEventsServiceProvider', |
| 37 | +] |
| 38 | +``` |
| 39 | + |
| 40 | +Register the facade in app.php |
| 41 | +```php |
| 42 | +'aliases' => [ |
| 43 | + // ... |
| 44 | + 'PubSubEvents' => 'Superbalist\Laravel4EventPubSub\PubSubEventsFacade', |
| 45 | +] |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +### Simple Events |
| 51 | + |
| 52 | +A `SimpleEvent` is an event which takes a name and optional attributes. |
| 53 | + |
| 54 | +```php |
| 55 | +// the pubsub_events.translator config setting should be set to 'pubsub.events.translators.simple' |
| 56 | + |
| 57 | +// get the event manager |
| 58 | +$manager = app('pubsub.events'); |
| 59 | + |
| 60 | +// publish an event |
| 61 | +$event = new \Superbalist\EventPubSub\Events\SimpleEvent( |
| 62 | + 'user.created', |
| 63 | + [ |
| 64 | + 'user' => [ |
| 65 | + 'id' => 1456, |
| 66 | + 'first_name' => 'Joe', |
| 67 | + 'last_name' => 'Soap', |
| 68 | + 'email' => 'joe.soap@example.org', |
| 69 | + ], |
| 70 | + ] |
| 71 | +); |
| 72 | +$manager->dispatch('events', $event); |
| 73 | + |
| 74 | +// listen for an event |
| 75 | +$manager->listen('events', 'user.created', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 76 | + var_dump($event->getName()); |
| 77 | + var_dump($event->getAttribute('user')); |
| 78 | +}); |
| 79 | + |
| 80 | +// listen for all events on the channel |
| 81 | +$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 82 | + var_dump($event->getName()); |
| 83 | +}); |
| 84 | + |
| 85 | +// all the aboce commands can also be done using the facade |
| 86 | +PubSubEvents::dispatch('events', $event); |
| 87 | +``` |
| 88 | + |
| 89 | +### Topic Events |
| 90 | + |
| 91 | +A `TopicEvent` is an event which takes a topic, name, version and optional attributes. |
| 92 | + |
| 93 | +```php |
| 94 | +// the pubsub_events.translator config setting should be set to 'pubsub.events.translators.topic' |
| 95 | + |
| 96 | +// get the event manager |
| 97 | +$manager = app('pubsub.events'); |
| 98 | + |
| 99 | +// publish an event |
| 100 | +$event = new \Superbalist\EventPubSub\Events\TopicEvent( |
| 101 | + 'user', |
| 102 | + 'created', |
| 103 | + '1.0', |
| 104 | + [ |
| 105 | + 'user' => [ |
| 106 | + 'id' => 1456, |
| 107 | + 'first_name' => 'Joe', |
| 108 | + 'last_name' => 'Soap', |
| 109 | + 'email' => 'joe.soap@example.org', |
| 110 | + ], |
| 111 | + ] |
| 112 | +); |
| 113 | +$manager->dispatch('events', $event); |
| 114 | + |
| 115 | +// listen for an event on a topic |
| 116 | +$manager->listen('events', 'user/created', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 117 | + // ... |
| 118 | +}); |
| 119 | + |
| 120 | +// listen for an event on a topic matching the given version |
| 121 | +$manager->listen('events', 'user/created/1.0', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 122 | + // ... |
| 123 | +}); |
| 124 | + |
| 125 | +// listen for all events on a topic |
| 126 | +$manager->listen('events', 'user/*', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 127 | + // ... |
| 128 | +}); |
| 129 | + |
| 130 | +// listen for all events on the channel |
| 131 | +$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 132 | + // ... |
| 133 | +}); |
| 134 | +``` |
| 135 | + |
| 136 | +### Schema Events |
| 137 | + |
| 138 | +A `SchemaEvent` is an extension of the `TopicEvent` and takes a schema and optional attributes. The topic, name and |
| 139 | +version are derived from the schema. |
| 140 | + |
| 141 | +The schema must be in the format `(protocol)://(......)?/events/(topic)/(channel)/(version).json` |
| 142 | + |
| 143 | +```php |
| 144 | +// the pubsub_events.translator config setting should be set to 'pubsub.events.translators.schema' |
| 145 | +// the pubsub_events.validator config setting can be set to 'pubsub.events.validators.json_schema' to take advantage of |
| 146 | +// JSON Schema validation on incoming events |
| 147 | + |
| 148 | +// get the event manager |
| 149 | +$manager = app('pubsub.events'); |
| 150 | + |
| 151 | +// publish an event |
| 152 | +$event = new \Superbalist\EventPubSub\Events\SchemaEvent( |
| 153 | + 'http://schemas.my-website.org/events/user/created/1.0.json', |
| 154 | + [ |
| 155 | + 'user' => [ |
| 156 | + 'id' => 1456, |
| 157 | + 'first_name' => 'Joe', |
| 158 | + 'last_name' => 'Soap', |
| 159 | + 'email' => 'joe.soap@example.org', |
| 160 | + ], |
| 161 | + ] |
| 162 | +); |
| 163 | +$manager->dispatch('events', $event); |
| 164 | + |
| 165 | +// the listen expressions are the same as those used for TopicEvents. |
| 166 | +``` |
0 commit comments