1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\Queueing\Plugins\StompBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This is the class that loads and manages your bundle configuration |
14
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
15
|
|
|
* |
16
|
|
|
* Logic heavily inspired by the way that Oldsound/RabbitMqBundle does things |
17
|
|
|
*/ |
18
|
|
|
class KaliopQueueingPluginsStompExtension extends Extension |
19
|
|
|
{ |
20
|
|
|
protected $config = array(); |
21
|
|
|
protected $container; |
22
|
|
|
protected $queueManagerService = 'kaliop_queueing.stomp.queue_manager'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
|
|
public function load(array $configs, ContainerBuilder $container) |
28
|
|
|
{ |
29
|
|
|
$this->container = $container; |
30
|
|
|
|
31
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
32
|
|
|
$loader->load('services.yml'); |
33
|
|
|
|
34
|
|
|
$configuration = new Configuration(); |
35
|
|
|
$this->config = $this->processConfiguration($configuration, $configs); |
36
|
|
|
|
37
|
|
|
$this->loadConnections(); |
38
|
|
|
$this->loadProducers(); |
39
|
|
|
$this->loadConsumers(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function loadConnections() |
43
|
|
|
{ |
44
|
|
|
// this is not so much a loading as a 'store definition for later access', really |
45
|
|
|
$definition = $this->container->findDefinition('kaliop_queueing.driver.stomp'); |
46
|
|
|
foreach ($this->config['connections'] as $key => $def) { |
47
|
|
|
$definition->addMethodCall('registerConnection', array($key, $def)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function loadProducers() |
52
|
|
|
{ |
53
|
|
|
$qmDefinition = null; |
54
|
|
|
if ($this->container->hasDefinition($this->queueManagerService)) { |
55
|
|
|
$qmDefinition = $this->container->findDefinition($this->queueManagerService); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($this->config['producers'] as $key => $producer) { |
59
|
|
View Code Duplication |
if (!isset($this->config['connections'][$producer['connection']])) { |
|
|
|
|
60
|
|
|
throw new \RuntimeException("Stomp producer '$key' can not use connection '{$producer['connection']}' because it is not defined in the connections section"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$connectionDefinition = $this->config['connections'][$producer['connection']]; |
64
|
|
|
|
65
|
|
|
$pDefinition = new Definition('%kaliop_queueing.stomp.producer.class%', array($connectionDefinition)); |
66
|
|
|
$pDefinition |
67
|
|
|
->addMethodCall('setStompQueueName', array($producer['queue_options']['name'])) |
68
|
|
|
; |
69
|
|
|
$name = sprintf('kaliop_queueing.stomp.%s_producer', $key); |
70
|
|
|
$this->container->setDefinition($name, $pDefinition); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
//if (!$producer['auto_setup_fabric']) { |
74
|
|
|
// $definition->addMethodCall('disableAutoSetupFabric'); |
75
|
|
|
//} |
76
|
|
|
|
77
|
|
|
if ($qmDefinition) { |
78
|
|
|
$qmDefinition->addMethodCall('registerProducer', array($key)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function loadConsumers() |
84
|
|
|
{ |
85
|
|
|
$qmDefinition = null; |
86
|
|
|
if ($this->container->hasDefinition($this->queueManagerService)) { |
87
|
|
|
$qmDefinition = $this->container->findDefinition($this->queueManagerService); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach ($this->config['consumers'] as $key => $consumer) { |
91
|
|
View Code Duplication |
if (!isset($this->config['connections'][$consumer['connection']])) { |
|
|
|
|
92
|
|
|
throw new \RuntimeException("Stomp consumer '$key' can not use connection '{$consumer['connection']}' because it is not defined in the connections section"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$connectionDefinition = $this->config['connections'][$consumer['connection']]; |
96
|
|
|
|
97
|
|
|
$cDefinition = new Definition('%kaliop_queueing.stomp.consumer.class%', array($connectionDefinition)); |
98
|
|
|
$cDefinition |
99
|
|
|
->addMethodCall('setSubscriptionName', array($consumer['subscription_options']['name'])) |
100
|
|
|
->addMethodCall('setStompQueueName', array($consumer['queue_options']['name'])) |
101
|
|
|
->addMethodCall('setCallback', array(new Reference($consumer['callback']))); |
102
|
|
|
; |
103
|
|
|
if (count($consumer['subscription_options']['routing_keys'])) { |
104
|
|
|
$cDefinition->addMethodCall('setRoutingKey', array(reset($consumer['subscription_options']['routing_keys']))); |
105
|
|
|
} |
106
|
|
|
$name = sprintf('kaliop_queueing.stomp.%s_consumer', $key); |
107
|
|
|
$this->container->setDefinition($name, $cDefinition); |
108
|
|
|
|
109
|
|
|
//if (!$consumer['auto_setup_fabric']) { |
110
|
|
|
// $definition->addMethodCall('disableAutoSetupFabric'); |
111
|
|
|
//} |
112
|
|
|
|
113
|
|
|
if ($qmDefinition) { |
114
|
|
|
$qmDefinition->addMethodCall('registerConsumer', array($key)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.