Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# laravel-pubsub

A Pub-Sub abstraction for Laravel.
A Pub-Sub abstraction for Laravel and Lumen.

[![Author](http://img.shields.io/badge/[email protected]?style=flat-square)](https://twitter.com/superbalist)
[![Build Status](https://img.shields.io/travis/Superbalist/laravel-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/laravel-pubsub)
Expand All @@ -9,14 +9,16 @@ A Pub-Sub abstraction for Laravel.
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/laravel-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-pubsub)
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/laravel-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/laravel-pubsub)

This package is a wrapper bridging [php-pubsub](https://github.com/Superbalist/php-pubsub) into Laravel.
This package is a wrapper bridging [php-pubsub](https://github.com/Superbalist/php-pubsub) into Laravel and Lumen.

For **Laravel 4** support, use the package https://github.com/Superbalist/laravel4-pubsub

Please note that **Laravel 5.3** is only supported up until version 2.0.2.

2.0.3+ supports **Laravel 5.4 and up** moving forward.

3.0.1+ supports **Lumen 5.5 and up** moving forward.

The following adapters are supported:
* Local
* /dev/null
Expand All @@ -25,7 +27,7 @@ The following adapters are supported:
* Google Cloud
* HTTP

## Installation
## Laravel Installation

```bash
composer require superbalist/laravel-pubsub
Expand All @@ -35,7 +37,7 @@ Register the service provider in app.php
```php
'providers' => [
// ...
Superbalist\LaravelPubSub\PubSubServiceProvider::class,
Superbalist\LaravelPubSub\PubSubLaravelServiceProvider::class,
]
```

Expand All @@ -47,6 +49,40 @@ Register the facade in app.php
]
```

To customize the configuration file, publish the package configuration using Artisan.
```bash
php artisan vendor:publish --provider="Superbalist\LaravelPubSub\PubSubLaravelServiceProvider"
```

You can then edit the generated config at `app/config/pubsub.php`.

## Lumen Installation

```bash
composer require superbalist/laravel-pubsub
```

Register the service provider in app.php
```php
$app->register(Superbalist\LaravelPubSub\PubSubLumenServiceProvider::class);
```

Register the facade in app.php
```php
if (!class_exists('PubSub')) {
class_alias('Superbalist\LaravelPubSub\PubSubFacade', 'PubSub');
}
```

To customize the configuration file, copy the package configuration.
```bash
cp vendor/superbalist/laravel-pubsub/config/pubsub.php config/pubsub.php
```

You can then edit the generated config at `config/pubsub.php`.

## Configuration

The package has a default configuration which uses the following environment variables.
```
PUBSUB_CONNECTION=redis
Expand All @@ -64,12 +100,7 @@ HTTP_PUBSUB_URI=null
HTTP_PUBSUB_SUBSCRIBE_CONNECTION=redis
```

To customize the configuration file, publish the package configuration using Artisan.
```bash
php artisan vendor:publish --provider="Superbalist\LaravelPubSub\PubSubServiceProvider"
```

You can then edit the generated config at `app/config/pubsub.php`.

## Kafka Adapter Installation

Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.0.1 - 2017-12-01

* Added Lumen support

## 3.0.0 - 2017-07-25

* Bump up to superbalist/php-pubsub-google-cloud ^5.0 which allows for background daemon support
Expand Down
99 changes: 99 additions & 0 deletions src/PubSubBaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Superbalist\LaravelPubSub;

use Google\Cloud\PubSub\PubSubClient as GoogleCloudPubSubClient;
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use Predis\Client as RedisClient;
use Superbalist\PubSub\PubSubAdapterInterface;

class PubSubBaseServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/pubsub.php' => config_path('pubsub.php'),
]);
}

/**
* Register bindings in the container.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/pubsub.php', 'pubsub');

$this->app->singleton('pubsub.factory', function ($app) {
return new PubSubConnectionFactory($app);
});

$this->app->bind('pubsub.connection', PubSubAdapterInterface::class);

$this->app->bind(PubSubAdapterInterface::class, function ($app) {
$manager = $app['pubsub']; /* @var PubSubManager $manager */
return $manager->connection();
});

$this->registerAdapterDependencies();

$this->commands(SubscriberMakeCommand::class);
}

