PHP code example of chocofamilyme / pubsub
1. Go to this page and download the library: Download chocofamilyme/pubsub 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/ */
chocofamilyme / pubsub example snippets
return [
'eventsource' => [
'default' => 'rabbitmq',
'drivers' => [
'rabbitmq' => [
'adapter' => 'RabbitMQ',
'hosts' => [
[
'host' => 'host',
'port' => 5672,
'user' => 'user',
'password' => 'password',
'vhost' => '/',
],
],
// Не объязательные параметры
'heartbeat' => 60,
'read_write_timeout' => 60,
'connection_timeout' => 60,
'wait_timeout' => 0,
],
],
],
];
$di = \Phalcon\Di::getDefault();
$config = $di->get('config')->eventsource;
$config = $config->drivers[$config->default];
$serviceName = $di->get('config')->domain;
$cache = $di->get('cache');
$di->setShared('eventsource',
function () use ($config, $serviceName, $cache) {
$adapter = $config->adapter;
$config = array_merge($config->toArray(), ['app_id' => $serviceName]);
$class = 'Chocofamily\PubSub\Provider\\'.$adapter;
$repeater = new Repeater($cache);
return $class::getInstance($config, $repeater);
}
);
$publisher = new Publisher($di->getShared('eventsource'));
$payload = [
'event_id' => 11995,
'name' => 'docx',
'age' => 25
];
$routeKey = 'order.created';
$publisher->send($payload, $routeKey);
$publisher = new Publisher($di->getShared('eventsource'));
$payload = [
'event_id' => 11995,
'name' => 'docx',
'age' => 25
];
$exchange = 'order';
$routeKey = 'order.created';
$publisher->send($payload, $routeKey, $exchange);
$params = [
'queue_name' => 'restapi_orderx',
];
$taskName = 'your_task_name';
$subscriber = new Subscriber($di->getShared('eventsource'), 'order.created.*', $params, $taskName);
$subscriber->subscribe(function ($headers, $body) {
echo print_r($headers, 1). PHP_EOL;
echo print_r($body, 1). PHP_EOL;
});
$params = [
'queue_name' => 'restapi_orderx',
];
$taskName = 'your_task_name';
$routeKeys = [
'order.created',
'order.payed',
];
$exchange = 'order';
$subscriber = new Subscriber($di->getShared('eventsource'), $routeKeys, $params, $taskName, $exchange);
$subscriber->subscribe(function ($headers, $body) {
echo print_r($headers, 1). PHP_EOL;
echo print_r($body, 1). PHP_EOL;
});
use Chocofamily\PubSub\Services\EventPrepare;
...
$order = new Order([
'user_id' => 11166541,
'status' => 0,
'total' => 5852,
]);
$eventSource = $di->get('eventsource');
$event = new EventPrepare($order, new OrderSerialize(['name' => 'docx']), 1);
$event->up($eventSource, 'order.created.-5');
use Chocofamily\PubSub\Services\EventPrepare;
...
$order = new Order([
'user_id' => 11166541,
'status' => 0,
'total' => 5852,
]);
$eventSource = $di->get('eventsource');
$routeKey = 'order.created.-5';
$exchange = 'order';
$event = new EventPrepare($order, new OrderSerialize(['name' => 'docx']), 1);
$event->up($eventSource, $routeKey, $exchange);
use Chocofamily\PubSub\Services\EventRepeater;
...
$dateStart = \DateTime::createFromFormat('Y-m-d', '2018-01-01');
$eventDataProvider = new Chocofamily\PubSub\Provider\Event($di->get('eventsource'), $dateStart);
try {
$event = new EventRepeater($eventDataProvider);
$event->retry();
} catch (\Exception $e) {
$message = sprintf('%d %s in %s:%s', $e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
$di->get('logger')->error($message);
}
use Chocofamily\PubSub\Services\EventCleaner;
...
try {
$event = new EventCleaner($di->get('modelsManager'));
$event->clean();
} catch (ModelException $e) {
$message = sprintf('%d %s in %s:%s', $e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
$di->get('logger')->error($message);
}
use Chocofamily\PubSub\Services\EventCleaner;
...
$dateTime = new \DateTime();
$dateTime = $dateTime->modify('-1 day');
try {
$event = new EventCleaner($di->get('modelsManager'), $dateTime);
$event->clean();
} catch (ModelException $e) {
$message = sprintf('%d %s in %s:%s', $e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
$di->get('logger')->error($message);
}