Skip to content

Commit

Permalink
Merge pull request #24 from tomaj/2.0.0-dev
Browse files Browse the repository at this point in the history
2.0.0 dev
  • Loading branch information
tomaj authored Oct 17, 2018
2 parents cd7db38 + a9072f5 commit 713c5a9
Show file tree
Hide file tree
Showing 49 changed files with 218 additions and 80 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ language: php

services:
- redis-server
- elasticsearch
- rabbitmq

php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

sudo: false

Expand All @@ -19,6 +14,7 @@ matrix:
- php: hhvm

before_script:
- sudo apt-get install libzmq3-dev
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo "extension = zmq.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- travis_retry composer self-update
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## [Unreleased][unreleased]


## 2.0.0 - 2018-08-14

### Added

* Message now support scheduled parameter - Driver needs to support this behaviour.
* Type hints

### Changed

* Dropped support for php 5.4
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ class JmsSerializer implements SerializerInterface
[jms/serializer]: http://jmsyst.com/libs/serializer



### Scheduled execution

From version 2.0 you can add 4th parameter to Message as timestamp in future. This message will be processed after this time. This funcionality is supported in RedisDriver right now.

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"source": "https://github.com/tomaj/hermes"
},
"require": {
"php": ">= 5.5.0",
"php": ">= 7.1.0",
"ramsey/uuid": "~3.0",
"psr/log": "^1.0",
"tracy/tracy": "^2.0"
Expand Down
1 change: 1 addition & 0 deletions examples/rabbitmq/emitter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\RabbitMqDriver;
use Tomaj\Hermes\Emitter;
Expand Down
1 change: 1 addition & 0 deletions examples/rabbitmq/processor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\RabbitMqDriver;
use PhpAmqpLib\Connection\AMQPStreamConnection;
Expand Down
1 change: 1 addition & 0 deletions examples/redis/emitter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\RedisSetDriver;
use Tomaj\Hermes\Emitter;
Expand Down
1 change: 1 addition & 0 deletions examples/redis/processor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\RedisSetDriver;
use Tomaj\Hermes\Dispatcher;
Expand Down
1 change: 1 addition & 0 deletions examples/sqs/emitter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\AmazonSqsDriver;
use Tomaj\Hermes\Emitter;
Expand Down
1 change: 1 addition & 0 deletions examples/sqs/processor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\AmazonSqsDriver;
use Tomaj\Hermes\Dispatcher;
Expand Down
1 change: 1 addition & 0 deletions examples/zmq/emitter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\ZeroMqDriver;
use Tomaj\Hermes\Emitter;
Expand Down
1 change: 1 addition & 0 deletions examples/zmq/processor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

use Tomaj\Hermes\Driver\ZeroMqDriver;
use Tomaj\Hermes\Dispatcher;
Expand Down
20 changes: 11 additions & 9 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes;

Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct(DriverInterface $driver, LoggerInterface $logger = n
/**
* @deprecated - use Emitter::emit method intead
*/
public function emit(MessageInterface $message)
public function emit(MessageInterface $message): DispatcherInterface
{
$this->driver->send($message);

Expand All @@ -86,7 +87,7 @@ public function emit(MessageInterface $message)
*
* @return void
*/
public function handle()
public function handle(): void
{
try {
$this->driver->wait(function (MessageInterface $message) {
Expand Down Expand Up @@ -118,7 +119,7 @@ public function handle()
*
* @return bool
*/
private function dispatch(MessageInterface $message)
private function dispatch(MessageInterface $message): bool
{
$type = $message->getType();

Expand Down Expand Up @@ -147,7 +148,7 @@ private function dispatch(MessageInterface $message)
*
* @return bool
*/
private function handleMessage(HandlerInterface $handler, MessageInterface $message)
private function handleMessage(HandlerInterface $handler, MessageInterface $message): bool
{
// check if handler implements Psr\Log\LoggerAwareInterface (you can use \Psr\Log\LoggerAwareTrait)
if ($this->logger && method_exists($handler, 'setLogger')) {
Expand Down Expand Up @@ -181,21 +182,22 @@ private function handleMessage(HandlerInterface $handler, MessageInterface $mess
*
* @return bool
*/
private function hasHandlers($type)
private function hasHandlers(string $type): bool
{
return isset($this->handlers[$type]) && count($this->handlers[$type]) > 0;
}

/**
* {@inheritdoc}
*/
public function registerHandler($type, HandlerInterface $handler)
public function registerHandler(string $type, HandlerInterface $handler): DispatcherInterface
{
if (!isset($this->handlers[$type])) {
$this->handlers[$type] = [];
}

$this->handlers[$type][] = $handler;
return $this;
}

/**
Expand All @@ -205,7 +207,7 @@ public function registerHandler($type, HandlerInterface $handler)
*
* @return array
*/
private function messageLoggerContext(MessageInterface $message)
private function messageLoggerContext(MessageInterface $message): array
{
return [
'id' => $message->getId(),
Expand All @@ -218,13 +220,13 @@ private function messageLoggerContext(MessageInterface $message)
/**
* Interal log method wrapper
*
* @param string $level
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
private function log($level, $message, array $context = array())
private function log($level, string $message, array $context = array()): void
{
if ($this->logger) {
$this->logger->log($level, $message, $context);
Expand Down
3 changes: 2 additions & 1 deletion src/DispatcherInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes;

Expand All @@ -18,5 +19,5 @@ interface DispatcherInterface
*
* @return $this
*/
public function registerHandler($type, HandlerInterface $handler);
public function registerHandler(string $type, HandlerInterface $handler): DispatcherInterface;
}
8 changes: 5 additions & 3 deletions src/Driver/AmazonSqsDriver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes\Driver;

Expand Down Expand Up @@ -68,7 +69,7 @@ class AmazonSqsDriver implements DriverInterface
* @param string $queueName
* @param array $queueAttributes
*/
public function __construct(SqsClient $client, $queueName, $queueAttributes = [])
public function __construct(SqsClient $client, string $queueName, array $queueAttributes = [])
{
$this->client = $client;
$this->queueName = $queueName;
Expand All @@ -85,18 +86,19 @@ public function __construct(SqsClient $client, $queueName, $queueAttributes = []
/**
* {@inheritdoc}
*/
public function send(MessageInterface $message)
public function send(MessageInterface $message): bool
{
$this->client->sendMessage([
'QueueUrl' => $this->queueUrl,
'MessageBody' => $this->serializer->serialize($message),
]);
return true;
}

/**
* {@inheritdoc}
*/
public function wait(Closure $callback)
public function wait(Closure $callback): void
{
while (true) {
$result = $this->client->receiveMessage(array(
Expand Down
7 changes: 4 additions & 3 deletions src/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes\Driver;

Expand All @@ -16,9 +17,9 @@ interface DriverInterface
*
* @param MessageInterface $message
*
* @return $this
* @return bool
*/
public function send(MessageInterface $message);
public function send(MessageInterface $message): bool;

/**
* Processing wait method.
Expand All @@ -32,5 +33,5 @@ public function send(MessageInterface $message);
*
* @return void
*/
public function wait(Closure $callback);
public function wait(Closure $callback): void;
}
10 changes: 6 additions & 4 deletions src/Driver/LazyRabbitMqDriver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes\Driver;

Expand Down Expand Up @@ -26,7 +27,7 @@ class LazyRabbitMqDriver implements DriverInterface
* @param AMQPLazyConnection $connection
* @param string $queue
*/
public function __construct(AMQPLazyConnection $connection, $queue)
public function __construct(AMQPLazyConnection $connection, string $queue)
{
$this->connection = $connection;
$this->queue = $queue;
Expand All @@ -36,16 +37,17 @@ public function __construct(AMQPLazyConnection $connection, $queue)
/**
* {@inheritdoc}
*/
public function send(MessageInterface $message)
public function send(MessageInterface $message): bool
{
$rabbitMessage = new AMQPMessage($this->serializer->serialize($message));
$this->getChannel()->basic_publish($rabbitMessage, '', $this->queue);
return true;
}

/**
* {@inheritdoc}
*/
public function wait(Closure $callback)
public function wait(Closure $callback): void
{
$this->getChannel()->basic_consume(
$this->queue,
Expand All @@ -65,7 +67,7 @@ function ($rabbitMessage) use ($callback) {
}
}

private function getChannel()
private function getChannel(): AMQPChannel
{
if ($this->channel) {
return $this->channel;
Expand Down
10 changes: 6 additions & 4 deletions src/Driver/MaxItemsTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes\Driver;

Expand All @@ -14,22 +15,23 @@ trait MaxItemsTrait
*/
private $maxProcessItems = 0;

public function setMaxProcessItems($count)
public function setMaxProcessItems(int $count): void
{
$this->maxProcessItems = $count;
}

public function incrementProcessedItems()
public function incrementProcessedItems(): int
{
$this->processed++;
return $this->processed;
}

public function processed()
public function processed(): int
{
return $this->processed;
}

public function shouldProcessNext()
public function shouldProcessNext(): bool
{
if ($this->maxProcessItems == 0) {
return true;
Expand Down
8 changes: 5 additions & 3 deletions src/Driver/RabbitMqDriver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Tomaj\Hermes\Driver;

Expand Down Expand Up @@ -34,7 +35,7 @@ class RabbitMqDriver implements DriverInterface
* @param AMQPChannel $channel
* @param string $queue
*/
public function __construct(AMQPChannel $channel, $queue)
public function __construct(AMQPChannel $channel, string $queue)
{
$this->channel = $channel;
$this->queue = $queue;
Expand All @@ -44,16 +45,17 @@ public function __construct(AMQPChannel $channel, $queue)
/**
* {@inheritdoc}
*/
public function send(MessageInterface $message)
public function send(MessageInterface $message): bool
{
$rabbitMessage = new AMQPMessage($this->serializer->serialize($message));
$this->channel->basic_publish($rabbitMessage, '', $this->queue);
return true;
}

/**
* {@inheritdoc}
*/
public function wait(Closure $callback)
public function wait(Closure $callback): void
{
$this->channel->basic_consume(
$this->queue,
Expand Down
Loading

0 comments on commit 713c5a9

Please sign in to comment.