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

Commit 301c02d

Browse files
add dockerfile and example php scripts
1 parent 83a1082 commit 301c02d

File tree

7 files changed

+243
-1
lines changed

7 files changed

+243
-1
lines changed

Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM php:7.0-fpm
2+
MAINTAINER Superbalist <tech+docker@superbalist.com>
3+
4+
RUN mkdir /opt/php-event-pubsub
5+
WORKDIR /opt/php-event-pubsub
6+
7+
# Packages
8+
RUN apt-get update \
9+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
10+
git \
11+
zlib1g-dev \
12+
unzip \
13+
&& rm -r /var/lib/apt/lists/*
14+
15+
# PHP Extensions
16+
RUN docker-php-ext-install -j$(nproc) zip
17+
18+
# Composer
19+
ENV COMPOSER_HOME /composer
20+
ENV PATH /composer/vendor/bin:$PATH
21+
ENV COMPOSER_ALLOW_SUPERUSER 1
22+
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
23+
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
24+
&& php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" \
25+
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --version=1.1.0 && rm -rf /tmp/composer-setup.php
26+
27+
# Install Composer Application Dependencies
28+
COPY composer.json /opt/php-event-pubsub/
29+
RUN composer install --no-autoloader --no-scripts --no-interaction
30+
31+
COPY src /opt/php-event-pubsub/src
32+
COPY examples /opt/php-event-pubsub/examples
33+
34+
RUN composer dump-autoload --no-interaction
35+
36+
CMD ["/bin/bash"]

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.PHONY: tests
2+
3+
up:
4+
@docker-compose rm -f
5+
@docker-compose pull
6+
@sed -e "s/HOSTIP/$$(docker-machine ip)/g" docker-compose.yml | docker-compose --file - up --build -d
7+
@docker-compose run php-event-pubsub /bin/bash
8+
9+
down:
10+
@docker-compose stop -t 1
11+
12+
tests:
13+
@./vendor/bin/phpunit --configuration phpunit.xml

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,20 @@ Your validator must implement the following methods.
261261
* @return bool
262262
*/
263263
public function validates(EventInterface $event);
264-
```
264+
```
265+
266+
## Examples
267+
268+
The library comes with [examples](examples) for the different types of events and a [Dockerfile](Dockerfile) for
269+
running the example scripts.
270+
271+
Run `make up`.
272+
273+
You will start at a `bash` prompt in the `/opt/php-event-pubsub` directory.
274+
275+
To run the examples:
276+
```bash
277+
$ php examples/SimpleEventExample.php
278+
$ php examples/TopicEventExample.php
279+
$ php examples/SchemaEventExample.php
280+
```

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '2'
2+
services:
3+
php-event-pubsub:
4+
build: .
5+
volumes:
6+
- ./src:/opt/php-event-pubsub/src
7+
- ./examples:/opt/php-event-pubsub/examples

examples/SchemaEventExample.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
5+
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
6+
7+
$translator = new \Superbalist\EventPubSub\Translators\SchemaEventMessageTranslator();
8+
9+
$schemas = [
10+
'events/user/created/1.0.json' => json_encode([
11+
'$schema' => 'http://json-schema.org/draft-04/schema#',
12+
'title' => 'My Schema',
13+
'type' => 'object',
14+
'properties' => [
15+
'schema' => [
16+
'type' => 'string',
17+
],
18+
'user' => [
19+
'type' => 'object',
20+
],
21+
],
22+
'required' => [
23+
'schema',
24+
'user',
25+
],
26+
]),
27+
];
28+
$loader = new \League\JsonGuard\Loaders\ArrayLoader($schemas);
29+
30+
$dereferencer = new \League\JsonGuard\Dereferencer();
31+
$dereferencer->registerLoader($loader, 'array');
32+
33+
$validator = new \Superbalist\EventPubSub\Validators\JSONSchemaEventValidator($dereferencer);
34+
35+
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator, $validator);
36+
37+
// listen for "user/created/1.0" event
38+
$manager->listen('events', 'user/created/1.0', function (\Superbalist\EventPubSub\EventInterface $event) {
39+
echo "Listener '*' received new event on channel 'events':\n";
40+
echo "\n";
41+
var_dump($event);
42+
echo "\n\n";
43+
});
44+
45+
// publish an event
46+
$event = new \Superbalist\EventPubSub\Events\SchemaEvent(
47+
'array://events/user/created/1.0.json',
48+
[
49+
'user' => [
50+
'id' => 1456,
51+
'first_name' => 'Joe',
52+
'last_name' => 'Soap',
53+
'email' => 'joe.soap@example.org',
54+
],
55+
]
56+
);
57+
$manager->dispatch('events', $event);

