Skip to content

Commit

Permalink
Update to use command/options (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ameech committed Aug 26, 2016
1 parent 8607fa9 commit 0b4c0a5
Show file tree
Hide file tree
Showing 33 changed files with 526 additions and 827 deletions.
73 changes: 6 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

Missing the driver you want? [Create it!](#creating-a-driver)

##### Available Serializers
- JSON

Missing the serializer you want? [Create it!](#creating-a-serializer)

### Creating A Consumer

```PHP
Expand All @@ -32,19 +27,12 @@ $driver = new Equip\Queue\Driver\RedisDriver($redis);
$emitter = new League\Event\Emitter;
$event = new Equip\Queue\Event($emitter);

// Instantiate the serializer class
$serializer = new Equip\Queue\Serializer\JsonSerializer;

// Create your handler routes
$routes = [
// Any message within the queue with $message->handler() equal to 'awesome', will fire off this callable.
'awesome' => function (Message $message) {
var_dump($message);
},
];
// Instantiate the command factory
$injector = new Auryn\Injector;
$factory = new Equip\Queue\Command\AurynCommandFactory($injector);

// Instantiate the Worker class
$worker = new Equip\Queue\Worker($driver, $event, $serializer, $routes);
$worker = new Equip\Queue\Worker($driver, $event, $factory);

// Kick off the consumer
$worker->consume($queue);
Expand All @@ -62,68 +50,19 @@ $redis->connect('127.0.0.1');
// Instantiate the Redis driver
$driver = new Equip\Queue\Driver\RedisDriver($redis);

// Instantiate the serializer class
$serializer = new Equip\Queue\Serializer\JsonSerializer;

// Instantiate the Queue class
$queue = new Queue($driver, $serializer);
$queue = new Queue($driver);
```

Here's an [example producer](https://github.com/equip/queue/blob/master/example/producer.php)

### Creating A Message

Creating a message is as simple as instantiating an object:
```PHP
$message = new Equip\Queue\Message(
// The queue this message will be pushed onto
'queue',

// The name of the handler that will be called when this message is being consumed
'handler',

// Message specific data that is used within your handler
['foo' => 'bar']
);
```

### Adding A Message To The Queue

After creating the [producer](#creating-a-producer), simply add your `Message` to the queue.
```PHP
$result = $queue->add($message);
$result = $queue->add($queue, Command::class, new Options);
```

A boolean (`$result`) is returned which contains the status of the push onto the queue.

### Creating A Handler

A handler is a [callable](http://php.net/manual/en/language.types.callable.php) that expects one parameter: `Equip\Queue\Message`.

Here's a couple examples of handlers:
```PHP
class ExampleHandler
{
public function __invoke(Equip\Queue\Message $message) {
var_dump($message);
}
}

// Routes array that is passed into the consumer
$routes = [
'example' => new ExampleHandler,
'foobar' => function (Equip\Queue\Message $message) {
var_dump($message);
},
];
```

If for some reason you want to stop the consumer after handling a message, you can return `false` from your handler.

### Creating A Driver

Creating a driver is as simple as implementing the [DriverInterface](https://github.com/equip/queue/blob/master/src/Driver/DriverInterface.php).

### Creating A Serializer

Creating a serializer is just as easy as creating a driver, just implement [MessageSerializerInterface](https://github.com/equip/queue/blob/master/src/Serializer/MessageSerializerInterface.php).
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "The better queueing library",
"type": "library",
"license": "MIT",
"config": {
"sort-packages": true
},
"authors": [
{
"name": "Equip Contributors",
Expand All @@ -12,15 +15,20 @@
"minimum-stability": "stable",
"require": {
"php": ">=5.6",
"equip/command": "^2.0",
"league/event": "^2.1",
"psr/log": "^1.0"
},
"require-dev": {
"eloquent/liberator": "^2.0",
"eloquent/phony": "^0.13.4",
"monolog/monolog": "^1.21",
"phpunit/phpunit": "^5.4"
"phpunit/phpunit": "^5.4",
"rdlowrey/auryn": "^1.4"
},
"suggest": {
"monolog/monolog": "For a simple logging implementation of PSR-3"
"monolog/monolog": "For a simple logging implementation of PSR-3",
"rdlowrey/auryn": "For AurynCommandFactory which handles dependency injection"
},
"autoload": {
"psr-4": {
Expand All @@ -29,7 +37,8 @@
},
"autoload-dev": {
"psr-4": {
"Equip\\Queue\\": "tests/"
"Equip\\Queue\\": "tests/",
"Example\\": "example/"
}
}
}
26 changes: 26 additions & 0 deletions example/ExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Example;

use Equip\Command\CommandImmutableOptionsTrait;
use Equip\Command\CommandInterface;

class ExampleCommand implements CommandInterface
{
use CommandImmutableOptionsTrait;

/**
* @var ExampleOptions
*/
private $options;

public function withOptions(ExampleOptions $options)
{
return $this->copyWithOptions($options);
}

public function execute()
{
var_dump($this->options, $this->options->test());
}
}
14 changes: 14 additions & 0 deletions example/ExampleOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Example;

use Equip\Command\OptionsInterface;
use Equip\Command\OptionsSerializerTrait;

class ExampleOptions implements OptionsInterface
{
public function test()
{
return 'example';
}
}
9 changes: 0 additions & 9 deletions example/Job.php

This file was deleted.

15 changes: 5 additions & 10 deletions example/consumer.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
require 'Job.php';

use Auryn\Injector;
use Equip\Queue\Command\AurynCommandFactory;
use Equip\Queue\Driver\RedisDriver;
use Equip\Queue\Event;
use Equip\Queue\Handler\SimpleHandlerFactory;
use Equip\Queue\Serializer\JsonSerializer;
use Equip\Queue\Worker;
use League\Event\Emitter;
use Monolog\Logger;
Expand All @@ -16,11 +15,7 @@

$worker = new Worker(
new RedisDriver($redis),
new Event(new Emitter),
new Logger('queue'),
new JsonSerializer,
new SimpleHandlerFactory([
'handler' => ExampleJob::class,
])
new Event(new Emitter, new Logger('queue')),
new AurynCommandFactory(new Injector)
);
$worker->consume('queue');
$worker->consume('example-queue');
16 changes: 5 additions & 11 deletions example/producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
require __DIR__ . '/../vendor/autoload.php';

use Equip\Queue\Driver\RedisDriver;
use Equip\Queue\Message;
use Equip\Queue\Queue;
use Example\ExampleCommand;
use Example\ExampleOptions;

$redis = new Redis;
$redis->connect('localhost');

$driver = new RedisDriver($redis);

$queue = new Queue($driver);

$message = new Message(
'queue',
'handler',
['data' => 'value']
$queue = new Queue(
new RedisDriver($redis)
);

$result = $queue->add($message);
$result = $queue->add('example-queue', ExampleCommand::class, new ExampleOptions);

var_dump($result);
34 changes: 34 additions & 0 deletions src/Command/AurynCommandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Equip\Queue\Command;

use Auryn\Injector;
use Equip\Command\CommandInterface;
use Equip\Queue\Exception\CommandException;

class AurynCommandFactory implements CommandFactoryInterface
{
/**
* @var Injector
*/
private $injector;

public function __construct(Injector $injector)
{
$this->injector = $injector;
}

/**
* @inheritdoc
*/
public function make($command)
{
$command = $this->injector->make($command);

if (!is_a($command, CommandInterface::class)) {
throw CommandException::invalidCommand($command);
}

return $command;
}
}
22 changes: 22 additions & 0 deletions src/Command/CommandFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Equip\Queue\Command;

use Equip\Command\CommandInterface;
use Equip\Queue\Exception\CommandException;
use Equip\Queue\Exception\HandlerException;

interface CommandFactoryInterface
{
/**
* Instantiates a command object
*
* @param string $command
*
* @return CommandInterface
*
* @throws CommandException If command class doesn't exist
* @throws CommandException If command is not an instance of CommandInterface
*/
public function make($command);
}
4 changes: 2 additions & 2 deletions src/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ interface DriverInterface
* Add a message to the queue
*
* @param string $queue
* @param string $message
* @param array $message
*
* @return bool
*/
public function enqueue($queue, $message);
public function enqueue($queue, array $message);

/**
* Retrieve a message from the queue
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/RedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function __construct(Redis $redis)
/**
* @inheritdoc
*/
public function enqueue($queue, $message)
public function enqueue($queue, array $message)
{
return (bool) $this->redis->rPush($queue, $message);
return (bool) $this->redis->rPush($queue, serialize($message));
}

/**
Expand Down

0 comments on commit 0b4c0a5

Please sign in to comment.