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

Commit a08231f

Browse files
initial commit
0 parents  commit a08231f

16 files changed

+865
-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

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
- hhvm
8+
- nightly
9+
10+
before_script:
11+
- composer install

Dockerfile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
FROM php:7.0-fpm
2+
MAINTAINER Superbalist <[email protected]>
3+
4+
RUN mkdir /opt/php-pubsub
5+
WORKDIR /opt/php-pubsub
6+
7+
# Packages
8+
RUN apt-get update \
9+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
10+
git \
11+
zlib1g-dev \
12+
librdkafka-dev \
13+
unzip \
14+
&& rm -r /var/lib/apt/lists/*
15+
16+
# PHP Extensions
17+
RUN docker-php-ext-install -j$(nproc) zip \
18+
&& ( \
19+
cd /tmp \
20+
&& mkdir php-rdkafka \
21+
&& cd php-rdkafka \
22+
&& git clone https://github.com/arnaud-lb/php-rdkafka.git . \
23+
&& git checkout php7 \
24+
&& phpize \
25+
&& ./configure \
26+
&& make -j$(nproc) \
27+
&& make install \
28+
) \
29+
&& rm -rf /tmp/php-rdkafka \
30+
&& docker-php-ext-enable rdkafka
31+
32+
# Composer
33+
ENV COMPOSER_HOME /composer
34+
ENV PATH /composer/vendor/bin:$PATH
35+
ENV COMPOSER_ALLOW_SUPERUSER 1
36+
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
37+
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
38+
&& 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); }" \
39+
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --version=1.1.0 && rm -rf /tmp/composer-setup.php
40+
41+
# Install Composer Application Dependencies
42+
COPY composer.json /opt/php-pubsub/
43+
RUN composer install --no-autoloader --no-scripts --no-interaction
44+
45+
COPY src /opt/php/pubsub/
46+
COPY examples /opt/php/pubsub
47+
48+
RUN composer dump-autoload --no-interaction
49+
50+
CMD ["/bin/bash"]

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) 2016 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.

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-pubsub-kafka /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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# php-pubsub-kafka
2+
3+
A Kafka adapter for the [php-pubsub](https://github.com/Superbalist/php-pubsub) package.
4+
5+
[![Author](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/superbalist)
6+
[![Build Status](https://img.shields.io/travis/Superbalist/php-pubsub-kafka/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/php-pubsub-kafka)
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/php-pubsub-kafka.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-pubsub-kafka)
9+
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/php-pubsub-kafka.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-pubsub-kafka)
10+
11+
12+
## Installation
13+
14+
1. Install [librdkafka c library](https://github.com/edenhill/librdkafka) (Debian/Ubuntu)
15+
16+
```bash
17+
$ sudo apt-get install librdkafka-dev
18+
```
19+
2. Install the [php-rdkafka](https://github.com/arnaud-lb/php-rdkafka) PECL extension
20+
21+
**PHP5**
22+
```bash
23+
$ sudo pecl install channel://pecl.php.net/rdkafka-alpha
24+
```
25+
26+
**PHP7**
27+
```bash
28+
$ cd /tmp
29+
$ mkdir php-rdkafka
30+
$ cd php-rdkafka
31+
$ git clone https://github.com/arnaud-lb/php-rdkafka.git .
32+
$ git checkout php7
33+
$ phpize
34+
$ ./configure
35+
$ make
36+
$ make install
37+
```
38+
39+
3. Add the following to your php.ini file to enable the php-rdkafka extension
40+
`extension=rdkafka.so`
41+
42+
4. `composer require superbalist/php-pubsub-kafka`
43+
44+
## Usage
45+
46+
```php
47+
// use this topic config for both the producer and consumer
48+
$topicConfig = new \RdKafka\TopicConf();
49+
$topicConfig->set('auto.offset.reset', 'smallest');
50+
$topicConfig->set('auto.commit.interval.ms', 300);
51+
52+
// create producer
53+
$producer = new \RdKafka\Producer();
54+
$producer->addBrokers('127.0.0.1');
55+
56+
// create consumer
57+
// see https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka.examples-high-level-consumer.html
58+
$config = new \RdKafka\Conf();
59+
$config->set('group.id', 'php-pubsub');
60+
61+
$consumer = new \RdKafka\Consumer($config);
62+
$consumer->addBrokers('127.0.0.1');
63+
64+
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer, $topicConfig);
65+
66+
// consume messages
67+
// note: this is a blocking call
68+
$adapter->subscribe('my_channel', function ($message) {
69+
var_dump($message);
70+
});
71+
72+
// publish messages
73+
$adapter->publish('my_channel', 'HELLO WORLD');
74+
$adapter->publish('my_channel', json_encode(['hello' => 'world']));
75+
$adapter->publish('my_channel', 1);
76+
$adapter->publish('my_channel', false);
77+
```
78+
79+
## Examples
80+
81+
The library comes with [examples](examples) for the adapter and a [Dockerfile](Dockerfile) for
82+
running the example scripts.
83+
84+
Run `make up`.
85+
86+
You will start at a `bash` prompt in the `/opt/php-pubsub` directory.
87+
88+
If you need another shell to publish a message to a blocking consumer, you can run `docker-compose run php-pubsub-kafka /bin/bash`
89+
90+
To run the examples:
91+
```bash
92+
$ php examples/KafkaConsumerExample.php
93+
$ php examples/KafkaPublishExample.php (in a separate shell)
94+
```

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 - 2016-09-02
4+
5+
* Initial release

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "superbalist/php-pubsub-kafka",
3+
"description": "A Kafka adapter for the php-pubsub package",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Superbalist.com a division of Takealot Online (Pty) Ltd",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.6.0",
13+
"ext-rdkafka": "*",
14+
"superbalist/php-pubsub": "^1.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Superbalist\\PubSub\\Kafka\\": "src/",
19+
"Tests\\": "tests/"
20+
}
21+
},
22+
"extra": {
23+
"branch-alias": {
24+
"dev-master": "1.0-dev"
25+
}
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^5.5",
29+
"mockery/mockery": "^0.9.5"
30+
},
31+
"repositories": [
32+
{
33+
"type": "vcs",
34+
"url": "https://github.com/Superbalist/php-pubsub.git"
35+
}
36+
]
37+
}

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '2'
2+
services:
3+
php-pubsub-kafka:
4+
build: .
5+
depends_on:
6+
- "kafka"
7+
volumes:
8+
- ./src:/opt/php-pubsub/src
9+
- ./examples:/opt/php-pubsub/examples
10+
kafka:
11+
image: spotify/kafka
12+
environment:
13+
- ADVERTISED_HOST=HOSTIP
14+
- ADVERTISED_PORT=9092
15+
ports:
16+
- "9092:9092"

examples/KafkaConsumerExample.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
5+
// use this topic config for both the producer and consumer
6+
$topicConfig = new \RdKafka\TopicConf();
7+
$topicConfig->set('auto.offset.reset', 'smallest');
8+
$topicConfig->set('auto.commit.interval.ms', 300);
9+
10+
// create producer
11+
$producer = new \RdKafka\Producer();
12+
$producer->addBrokers('kafka');
13+
14+
// create consumer
15+
// see https://arnaud-lb.github.io/php-rdkafka/phpdoc/rdkafka.examples-high-level-consumer.html
16+
$config = new \RdKafka\Conf();
17+
$config->set('group.id', 'php-pubsub');
18+
19+
$consumer = new \RdKafka\Consumer($config);
20+
$consumer->addBrokers('kafka');
21+
22+
$adapter = new \Superbalist\PubSub\Kafka\KafkaPubSubAdapter($producer, $consumer, $topicConfig);
23+
24+
$adapter->subscribe('my_channel', function ($message) {
25+
var_dump($message);
26+
});

0 commit comments

Comments
 (0)