examples/SimpleEventExample.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
5+
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
6+
$translator = new \Superbalist\EventPubSub\Translators\SimpleEventMessageTranslator();
7+
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator);
8+
9+
// listen for "*" event
10+
$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) {
11+
echo "Listener '*' received new event on channel 'events':\n";
12+
echo "\n";
13+
var_dump($event);
14+
echo "\n\n";
15+
});
16+
17+
// listen for "user.created" event
18+
$manager->listen('events', 'user.created', function (\Superbalist\EventPubSub\EventInterface $event) {
19+
echo "Listener 'user.created' received new event on channel 'events':\n";
20+
echo "\n";
21+
var_dump($event);
22+
echo "\n\n";
23+
});
24+
25+
// publish an event
26+
$event = new \Superbalist\EventPubSub\Events\SimpleEvent(
27+
'user.created',
28+
[
29+
'user' => [
30+
'id' => 1456,
31+
'first_name' => 'Joe',
32+
'last_name' => 'Soap',
33+
'email' => 'joe.soap@example.org',
34+
],
35+
]
36+
);
37+
$manager->dispatch('events', $event);

examples/TopicEventExample.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
5+
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
6+
$translator = new \Superbalist\EventPubSub\Translators\TopicEventMessageTranslator();
7+
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator);
8+
9+
// listen for "*" event
10+
$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) {
11+
echo "Listener '*' received new event on channel 'events':\n";
12+
echo "\n";
13+
var_dump($event);
14+
echo "\n\n";
15+
});
16+
17+
// listen for "user/created" event
18+
$manager->listen('events', 'user/created', function (\Superbalist\EventPubSub\EventInterface $event) {
19+
echo "Listener 'user/created' received new event on channel 'events':\n";
20+
echo "\n";
21+
var_dump($event);
22+
echo "\n\n";
23+
});
24+
25+
// listen for "user/*" event
26+
$manager->listen('events', 'user/*', function (\Superbalist\EventPubSub\EventInterface $event) {
27+
echo "Listener 'user/*' received new event on channel 'events':\n";
28+
echo "\n";
29+
var_dump($event);
30+
echo "\n\n";
31+
});
32+
33+
// listen for "user/created/1.0" event
34+
$manager->listen('events', 'user/created/1.0', function (\Superbalist\EventPubSub\EventInterface $event) {
35+
echo "Listener 'user/created/1.0' received new event on channel 'events':\n";
36+
echo "\n";
37+
var_dump($event);
38+
echo "\n\n";
39+
});
40+
41+
// listen for "order/created" event
42+
$manager->listen('events', 'order/created', function (\Superbalist\EventPubSub\EventInterface $event) {
43+
echo "Listener 'order/created' received new event on channel 'events':\n";
44+
echo "\n";
45+
var_dump($event);
46+
echo "\n\n";
47+
});
48+
49+
// publish an event
50+
$event = new \Superbalist\EventPubSub\Events\TopicEvent(
51+
'user',
52+
'created',
53+
'1.0',
54+
[
55+
'user' => [
56+
'id' => 1456,
57+
'first_name' => 'Joe',
58+
'last_name' => 'Soap',
59+
'email' => 'joe.soap@example.org',
60+
],
61+
]
62+
);
63+
$manager->dispatch('events', $event);
64+
65+
// publish an event
66+
$event = new \Superbalist\EventPubSub\Events\TopicEvent(
67+
'order',
68+
'created',
69+
'2.1',
70+
[
71+
'order' => [
72+
'id' => 1456,
73+
],
74+
]
75+
);
76+
$manager->dispatch('events', $event);

0 commit comments

Comments
 (0)