1. Go to this page and download the library: Download tomaj/hermes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tomaj / hermes example snippets
use Redis;
use Tomaj\Hermes\Message;
use Tomaj\Hermes\Emitter;
use Tomaj\Hermes\Driver\RedisSetDriver;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$driver = new RedisSetDriver($redis);
$emitter = new Emitter($driver);
$message = new Message('send-email', [
'to' => '[email protected]',
'subject' => 'Testing hermes email',
'message' => 'Hello from hermes!'
]);
$emitter->emit($message);
# file handler.php
use Redis;
use Tomaj\Hermes\Driver\RedisSetDriver;
use Tomaj\Hermes\Dispatcher;
use Tomaj\Hermes\Handler\HandlerInterface;
class SendEmailHandler implements HandlerInterface
{
// here you will receive message that was emitted from web application
public function handle(MessageInterface $message)
{
$payload = $message->getPayload();
mail($payload['to'], $payload['subject'], $payload['message']);
return true;
}
}
// create dispatcher like in the first snippet
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$driver = new RedisSetDriver($redis);
$dispatcher = new Dispatcher($driver);
// register handler for event
$dispatcher->registerHandler('send-email', new SendEmailHandler());
// at this point this script will wait for new message
$dispatcher->handle();
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('hermes');
$log->pushHandler(new StreamHandler('hermes.log'));
// $driver = ....
$dispatcher = new Dispatcher($driver, $log);
use Redis;
use Tomaj\Hermes\Driver\RedisSetDriver;
use Tomaj\Hermes\Dispatcher;
use Tomaj\Hermes\Handler\HandlerInterface;
use Psr\Log\LoggerAwareTrait;
class SendEmailHandlerWithLogger implements HandlerInterface
{
// enable logger
use LoggerAwareTrait;
public function handle(MessageInterface $message)
{
$payload = $message->getPayload();
// log info message
$this->logger->info("Trying to send email to {$payload['to']}");
mail($payload['to'], $payload['subject'], $payload['message']);
return true;
}
}
declare(strict_types=1);
namespace Tomaj\Hermes\Handler;
use Tomaj\Hermes\MessageInterface;
class EchoHandler implements HandlerInterface
{
use RetryTrait;
public function handle(MessageInterface $message): bool
{
throw new \Exception('this will always fail');
}
// optional - default is 25
public function maxRetry(): int
{
return 10;
}
}
use Tomaj\Hermes\Driver\RedisSetDriver;
use Tomaj\Hermes\Emitter;
use Tomaj\Hermes\Message;
use Tomaj\Hermes\Dispatcher;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$driver = new RedisSetDriver($redis);
$driver->setupPriorityQueue('hermes_low', Dispatcher::DEFAULT_PRIORITY - 10);
$driver->setupPriorityQueue('hermes_high', Dispatcher::DEFAULT_PRIORITY + 10);
$emitter = new Emitter($driver);
$emitter->emit(new Message('type1', ['a' => 'b'], Dispatcher::DEFAULT_PRIORITY - 10));
$emitter->emit(new Message('type1', ['c' => 'd'], Dispatcher::DEFAULT_PRIORITY + 10));
$shutdownFile = '/tmp/hermes_shutdown';
$shutdown = Tomaj\Hermes\Shutdown\SharedFileShutdown($shutdownFile);
// $log = ...
// $driver = ....
$dispatcher = new Dispatcher($driver, $log, $shutdown);
// ...
// shutdown can be triggered be calling `ShutdownInterface::shutdown()`
$shutdown->shutdown();
$redisClient = new Predis\Client();
$redisShutdownKey = 'hermes_shutdown'; // can be omitted; default value is `hermes_shutdown`
$shutdown = Tomaj\Hermes\Shutdown\RedisShutdown($redisClient, $redisShutdownKey);
// $log = ...
// $driver = ....
$dispatcher = new Dispatcher($driver, $log, $shutdown);
// ...
// shutdown can be triggered be calling `ShutdownInteface::shutdown()`
$shutdown->shutdown();
namespace My\Custom\Driver;
use Tomaj\Hermes\Driver\DriverInterface;
use Tomaj\Hermes\Message;
use Closure;
class GearmanDriver implements DriverInterface
{
private $client;
private $worker;
private $channel;
private $serializer;
public function __construct(GearmanClient $client, GearmanWorker $worker, $channel = 'hermes')
{
$this->client = $client;
$this->worker = $worker;
$this->channel = $channel;
$this->serializer = $serialier;
}
public function send(Message $message)
{
$this->client->do($this->channel, $this->serializer->serialize($message));
}
public function wait(Closure $callback)
{
$worker->addFunction($this->channel, function ($gearmanMessage) use ($callback) {
$message = $this->serializer->unserialize($gearmanMessage);
$callback($message);
});
while ($this->worker->work());
}
}
namespace My\Custom\Serializer;
use Tomaj\Hermes\SerializerInterface;
use Tomaj\Hermes\MessageInterface;
class JmsSerializer implements SerializerInterface
{
public function serialize(MessageInterface $message)
{
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
return $serializer->serialize($message, 'json');
}
public function unserialize($string)
{
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
return $serializer->deserialize($message, 'json');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.