|
| 1 | +# php-event-pubsub |
| 2 | + |
| 3 | +An event protocol and implementation over pub/sub |
| 4 | + |
| 5 | +[](https://twitter.com/superbalist) |
| 6 | +[](https://travis-ci.org/Superbalist/php-event-pubsub) |
| 7 | +[](LICENSE) |
| 8 | +[](https://packagist.org/packages/superbalist/php-event-pubsub) |
| 9 | +[](https://packagist.org/packages/superbalist/php-event-pubsub) |
| 10 | + |
| 11 | +This library builds on top of the [php-pubsub](https://github.com/Superbalist/php-pubsub) package and adds support for |
| 12 | +listening to and dispatching events over pub/sub channels. |
| 13 | + |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +composer require superbalist/php-event-pubsub |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Simple Events |
| 24 | + |
| 25 | +A `SimpleEvent` is an event which takes a name and optional attributes. |
| 26 | + |
| 27 | +```php |
| 28 | +// create a new event manager |
| 29 | +$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter(); |
| 30 | +$translator = new \Superbalist\EventPubSub\Translators\SimpleEventMessageTranslator(); |
| 31 | +$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator); |
| 32 | + |
| 33 | +// publish an event |
| 34 | +$event = new \Superbalist\EventPubSub\Events\SimpleEvent( |
| 35 | + 'user.created', |
| 36 | + [ |
| 37 | + 'user' => [ |
| 38 | + 'id' => 1456, |
| 39 | + 'first_name' => 'Joe', |
| 40 | + 'last_name' => 'Soap', |
| 41 | + 'email' => 'joe.soap@example.org', |
| 42 | + ], |
| 43 | + ] |
| 44 | +); |
| 45 | +$manager->dispatch('events', $event); |
| 46 | + |
| 47 | +// listen for an event |
| 48 | +$manager->listen('events', 'user.created', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 49 | + var_dump($event->getName()); |
| 50 | + var_dump($event->getAttribute('user')); |
| 51 | +}); |
| 52 | + |
| 53 | +// listen for all events on the channel |
| 54 | +$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 55 | + var_dump($event->getName()); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +### Topic Events |
| 60 | + |
| 61 | +A `TopicEvent` is an event which takes a topic, name, version and optional attributes. |
| 62 | + |
| 63 | +```php |
| 64 | +// create a new event manager |
| 65 | +$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter(); |
| 66 | +$translator = new \Superbalist\EventPubSub\Translators\TopicEventMessageTranslator(); |
| 67 | +$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator); |
| 68 | + |
| 69 | +// publish an event |
| 70 | +$event = new \Superbalist\EventPubSub\Events\TopicEvent( |
| 71 | + 'user', |
| 72 | + 'created', |
| 73 | + '1.0', |
| 74 | + [ |
| 75 | + 'user' => [ |
| 76 | + 'id' => 1456, |
| 77 | + 'first_name' => 'Joe', |
| 78 | + 'last_name' => 'Soap', |
| 79 | + 'email' => 'joe.soap@example.org', |
| 80 | + ], |
| 81 | + ] |
| 82 | +); |
| 83 | +$manager->dispatch('events', $event); |
| 84 | + |
| 85 | +// listen for an event on a topic |
| 86 | +$manager->listen('events', 'user/created', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 87 | + // ... |
| 88 | +}); |
| 89 | + |
| 90 | +// listen for an event on a topic matching the given version |
| 91 | +$manager->listen('events', 'user/created/1.0', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 92 | + // ... |
| 93 | +}); |
| 94 | + |
| 95 | +// listen for all events on a topic |
| 96 | +$manager->listen('events', 'user/*', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 97 | + // ... |
| 98 | +}); |
| 99 | + |
| 100 | +// listen for all events on the channel |
| 101 | +$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) { |
| 102 | + // ... |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +### Schema Events |
| 107 | + |
| 108 | +A `SchemaEvent` is an extension of the `TopicEvent` and takes a schema and optional attributes. The topic, name and |
| 109 | +version are derived from the schema. |
| 110 | + |
| 111 | +The schema must be in the format `(protocol)://(......)?/events/(topic)/(channel)/(version).json` |
| 112 | + |
| 113 | +```php |
| 114 | +// create a new event manager |
| 115 | +$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter(); |
| 116 | + |
| 117 | +$translator = new \Superbalist\EventPubSub\Translators\SchemaEventMessageTranslator(); |
| 118 | + |
| 119 | +$schemas = [ |
| 120 | + 'events/user/created/1.0.json' => json_encode([ |
| 121 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 122 | + 'title' => 'My Schema', |
| 123 | + 'type' => 'object', |
| 124 | + 'properties' => [ |
| 125 | + 'schema' => [ |
| 126 | + 'type' => 'string', |
| 127 | + ], |
| 128 | + 'user' => [ |
| 129 | + 'type' => 'object', |
| 130 | + ], |
| 131 | + ], |
| 132 | + 'required' => [ |
| 133 | + 'schema', |
| 134 | + 'user', |
| 135 | + ], |
| 136 | + ]), |
| 137 | +]; |
| 138 | +$loader = new \League\JsonGuard\Loaders\ArrayLoader($schemas); |
| 139 | + |
| 140 | +$dereferencer = new \League\JsonGuard\Dereferencer(); |
| 141 | +$dereferencer->registerLoader($loader, 'array'); |
| 142 | + |
| 143 | +$validator = new \Superbalist\EventPubSub\Validators\JSONSchemaEventValidator($dereferencer); |
| 144 | + |
| 145 | +$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator, $validator); |
| 146 | + |
| 147 | +// publish an event |
| 148 | +$event = new \Superbalist\EventPubSub\Events\SchemaEvent( |
| 149 | + 'http://schemas.my-website.org/events/user/created/1.0.json', |
| 150 | + [ |
| 151 | + 'user' => [ |
| 152 | + 'id' => 1456, |
| 153 | + 'first_name' => 'Joe', |
| 154 | + 'last_name' => 'Soap', |
| 155 | + 'email' => 'joe.soap@example.org', |
| 156 | + ], |
| 157 | + ] |
| 158 | +); |
| 159 | +$manager->dispatch('events', $event); |
| 160 | + |
| 161 | +// the listen expressions are the same as those used for TopicEvents. |
| 162 | +``` |
| 163 | + |
| 164 | +### Custom Events |
| 165 | + |
| 166 | +You can easily use a custom event structure by writing a class which implements the `EventInterface` interface. |
| 167 | +You will then need to write a custom translator to translate incoming messages to your own event object. |
| 168 | + |
| 169 | +Your event must implement the following methods. |
| 170 | + |
| 171 | +```php |
| 172 | +/** |
| 173 | + * Return the event name. |
| 174 | + * |
| 175 | + * @return string |
| 176 | + */ |
| 177 | +public function getName(); |
| 178 | + |
| 179 | +/** |
| 180 | + * Return all event attributes. |
| 181 | + * |
| 182 | + * @return array |
| 183 | + */ |
| 184 | +public function getAttributes(); |
| 185 | + |
| 186 | +/** |
| 187 | + * Return an event attribute. |
| 188 | + * |
| 189 | + * @param string $name |
| 190 | + * @return mixed |
| 191 | + */ |
| 192 | +public function getAttribute($name); |
| 193 | + |
| 194 | +/** |
| 195 | + * Check whether or not an event has an attribute. |
| 196 | + * |
| 197 | + * @param string $name |
| 198 | + * @return bool |
| 199 | + */ |
| 200 | +public function hasAttribute($name); |
| 201 | + |
| 202 | +/** |
| 203 | + * Check whether or not the event matches the given expression. |
| 204 | + * |
| 205 | + * @param string $expr |
| 206 | + * @return bool |
| 207 | + */ |
| 208 | +public function matches($expr); |
| 209 | + |
| 210 | +/** |
| 211 | + * Return the event in a message format ready for publishing. |
| 212 | + * |
| 213 | + * @return mixed |
| 214 | + */ |
| 215 | +public function toMessage(); |
| 216 | +``` |
| 217 | + |
| 218 | +## Translators |
| 219 | + |
| 220 | +A translator is used to translate an incoming message into an event. |
| 221 | + |
| 222 | +The package comes bundled with a `SimpleEventMessageTranslator`, `TopicEventMessageTranslator` and a |
| 223 | +`SchemaEventMessageTranslator`. |
| 224 | + |
| 225 | +### Custom Translators |
| 226 | + |
| 227 | +You can easily write your own translator by implementing the `MessageTranslatorInterface` interface. |
| 228 | + |
| 229 | +Your translator must implement the following methods. |
| 230 | + |
| 231 | +```php |
| 232 | +/** |
| 233 | + * @param mixed $message |
| 234 | + * @return null|EventInterface |
| 235 | + */ |
| 236 | +public function translate($message); |
| 237 | +``` |
| 238 | + |
| 239 | +## Validators |
| 240 | + |
| 241 | +A validator is an optional component used to validate an incoming event. |
| 242 | + |
| 243 | +### JSONSchemaEventValidator |
| 244 | + |
| 245 | +This package comes bundled with a `JSONSchemaEventValidator` which works with `SchemaEvent` type events. |
| 246 | + |
| 247 | +This validator validates events against a [JSON Schema Spec](http://json-schema.org/) using the |
| 248 | +[JSON Guard](http://json-guard.thephpleague.com/dereferencing/overview/) PHP package. |
| 249 | + |
| 250 | +Please see the "Schema Events" section above and the JSON Guard documentation for usage examples. |
| 251 | + |
| 252 | +### Custom Validators |
| 253 | + |
| 254 | +You can write your own validator by implementing the `EventValidatorInterface` interface. |
| 255 | + |
| 256 | +Your validator must implement the following methods. |
| 257 | + |
| 258 | +```php |
| 259 | +/** |
| 260 | + * @param EventInterface $event |
| 261 | + * @return bool |
| 262 | + */ |
| 263 | +public function validates(EventInterface $event); |
| 264 | +``` |
0 commit comments