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

Commit 61dbd90

Browse files
initial commit
0 parents  commit 61dbd90

File tree

11 files changed

+579
-0
lines changed

11 files changed

+579
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
composer.lock
3+
vendor
4+
bin
5+
coverage
6+
coverage.xml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Superbalist.com a division of Takealot Online (Pty) Ltd
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# laravel4-event-pubsub
2+
3+
An event protocol and implementation over pub/sub for Laravel 4.
4+
5+
[![Author](http://img.shields.io/badge/author-@superbalist-blue.svg?style=flat-square)](https://twitter.com/superbalist)
6+
[![Build Status](https://img.shields.io/travis/Superbalist/laravel4-event-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/laravel4-event-pubsub)
7+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
8+
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/laravel4-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel4-event-pubsub)
9+
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/laravel4-event-pubsub.svg?style=flat-square)](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+
```

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 1.0.0 - ?
4+
5+
* Initial release

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "superbalist/laravel4-event-pubsub",
3+
"description": "An event protocol and implementation over pub/sub for Laravel 4",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Superbalist.com a division of Takealot Online (Pty) Ltd",
8+
"email": "info@superbalist.com"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.6.0",
13+
"superbalist/php-event-pubsub": "^1.0",
14+
"superbalist/laravel4-pubsub": "^2.0",
15+
"illuminate/support": "^4.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Superbalist\\Laravel4EventPubSub\\": "src/",
20+
"Tests\\": "tests/"
21+
}
22+
},
23+
"extra": {
24+
"branch-alias": {
25+
"dev-master": "1.0-dev"
26+
}
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "^5.5",
30+
"mockery/mockery": "^0.9.5"
31+
}
32+
}

phpunit.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
include __DIR__ . '/vendor/autoload.php';

phpunit.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="./phpunit.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="true"
12+
verbose="true"
13+
>
14+
<testsuites>
15+
<testsuite name="laravel4-event-pubsub/tests">
16+
<directory suffix=".php">./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">./src/</directory>
22+
</whitelist>
23+
</filter>
24+
<logging>
25+
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
26+
<log type="coverage-html" target="coverage" showUncoveredFiles="true"/>
27+
<log type="coverage-clover" target="coverage.xml" showUncoveredFiles="true"/>
28+
</logging>
29+
</phpunit>

src/PubSubEventsFacade.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Superbalist\Laravel4EventPubSub;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class PubSubEventsFacade extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'pubsub.events';
17+
}
18+
}

0 commit comments

Comments
 (0)