Ejemplo n.º 1
0
 /**
  * @param string $url
  *
  * @return ConnectionInterface
  */
 public function get($url)
 {
     $hash = $this->hash($url);
     if (!isset($this->connections[$hash])) {
         $this->connections[$hash] = ConnectionBuilder::make()->create($url);
     }
     return $this->connections[$hash];
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     if (!isset($_SERVER['AMQP_URL'])) {
         $this->markTestSkipped('This test require AMQP Server, use AMQP_URL environment variables to set connection details');
     }
     $this->exchange = uniqid('phpunit.');
     $env = function ($key, $default = null) {
         return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
     };
     $this->connection = ConnectionBuilder::make()->create($env('AMQP_URL', ''));
     $this->handler = $this->createMock(HandlerInterface::class);
     $this->channel = $this->connection->channel();
     $this->consumer = new Consumer($this->connection, $this->handler);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     if (!isset($_SERVER['AMQP_URL'])) {
         $this->markTestSkipped('This test require AMQP Server, use AMQP_URL environment variables to set connection details');
     }
     $this->exchange = uniqid('phpunit.');
     $env = function ($key, $default = null) {
         return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
     };
     $this->connection = ConnectionBuilder::make()->create($env('AMQP_URL', ''));
     $this->channel = $this->connection->channel();
     $this->channel->exchange($this->exchange)->define('fanout');
     $this->client = new ServiceBus(new ArrayDiscovery([new Endpoint(new Identity('calc', ['amqp://localhost/?exchange=' . $this->exchange]), new Driver($this->connection, ''))]), new Broadcaster($this->connection, $this->exchange));
     $this->initialize();
 }
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use ServiceBus\Broadcaster;
use ServiceBus\Discovery\ArrayDiscovery;
use ServiceBus\Endpoint;
use ServiceBus\Identity;
use ServiceBus\ServiceBus;
use ServiceBus\Message\Request;
use ServiceBus\Protocol\AMQP;
$env = function ($key, $default = null) {
    return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
};
$amqpUrl = $env('AMQP_URL', 'amqp://*****:*****@localhost/%2f');
$connection = \ButterAMQP\ConnectionBuilder::make()->create($amqpUrl);
$driver = new AMQP\Driver($connection, 'outbox');
// Use AMQP driver for category_repository endpoint to send message over AMQP
$bus = new ServiceBus(new ArrayDiscovery([new Endpoint(new Identity('category_repository', [$amqpUrl . '?exchange=category-repository']), $driver)]), new Broadcaster\NullBroadcaster());
// Publish a message asynchronously
$bus->discover('category_repository')->publish(new Request('category_repository.updated', '1.0', ['category' => ['name' => 'Fruits', 'position' => 10]]));
Ejemplo n.º 5
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use ServiceBus\Broadcaster\NullBroadcaster;
use ServiceBus\Discovery\NullDiscovery;
use ServiceBus\Identity;
use ServiceBus\Service\Handling\CallableHandler;
use ServiceBus\Service\Middleware\Delegate\Router\SimpleRouter;
use ServiceBus\Service\Middleware\RoutingMiddleware;
use ServiceBus\ServiceBus;
use ServiceBus\Protocol\AMQP;
$env = function ($key, $default = null) {
    return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
};
// Driver only matter for sending requests. This example shows how to handle requests,
// so discovery and broadcaster are not important.
$bus = new ServiceBus(new NullDiscovery(), new NullBroadcaster());
// Define a service and few methods
$service = $bus->define(new Identity('calc'))->apply(new RoutingMiddleware(new SimpleRouter(['add' => function ($a, $b) {
    return $a + $b;
}, 'sub' => function ($a, $b) {
    return $a - $b;
}, 'div' => function ($a, $b) {
    return $a / $b;
}])));
$connection = \ButterAMQP\ConnectionBuilder::make()->create($env('AMQP_URL', 'amqp://*****:*****@localhost/%2f'));
// Create topology
$topology = (new AMQP\Topology($connection))->addExchange(new AMQP\Topology\Exchange('calc', 'topic'))->addQueue(new AMQP\Topology\Queue('calc', 0, [new AMQP\Topology\Binding('calc', 'add'), new AMQP\Topology\Binding('calc', 'sub'), new AMQP\Topology\Binding('calc', 'div')]));
// Create consumer
$consumer = new AMQP\Consumer($connection, new AMQP\Handler($service));
$consumer->consume($topology->queue('calc'));
<?php

use ButterAMQP\ConnectionBuilder;
use ServiceBus\Discovery\LookupDiscovery;
use ServiceBus\Discovery\Registry\InMemoryRegistry;
use ServiceBus\Discovery\Registry\LookupRegistry;
use ServiceBus\Discovery\RegistryDiscovery;
use ServiceBus\Driver\ArrayDriver;
use ServiceBus\DriverFactory;
use ServiceBus\Endpoint;
use ServiceBus\Identity;
use ServiceBus\Message\Request;
use ServiceBus\Protocol\JSONRPC;
use ServiceBus\Protocol\AMQP;
use ServiceBus\ServiceBus;
$amqp = ConnectionBuilder::make()->create('amqp://localhost:5672/%2f');
$drivers = new ArrayDriver([new JSONRPC\Driver(), new AMQP\Driver($amqp)]);
// This endpoint will be used to fetch discovery information.
$discovery = new Endpoint(new Identity('discovery', ['jsonrpc://discovery', 'amqp://discovery?exchange=inbox.discovery']), $drivers);
// Lookup registry will try to fetch service identity from in-memory registry and if its not there
// will use discovery service.
$registry = new LookupRegistry($discovery, new InMemoryRegistry());
$bus = new ServiceBus(new RegistryDiscovery($registry, $drivers), new AMQP\Broadcaster($amqp, 'outbox'));
$pong = $bus->discover('ping-pong')->call(new Request('ping', ['payload' => $payload = uniqid('ping')]))->resolve()->getValue();
echo $pong['payload'] == $payload ? 'Service ping-pong working fine!' : 'Reply payload does not match';
echo PHP_EOL;