1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RabbitMqModule\Controller; |
4
|
|
|
|
5
|
|
|
use RabbitMqModule\Service\SetupFabricAwareInterface; |
6
|
|
|
use Zend\Console\ColorInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SetupFabricController |
10
|
|
|
* @package RabbitMqModule\Controller |
11
|
|
|
*/ |
12
|
|
|
class SetupFabricController extends AbstractConsoleController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Default action if none provided |
16
|
|
|
* |
17
|
|
|
* @return \Zend\Console\Response |
18
|
|
|
*/ |
19
|
2 |
|
public function indexAction() |
20
|
|
|
{ |
21
|
|
|
/** @var \Zend\Console\Response $response */ |
22
|
2 |
|
$response = $this->getResponse(); |
23
|
2 |
|
$this->getConsole()->writeLine('Setting up the AMQP fabric'); |
24
|
|
|
|
25
|
|
|
try { |
26
|
2 |
|
$services = $this->getServiceParts(); |
27
|
|
|
|
28
|
1 |
|
foreach ($services as $service) { |
29
|
1 |
|
if (!$service instanceof SetupFabricAwareInterface) { |
30
|
1 |
|
continue; |
31
|
|
|
} |
32
|
1 |
|
$service->setupFabric(); |
33
|
1 |
|
} |
34
|
2 |
|
} catch (\Exception $e) { |
35
|
1 |
|
$response->setErrorLevel(1); |
36
|
1 |
|
$this->getConsole()->writeText(sprintf('Exception: %s', $e->getMessage()), ColorInterface::LIGHT_RED); |
37
|
|
|
|
38
|
1 |
|
return $response; |
39
|
|
|
} finally { |
40
|
2 |
|
return $response; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return SetupFabricAwareInterface[] |
46
|
|
|
*/ |
47
|
2 |
|
protected function getServiceParts() |
48
|
|
|
{ |
49
|
|
|
$serviceKeys = [ |
50
|
2 |
|
'consumer', |
51
|
2 |
|
'producer', |
52
|
2 |
|
'rpc_client', |
53
|
2 |
|
'rpc_server', |
54
|
2 |
|
]; |
55
|
2 |
|
$parts = []; |
56
|
2 |
|
foreach ($serviceKeys as $serviceKey) { |
57
|
2 |
|
$keys = $this->getServiceKeys($serviceKey); |
58
|
1 |
|
foreach ($keys as $key) { |
59
|
1 |
|
$parts[] = $this->getServiceLocator()->get(sprintf('rabbitmq_module.%s.%s', $serviceKey, $key)); |
60
|
1 |
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
return $parts; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @param $service |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
2 |
|
protected function getServiceKeys($service) |
72
|
|
|
{ |
73
|
|
|
/** @var array $config */ |
74
|
2 |
|
$config = $this->getServiceLocator()->get('Configuration'); |
75
|
2 |
|
if (!isset($config['rabbitmq_module'][$service])) { |
76
|
1 |
|
throw new \RuntimeException(sprintf('No service "rabbitmq_module.%s" found in configuration', $service)); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return array_keys($config['rabbitmq_module'][$service]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|