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

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgoslett committed Sep 2, 2016
0 parents commit 945072f
Show file tree
Hide file tree
Showing 18 changed files with 476 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
composer.lock
vendor
bin
coverage
coverage.xml
11 changes: 11 additions & 0 deletions .travis.yml
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
36 changes: 36 additions & 0 deletions Dockerfile
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"]
21 changes: 21 additions & 0 deletions LICENSE
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.
13 changes: 13 additions & 0 deletions Makefile
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
77 changes: 77 additions & 0 deletions README.md
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
```
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 1.0.0 - 2016-09-02

* Initial release
29 changes: 29 additions & 0 deletions composer.json
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"
}
}
7 changes: 7 additions & 0 deletions docker-compose.yml
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
10 changes: 10 additions & 0 deletions examples/LocalExample.php
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!');
3 changes: 3 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

include __DIR__ . '/vendor/autoload.php';
29 changes: 29 additions & 0 deletions phpunit.xml
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>
30 changes: 30 additions & 0 deletions src/Adapters/DevNullPubSubAdapter.php
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
}
}
51 changes: 51 additions & 0 deletions src/Adapters/LocalPubSubAdapter.php
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] : [];
}
}
22 changes: 22 additions & 0 deletions src/PubSubAdapterInterface.php
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);
}
Loading

0 comments on commit 945072f

Please sign in to comment.