|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ETNA\Silex\Provider\RabbitMQ; |
|
4
|
|
|
|
|
5
|
|
|
use Pimple\Container; |
|
6
|
|
|
use Pimple\ServiceProviderInterface; |
|
7
|
|
|
|
|
8
|
|
|
use ETNA\Silex\Provider\RabbitMQ\RabbitServiceProvider; |
|
9
|
|
|
|
|
10
|
|
|
class RabbitConfig implements ServiceProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $rmq_config; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($rmq_config = null) |
|
15
|
|
|
{ |
|
16
|
|
|
$rmq_config = $rmq_config ?: []; |
|
17
|
|
|
|
|
18
|
|
|
$this->rmq_config['exchanges'] = isset($rmq_config['exchanges']) ? $rmq_config['exchanges'] : []; |
|
19
|
|
|
$this->rmq_config['queues'] = isset($rmq_config['queues']) ? $rmq_config['queues'] : []; |
|
20
|
|
|
$this->rmq_config['connections'] = isset($rmq_config['connections']) ? $rmq_config['connections'] : []; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @{inherit doc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function register(Container $app) |
|
28
|
|
|
{ |
|
29
|
|
|
if (true !== isset($app["application_env"])) { |
|
30
|
|
|
throw new \Exception('$app["application_env"] is not set'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$rmq_url = getenv('RABBITMQ_URL'); |
|
34
|
|
|
$rmq_vhost = getenv('RABBITMQ_VHOST'); |
|
35
|
|
|
|
|
36
|
|
|
if (false === $rmq_url) { |
|
37
|
|
|
throw new \Exception('RABBITMQ_URL is not defined'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (false === $rmq_vhost) { |
|
41
|
|
|
throw new \Exception('RABBITMQ_VHOST is not defined'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$config = parse_url($rmq_url); |
|
45
|
|
|
$rmq_producers = isset($app["rmq_producers"]) ? $app["rmq_producers"] : []; |
|
46
|
|
|
$rmq_consumers = isset($app["rmq_consumers"]) ? $app["rmq_consumers"] : []; |
|
47
|
|
|
|
|
48
|
|
|
foreach (["host", "port", "user", "pass"] as $config_key) { |
|
49
|
|
|
if (!isset($config[$config_key])) { |
|
50
|
|
|
throw new \Exception("Invalid RABBITMQ_URL : cannot resolve {$config_key}"); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$connections = [ |
|
|
|
|
|
|
55
|
|
|
"default" => [ |
|
56
|
|
|
"connection_opt" => [ |
|
57
|
|
|
"verify_peer" => true, |
|
58
|
|
|
"heartbeat" => 30 |
|
59
|
|
|
] |
|
60
|
|
|
] |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
// Set la connection |
|
64
|
|
|
$rabbit_config = [ |
|
65
|
|
|
"rabbit.connections" => array_replace_recursive( |
|
66
|
|
|
[ |
|
67
|
|
|
"default" => [ |
|
68
|
|
|
"host" => $config["host"], |
|
69
|
|
|
"port" => $config["port"], |
|
70
|
|
|
"user" => $config["user"], |
|
71
|
|
|
"password" => $config["pass"], |
|
72
|
|
|
"vhost" => $rmq_vhost, |
|
73
|
|
|
"ssl" => "testing" !== $app["application_env"], |
|
74
|
|
|
"connection_opt" => [ |
|
75
|
|
|
"verify_peer" => true |
|
76
|
|
|
] |
|
77
|
|
|
] |
|
78
|
|
|
], |
|
79
|
|
|
$this->rmq_config['connections'] |
|
80
|
|
|
) |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
// Ajoute les producers |
|
84
|
|
|
if (!empty($rmq_producers)) { |
|
85
|
|
|
$rabbit_config["rabbit.producers"] = $rmq_producers; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Ajoute les consumers |
|
89
|
|
|
if (!empty($rmq_consumers)) { |
|
90
|
|
|
$rabbit_config["rabbit.consumers"] = $rmq_consumers; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$app["rmq.config"] = $rabbit_config; |
|
94
|
|
|
$app['rmq.exchanges'] = $this->rmq_config['exchanges']; |
|
95
|
|
|
$app['rmq.queues'] = $this->rmq_config['queues']; |
|
96
|
|
|
|
|
97
|
|
|
$app->register(new RabbitServiceProvider(), $app["rmq.config"]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.