Skip to content

Commit

Permalink
Merge pull request #7 from faouzic/feature/28934
Browse files Browse the repository at this point in the history
[FEATURE#28934] RabbitMQ v2
  • Loading branch information
SparSio committed May 27, 2016
2 parents 67426fb + b03703d commit e58a024
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": ">=5.5",
"silex/silex": "~2.0@dev",
"etna/rabbitmq-service-provider": "0.x"
"etna/rabbitmq-service-provider": "2.x-dev"
},
"config": {
"bin-dir": "bin"
Expand All @@ -31,7 +31,7 @@
"repositories": [
{
"type": "composer",
"url": "http://blu-composer.herokuapp.com/"
"url": "https://blu-composer.herokuapp.com/"
}
]
}
42 changes: 26 additions & 16 deletions src/SPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,55 @@

namespace ETNA\Silex\Provider\SPrinter;

use ETNA\Silex\Provider\RabbitMQ\Queue;
use OldSound\RabbitMqBundle\RabbitMq\Producer;
use Silex\Application;

/**
*
*/
class SPrinter
{
public function __construct($exchange, $options)
public function __construct($app)
{
$this->exchange = $exchange;
$this->routing_key = $options["default.routing_key"];
// On crée un producer pour publier des jobs
$connection = $app['rabbit.connections']['default'];
$producer = new Producer($connection);
$producer->setExchangeOptions([
"name" => "SPrinter",
"channel" => "default",
"type" => "direct",
"passive" => false,
"durable" => true,
"auto_delete" => false,
]);

$this->producer = $producer;
$this->routing_key = $app["sprinter.options"]["default.routing_key"];
}

public function getDefaultRoutingKey()
{
return $this->routing_key;
}

public function getProducer()
{
return $this->producer;
}

public function sendPrint($template, $data, $print_flag, $routing_key = null, $opt = null)
{
$queue_opt = [
"passive" => false,
"durable" => true,
"exclusive" => false,
"auto_delete" => false,
];
$params = [
$print_params = [
"template" => $template,
"data" => $data,
"printflag" => $print_flag
];
if ($opt) {
$params = array_merge($params, $opt);
$print_params = array_merge($print_params, $opt);
}

$routing_key = $routing_key ?: $this->routing_key;

// crée la queue au besoin
$queue = new Queue($routing_key, $this->exchange, $this->exchange->getChannel(), $queue_opt);

$this->exchange->send($params, $routing_key);
$this->producer->publish($print_params, $routing_key);
}
}
18 changes: 10 additions & 8 deletions src/SPrinterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ETNA\Silex\Provider\SPrinter;

use Silex\Application;
use Pimple\ServiceProviderInterface;
use Pimple\Container;

Expand All @@ -13,23 +12,26 @@ class SPrinterServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
if (!isset($app["sprinter.options"]) || !isset($app["sprinter.options"]["default.routing_key"])) {
if (false === isset($app["sprinter.options"]) || false === isset($app["sprinter.options"]["default.routing_key"])) {
$environment_key = "{$app["application_name"]}_SPRINTER_ROUTING_KEY";
$routing_key = getenv(strtoupper($environment_key));

if (false === $routing_key) {
throw new Exception("No sprinter routing key found for environment {$environment_key}");
throw new \Exception("No sprinter routing key found for environment {$environment_key}");
}

$app["sprinter.options"] = [
"default.routing_key" => $routing_key,
];
}

$app["sprinter"] = $app->share(
function (Application $app) {
return new SPrinter($app["amqp.exchanges"]["SPrinter"], $app["sprinter.options"]);
}
);
// On vérifie que la connection rmq est bien settée
if (false === isset($app['rabbit.connections']) || false === isset($app['rabbit.connections']['default'])) {
throw new \Exception("RabbitMQ default connection not set");
}

$app["sprinter"] = function ($app) {
return new SPrinter($app);
};
}
}

0 comments on commit e58a024

Please sign in to comment.