1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Portiny\RabbitMQ\Adapter\Nette\DI; |
4
|
|
|
|
5
|
|
|
use Nette\DI\CompilerExtension; |
6
|
|
|
use Nette\DI\ContainerBuilder; |
7
|
|
|
use Portiny\RabbitMQ\BunnyManager; |
8
|
|
|
use Portiny\RabbitMQ\Command\ConsumeCommand; |
9
|
|
|
use Portiny\RabbitMQ\Command\DeclareCommand; |
10
|
|
|
use Portiny\RabbitMQ\Consumer\AbstractConsumer; |
11
|
|
|
use Portiny\RabbitMQ\Exchange\AbstractExchange; |
12
|
|
|
use Portiny\RabbitMQ\Producer\AbstractProducer; |
13
|
|
|
use Portiny\RabbitMQ\Producer\Producer; |
14
|
|
|
use Portiny\RabbitMQ\Queue\AbstractQueue; |
15
|
|
|
use Symfony\Component\Console\Application; |
16
|
|
|
|
17
|
|
|
final class RabbitMQExtension extends CompilerExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $defaults = [ |
23
|
|
|
'aliases' => [], |
24
|
|
|
'connection' => [ |
25
|
|
|
'host' => '127.0.0.1', |
26
|
|
|
'port' => 5672, |
27
|
|
|
'user' => 'guest', |
28
|
|
|
'password' => 'guest', |
29
|
|
|
'vhost' => '/', |
30
|
|
|
'timeout' => 1, |
31
|
|
|
'heartbeat' => 60.0, |
32
|
|
|
'persistent' => false, |
33
|
|
|
'path' => '/', |
34
|
|
|
'tcp_nodelay' => false, |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function loadConfiguration(): void |
42
|
|
|
{ |
43
|
1 |
|
$builder = $this->getContainerBuilder(); |
44
|
|
|
|
45
|
1 |
|
$builder->addDefinition($this->prefix('producer')) |
46
|
1 |
|
->setType(Producer::class); |
47
|
|
|
|
48
|
|
|
// import RabbitMQ commands into Symfony/Console if exists |
49
|
1 |
|
$this->registerCommandsIntoConsole($builder); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function beforeCompile(): void |
56
|
|
|
{ |
57
|
1 |
|
$builder = $this->getContainerBuilder(); |
58
|
1 |
|
$config = $this->createConfig($builder); |
59
|
|
|
|
60
|
1 |
|
$this->setupBunnyManager($builder, $config); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
private function registerCommandsIntoConsole(ContainerBuilder $containerBuilder): void |
64
|
|
|
{ |
65
|
1 |
|
if ($this->hasSymfonyConsole()) { |
66
|
1 |
|
$commands = [ConsumeCommand::class, DeclareCommand::class]; |
67
|
1 |
|
foreach ($commands as $index => $command) { |
68
|
1 |
|
$containerBuilder->addDefinition($this->prefix('command.' . $index)) |
69
|
1 |
|
->setType($command); |
70
|
|
|
} |
71
|
|
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
private function createConfig(ContainerBuilder $containerBuilder): array |
75
|
|
|
{ |
76
|
1 |
|
$config = $this->validateConfig($this->defaults); |
77
|
|
|
|
78
|
1 |
|
$config['consumers'] = []; |
79
|
1 |
|
foreach ($containerBuilder->findByType(AbstractConsumer::class) as $serviceDefinition) { |
80
|
1 |
|
$config['consumers'][] = $serviceDefinition->getType(); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$config['exchanges'] = []; |
84
|
1 |
|
foreach ($containerBuilder->findByType(AbstractExchange::class) as $serviceDefinition) { |
85
|
1 |
|
$config['exchanges'][] = $serviceDefinition->getType(); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$config['publishers'] = []; |
89
|
1 |
|
foreach ($containerBuilder->findByType(AbstractProducer::class) as $serviceDefinition) { |
90
|
1 |
|
$config['publishers'][] = $serviceDefinition->getType(); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$config['queues'] = []; |
94
|
1 |
|
foreach ($containerBuilder->findByType(AbstractQueue::class) as $serviceDefinition) { |
95
|
1 |
|
$config['queues'][] = $serviceDefinition->getType(); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $config; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
private function setupBunnyManager(ContainerBuilder $containerBuilder, array $config): void |
102
|
|
|
{ |
103
|
1 |
|
$containerBuilder->addDefinition($this->prefix('bunnyManager')) |
104
|
1 |
|
->setFactory(BunnyManager::class, ['@container', $config]); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
private function hasSymfonyConsole(): bool |
108
|
|
|
{ |
109
|
1 |
|
return class_exists(Application::class); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|