1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\RpcServerPass; |
6
|
|
|
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\SubscriberPass; |
7
|
|
|
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\WorkerPass; |
8
|
|
|
use Symfony\Component\Config\FileLocator; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
class CmobiRabbitmqExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
public function load(array $configs, ContainerBuilder $container) |
17
|
|
|
{ |
18
|
|
|
$fileLocator = new FileLocator(__DIR__ . '/../Resources/config'); |
19
|
|
|
$loader = new YamlFileLoader($container, $fileLocator); |
20
|
|
|
$loader->load('rabbitmq.yml'); |
21
|
|
|
|
22
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
23
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
24
|
|
|
$this->loadConnections($container, $configs[0]); |
25
|
|
|
$this->loadRpcServers($container, $config); |
26
|
|
|
$this->loadWorkers($container, $config); |
27
|
|
|
|
28
|
|
|
/* Compile and lock container */ |
29
|
|
|
$container->compile(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function loadConnections(ContainerBuilder $container, array $configs) |
33
|
|
|
{ |
34
|
|
|
$factories = []; |
35
|
|
|
|
36
|
|
|
foreach ($configs['connections'] as $name => $connection) { |
37
|
|
|
$connectionClass = '%cmobi_rabbitmq.connection.class%'; |
38
|
|
|
|
39
|
|
|
if ($connection['lazy']) { |
40
|
|
|
$connectionClass = '%cmobi_rabbitmq.lazy.connection.class%'; |
41
|
|
|
} |
42
|
|
|
$definition = new Definition( |
43
|
|
|
'%cmobi_rabbitmq.connection.factory.class%', |
44
|
|
|
[ |
45
|
|
|
$connectionClass, |
46
|
|
|
$connection, |
47
|
|
|
] |
48
|
|
|
); |
49
|
|
|
$factoryName = sprintf('cmobi_rabbitmq.connection.factory.%s', $name); |
50
|
|
|
$container->setDefinition($factoryName, $definition); |
51
|
|
|
$factories[$name] = $factoryName; |
52
|
|
|
} |
53
|
|
|
$container->setParameter('cmobi_rabbitmq.connection.factories', $factories); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function loadRpcServers(ContainerBuilder $container, array $configs) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
foreach ($configs['rpc_servers'] as $server) { |
59
|
|
|
|
60
|
|
|
$container->addCompilerPass(new RpcServerPass( |
61
|
|
|
$server['queue']['name'], |
62
|
|
|
$server['queue']['connection'], |
63
|
|
|
$server['service'], |
64
|
|
|
$server['queue']['basic_qos'], |
65
|
|
|
$server['queue']['durable'], |
66
|
|
|
$server['queue']['auto_delete'], |
67
|
|
|
$server['queue']['arguments'] |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function loadWorkers(ContainerBuilder $container, array $configs) |
73
|
|
|
{ |
74
|
|
|
foreach ($configs['workers'] as $worker) { |
75
|
|
|
$container->addCompilerPass(new WorkerPass( |
76
|
|
|
$worker['queue']['name'], |
77
|
|
|
$worker['queue']['connection'], |
78
|
|
|
$worker['service'], |
79
|
|
|
$worker['queue']['basic_qos'], |
80
|
|
|
$worker['queue']['arguments'] |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function loadSubscribers(ContainerBuilder $container, array $configs) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
foreach ($configs['subscribers'] as $subscriber) { |
88
|
|
|
$container->addCompilerPass(new SubscriberPass( |
89
|
|
|
$subscriber['queue']['exchange'], |
90
|
|
|
$subscriber['queue']['exchange_type'], |
91
|
|
|
$subscriber['queue']['name'], |
92
|
|
|
$subscriber['queue']['connection'], |
93
|
|
|
$subscriber['service'], |
94
|
|
|
$subscriber['queue']['basic_qos'], |
95
|
|
|
$subscriber['queue']['arguments'] |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.