This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 945072f
Showing
18 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.idea | ||
composer.lock | ||
vendor | ||
bin | ||
coverage | ||
coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: php | ||
|
||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
- hhvm | ||
- nightly | ||
|
||
before_script: | ||
- composer install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
FROM php:7.0-fpm | ||
MAINTAINER Superbalist <[email protected]> | ||
|
||
RUN mkdir /opt/php-pubsub | ||
WORKDIR /opt/php-pubsub | ||
|
||
# Packages | ||
RUN apt-get update \ | ||
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \ | ||
git \ | ||
zlib1g-dev \ | ||
unzip \ | ||
&& rm -r /var/lib/apt/lists/* | ||
|
||
# PHP Extensions | ||
RUN docker-php-ext-install -j$(nproc) zip | ||
|
||
# Composer | ||
ENV COMPOSER_HOME /composer | ||
ENV PATH /composer/vendor/bin:$PATH | ||
ENV COMPOSER_ALLOW_SUPERUSER 1 | ||
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \ | ||
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \ | ||
&& 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); }" \ | ||
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --version=1.1.0 && rm -rf /tmp/composer-setup.php | ||
|
||
# Install Composer Application Dependencies | ||
COPY composer.json /opt/php-pubsub/ | ||
RUN composer install --no-autoloader --no-scripts --no-interaction | ||
|
||
COPY src /opt/php/pubsub/ | ||
COPY examples /opt/php/pubsub | ||
|
||
RUN composer dump-autoload --no-interaction | ||
|
||
CMD ["/bin/bash"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Superbalist.com a division of Takealot Online (Pty) Ltd | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.PHONY: tests | ||
|
||
up: | ||
@docker-compose rm -f | ||
@docker-compose pull | ||
@sed -e "s/HOSTIP/$$(docker-machine ip)/g" docker-compose.yml | docker-compose --file - up --build -d | ||
@docker-compose run php-pubsub /bin/bash | ||
|
||
down: | ||
@docker-compose stop -t 1 | ||
|
||
tests: | ||
@./vendor/bin/phpunit --configuration phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# php-pubsub | ||
|
||
A PHP abstraction for the pub-sub pattern | ||
|
||
[![Author](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/superbalist) | ||
[![Build Status](https://img.shields.io/travis/Superbalist/php-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/php-pubsub) | ||
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) | ||
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/php-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-pubsub) | ||
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/php-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-pubsub) | ||
|
||
|
||
## Installation | ||
|
||
```bash | ||
composer require superbalist/php-pubsub | ||
``` | ||
|
||
## Adapters | ||
|
||
* Local (bundled) | ||
* /dev/null (bundled) | ||
* Redis - link pending | ||
* Kafka - link pending | ||
|
||
## Usage | ||
|
||
```php | ||
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter(); | ||
|
||
// consume messages | ||
$adapter->subscribe('my_channel', function ($message) { | ||
var_dump($message); | ||
}); | ||
|
||
// publish messages | ||
$adapter->publish('my_channel', 'Hello World!'); | ||
``` | ||
|
||
## Writing an Adapter | ||
|
||
You can easily write your own custom adapter by implementing the [PubSubAdapterInterface](src/PubSubAdapterInterface.php) interface. | ||
|
||
Your adapter must implement the following methods: | ||
|
||
```php | ||
/** | ||
* Subscribe a handler to a channel. | ||
* | ||
* @param string $channel | ||
* @param callable $handler | ||
*/ | ||
public function subscribe($channel, callable $handler); | ||
|
||
/** | ||
* Publish a message to a channel. | ||
* | ||
* @param string $channel | ||
* @param mixed $message | ||
*/ | ||
public function publish($channel, $message); | ||
``` | ||
|
||
## Examples | ||
|
||
The library comes with [examples](examples) for all adapters and a [Dockerfile](Dockerfile) for | ||
running the example scripts. | ||
|
||
Run `make up`. | ||
|
||
You will start at a `bash` prompt in the `/opt/php-pubsub` directory. | ||
|
||
If you need another shell to publish a message to a blocking consumer, you can run `docker-compose run php-pubsub /bin/bash` | ||
|
||
To run the examples: | ||
```bash | ||
$ php examples/LocalExample.php | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## 1.0.0 - 2016-09-02 | ||
|
||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "superbalist/php-pubsub", | ||
"description": "An adapter based PubSub package for PHP", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Superbalist.com a division of Takealot Online (Pty) Ltd", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Superbalist\\PubSub\\": "src/", | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.0-dev" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^5.5", | ||
"mockery/mockery": "^0.9.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: '2' | ||
services: | ||
php-pubsub: | ||
build: . | ||
volumes: | ||
- ./src:/opt/php-pubsub/src | ||
- ./examples:/opt/php-pubsub/examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
include __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter(); | ||
$adapter->subscribe('my_channel', function ($message) { | ||
var_dump($message); | ||
}); | ||
|
||
$adapter->publish('my_channel', 'Hello World!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
include __DIR__ . '/vendor/autoload.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="./phpunit.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="true" | ||
verbose="true" | ||
> | ||
<testsuites> | ||
<testsuite name="php-pubsub/tests"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">./src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/> | ||
<log type="coverage-html" target="coverage" showUncoveredFiles="true"/> | ||
<log type="coverage-clover" target="coverage.xml" showUncoveredFiles="true"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Superbalist\PubSub\Adapters; | ||
|
||
use Superbalist\PubSub\PubSubAdapterInterface; | ||
|
||
class DevNullPubSubAdapter implements PubSubAdapterInterface | ||
{ | ||
/** | ||
* Subscribe a handler to a channel. | ||
* | ||
* @param string $channel | ||
* @param callable $handler | ||
*/ | ||
public function subscribe($channel, callable $handler) | ||
{ | ||
// you ain't subscribing to anything | ||
} | ||
|
||
/** | ||
* Publish a message to a channel. | ||
* | ||
* @param string $channel | ||
* @param mixed $message | ||
*/ | ||
public function publish($channel, $message) | ||
{ | ||
// your message is going to /dev/null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Superbalist\PubSub\Adapters; | ||
|
||
use Superbalist\PubSub\PubSubAdapterInterface; | ||
|
||
class LocalPubSubAdapter implements PubSubAdapterInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $subscribers = []; | ||
|
||
/** | ||
* Subscribe a handler to a channel. | ||
* | ||
* @param string $channel | ||
* @param callable $handler | ||
*/ | ||
public function subscribe($channel, callable $handler) | ||
{ | ||
if (!isset($this->subscribers[$channel])) { | ||
$this->subscribers[$channel] = []; | ||
} | ||
$this->subscribers[$channel][] = $handler; | ||
} | ||
|
||
/** | ||
* Publish a message to a channel. | ||
* | ||
* @param string $channel | ||
* @param mixed $message | ||
*/ | ||
public function publish($channel, $message) | ||
{ | ||
foreach ($this->getSubscribersForChannel($channel) as $handler) { | ||
call_user_func($handler, $message); | ||
} | ||
} | ||
|
||
/** | ||
* Return all subscribers on the given channel. | ||
* | ||
* @param string $channel | ||
* @return array | ||
*/ | ||
public function getSubscribersForChannel($channel) | ||
{ | ||
return isset($this->subscribers[$channel]) ? $this->subscribers[$channel] : []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Superbalist\PubSub; | ||
|
||
interface PubSubAdapterInterface | ||
{ | ||
/** | ||
* Subscribe a handler to a channel. | ||
* | ||
* @param string $channel | ||
* @param callable $handler | ||
*/ | ||
public function subscribe($channel, callable $handler); | ||
|
||
/** | ||
* Publish a message to a channel. | ||
* | ||
* @param string $channel | ||
* @param mixed $message | ||
*/ | ||
public function publish($channel, $message); | ||
} |
Oops, something went wrong.