/**
* Register adapter dependencies in the container.
*/
protected function registerAdapterDependencies()
{
$this->app->bind('pubsub.redis.redis_client', function ($app, $parameters) {
return new RedisClient($parameters['config']);
});

$this->app->bind('pubsub.gcloud.pub_sub_client', function ($app, $parameters) {
return new GoogleCloudPubSubClient($parameters['config']);
});

$this->app->bind('pubsub.kafka.topic_conf', function () {
return new \RdKafka\TopicConf();
});

$this->app->bind('pubsub.kafka.producer', function () {
return new \RdKafka\Producer();
});

$this->app->bind('pubsub.kafka.conf', function () {
return new \RdKafka\Conf();
});

$this->app->bind('pubsub.kafka.consumer', function ($app, $parameters) {
return new \RdKafka\KafkaConsumer($parameters['conf']);
});

$this->app->bind('pubsub.http.client', function () {
return new Client();
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'pubsub',
'pubsub.factory',
'pubsub.connection',
'pubsub.redis.redis_client',
'pubsub.gcloud.pub_sub_client',
'pubsub.kafka.topic_conf',
'pubsub.kafka.producer',
'pubsub.kafka.consumer',
'pubsub.http.client',
];
}
}
19 changes: 19 additions & 0 deletions src/PubSubLaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Superbalist\LaravelPubSub;

class PubSubLaravelServiceProvider extends PubSubBaseServiceProvider
{

/**
* Register bindings in the container.
*/
public function register()
{
Parent::register();

$this->app->singleton('pubsub', function ($app) {
return new PubSubManager($app, $app['pubsub.factory']);
});
}
}
19 changes: 19 additions & 0 deletions src/PubSubLumenManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Superbalist\LaravelPubSub;

use Laravel\Lumen\Application;

class PubSubLumenManager extends PubSubManager
{

/**
* @param Application $app
* @param PubSubConnectionFactory $factory
*/
public function __construct(Application $app, PubSubConnectionFactory $factory)
{
$this->app = $app;
$this->factory = $factory;
}
}
26 changes: 26 additions & 0 deletions src/PubSubLumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Superbalist\LaravelPubSub;

class PubSubLumenServiceProvider extends PubSubBaseServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
$this->app->configure('pubsub');
}

/**
* Register bindings in the container.
*/
public function register()
{
Parent::register();

$this->app->singleton('pubsub', function ($app) {
return new PubSubLumenManager($app, $app['pubsub.factory']);
});
}
}
99 changes: 2 additions & 97 deletions src/PubSubServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,7 @@

namespace Superbalist\LaravelPubSub;

use Google\Cloud\PubSub\PubSubClient as GoogleCloudPubSubClient;
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use Predis\Client as RedisClient;
use Superbalist\PubSub\PubSubAdapterInterface;

class PubSubServiceProvider extends ServiceProvider
class PubSubServiceProvider extends PubSubLaravelServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../config/pubsub.php' => config_path('pubsub.php'),
]);
}

/**
* Register bindings in the container.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/pubsub.php', 'pubsub');

$this->app->singleton('pubsub.factory', function ($app) {
return new PubSubConnectionFactory($app);
});

$this->app->singleton('pubsub', function ($app) {
return new PubSubManager($app, $app['pubsub.factory']);
});

$this->app->bind('pubsub.connection', PubSubAdapterInterface::class);

$this->app->bind(PubSubAdapterInterface::class, function ($app) {
$manager = $app['pubsub']; /* @var PubSubManager $manager */
return $manager->connection();
});

$this->registerAdapterDependencies();

$this->commands(SubscriberMakeCommand::class);
}

/**
* Register adapter dependencies in the container.
*/
protected function registerAdapterDependencies()
{
$this->app->bind('pubsub.redis.redis_client', function ($app, $parameters) {
return new RedisClient($parameters['config']);
});

$this->app->bind('pubsub.gcloud.pub_sub_client', function ($app, $parameters) {
return new GoogleCloudPubSubClient($parameters['config']);
});

$this->app->bind('pubsub.kafka.topic_conf', function () {
return new \RdKafka\TopicConf();
});

$this->app->bind('pubsub.kafka.producer', function () {
return new \RdKafka\Producer();
});

$this->app->bind('pubsub.kafka.conf', function () {
return new \RdKafka\Conf();
});

$this->app->bind('pubsub.kafka.consumer', function ($app, $parameters) {
return new \RdKafka\KafkaConsumer($parameters['conf']);
});

$this->app->bind('pubsub.http.client', function () {
return new Client();
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'pubsub',
'pubsub.factory',
'pubsub.connection',
'pubsub.redis.redis_client',
'pubsub.gcloud.pub_sub_client',
'pubsub.kafka.topic_conf',
'pubsub.kafka.producer',
'pubsub.kafka.consumer',
'pubsub.http.client',
];
}
// kept for backwards compatibility
}