Skip to content

Commit 9f70222

Browse files
committed
Merge pull request #57 from jeskew/feature/named-providers
Feature/named providers
2 parents d6a0e38 + 34aff09 commit 9f70222

8 files changed

Lines changed: 143 additions & 51 deletions

File tree

docs/aws-provider.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ credentials in your configuration.
2828
2929
uecode_qpush:
3030
providers:
31-
aws:
31+
my_provider:
32+
driver: aws
3233
key: <aws key>
3334
secret: <aws secret>
3435
region: us-east-1
3536
queues:
3637
my_queue_name:
37-
provider: aws
38+
provider: my_provider
3839
options:
3940
push_notifications: true
4041
subscribers:

docs/configuration.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,24 @@ A working configuration would look like the following
107107
logging_enabled: true
108108
providers:
109109
aws:
110+
driver: aws #optional for providers named 'aws' or 'ironmq'
111+
key: YOUR_AWS_KEY_HERE
112+
secret: YOUR_AWS_SECRET_HERE
113+
region: YOUR_AWS_REGION_HERE
114+
another_aws_provider:
115+
driver: aws #required for named providers
110116
key: YOUR_AWS_KEY_HERE
111117
secret: YOUR_AWS_SECRET_HERE
112118
region: YOUR_AWS_REGION_HERE
113119
ironmq:
120+
driver: aws #optional for providers named 'aws' or 'ironmq'
114121
token: YOUR_IRONMQ_TOKEN_HERE
115122
project_id: YOUR_IRONMQ_PROJECT_ID_HERE
123+
in_band:
124+
driver: sync
116125
queues:
117126
my_queue_key:
118-
provider: ironmq #or aws or sync
127+
provider: ironmq #or aws or in_band or another_aws_provider
119128
options:
120129
queue_name: my_actual_queue_name
121130
push_notifications: true

docs/iron-mq-provider.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,24 @@ an account and have a project id.
3030
testing and using this service extremely easy.
3131

3232
Just include your OAuth `token` and `project_id` in the configuration and set your
33-
queue to use the `ironmq` provider.
33+
queue to use a provider using the `ironmq` driver.
3434

3535
.. code-block:: yaml
3636
3737
#app/config.yml
3838
3939
uecode_qpush:
4040
providers:
41-
ironmq:
41+
my_provider:
42+
driver: ironmq
4243
token: YOUR_TOKEN_HERE
4344
project_id: YOUR_PROJECT_ID_HERE
4445
host: YOUR_OPTIONAL_HOST_HERE
4546
port: YOUR_OPTIONAL_PORT_HERE
4647
version_id: YOUR_OPTIONAL_VERSION_HERE
4748
queues:
4849
my_queue_name:
49-
provider: ironmq
50+
provider: my_provider
5051
options:
5152
push_notifications: true
5253
subscribers:

docs/sync-provider.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ of queue-based code paths.
88
Configuration
99
^^^^^^^^^^^^^
1010

11-
To use the sync queue, set the ``provider`` of a given queue to ``sync``. No further
11+
To designate a queue as synchronous, set the ``driver`` of its provider to ``sync``. No further
1212
configuration is necessary.
1313

1414
.. code-block:: yaml
1515
1616
#app/config_dev.yml
1717
1818
uecode_qpush:
19+
providers:
20+
in_band:
21+
driver: sync
1922
queues:
2023
my_queue_name:
21-
provider: sync
24+
provider: in_band

src/DependencyInjection/Configuration.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,60 @@ private function getProvidersNode()
5656
{
5757
$treeBuilder = new TreeBuilder();
5858
$node = $treeBuilder->root('providers');
59+
$requirements = [
60+
'aws' => ['key', 'secret'],
61+
'ironmq' => ['token', 'project_id'],
62+
'sync' => [],
63+
];
5964

6065
$node
61-
->children()
62-
->arrayNode('aws')
63-
->children()
64-
->scalarNode('key')->end()
65-
->scalarNode('secret')->end()
66-
->scalarNode('region')
67-
->defaultValue('us-east-1')
68-
->end()
66+
->useAttributeAsKey('name')
67+
->prototype('array')
68+
->treatNullLike([])
69+
->children()
70+
->enumNode('driver')
71+
->isRequired()
72+
->values(array_keys($requirements))
6973
->end()
70-
->end()
71-
->arrayNode('ironmq')
72-
->children()
73-
->scalarNode('token')->end()
74-
->scalarNode('project_id')->end()
75-
->enumNode('host')
76-
->defaultValue('mq-aws-us-east-1')
77-
->values([
78-
'mq-aws-us-east-1',
79-
'mq-aws-eu-west-1',
80-
'mq-rackspace-ord',
81-
'mq-rackspace-lon'
82-
])
83-
->end()
84-
->scalarNode('port')
85-
->defaultValue('443')
86-
->end()
87-
->scalarNode('api_version')
88-
->defaultValue(1)
89-
->end()
74+
// IronMQ
75+
->scalarNode('token')->end()
76+
->scalarNode('project_id')->end()
77+
->enumNode('host')
78+
->defaultValue('mq-aws-us-east-1')
79+
->values([
80+
'mq-aws-us-east-1',
81+
'mq-aws-eu-west-1',
82+
'mq-rackspace-ord',
83+
'mq-rackspace-lon',
84+
])
85+
->end()
86+
->scalarNode('port')
87+
->defaultValue('443')
88+
->end()
89+
->scalarNode('api_version')
90+
->defaultValue(1)
91+
->end()
92+
// AWS
93+
->scalarNode('key')->end()
94+
->scalarNode('secret')->end()
95+
->scalarNode('region')
96+
->defaultValue('us-east-1')
9097
->end()
9198
->end()
99+
100+
->validate()
101+
->always()
102+
->then(function (array $provider) use ($node, $requirements) {
103+
foreach ($requirements[$provider['driver']] as $requirement) {
104+
if (empty($provider[$requirement])) {
105+
throw new \InvalidArgumentException(
106+
sprintf('%s queue providers must have a %s; none provided', $provider['driver'], $requirement)
107+
);
108+
}
109+
}
110+
111+
return $provider;
112+
})
92113
->end()
93114
;
94115

src/DependencyInjection/UecodeQPushExtension.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,21 @@ public function load(array $configs, ContainerBuilder $container)
6060
$class = null;
6161
$client = null;
6262

63-
switch ($provider) {
63+
switch ($config['providers'][$provider]['driver']) {
6464
case 'aws':
6565
$class = $container->getParameter('uecode_qpush.provider.aws');
6666
$client = $this->createAwsClient(
6767
$config['providers'][$provider],
68-
$container
68+
$container,
69+
$provider
6970
);
7071
break;
7172
case 'ironmq':
7273
$class = $container->getParameter('uecode_qpush.provider.ironmq');
7374
$client = $this->createIronMQClient(
7475
$config['providers'][$provider],
75-
$container
76+
$container,
77+
$provider
7678
);
7779
break;
7880
case 'sync':
@@ -116,12 +118,15 @@ public function load(array $configs, ContainerBuilder $container)
116118
*
117119
* @param array $config A Configuration array for the client
118120
* @param ContainerBuilder $container The container
121+
* @param string $name The provider key
119122
*
120-
* return Reference
123+
* @return Reference
121124
*/
122-
private function createAwsClient($config, ContainerBuilder $container)
125+
private function createAwsClient($config, ContainerBuilder $container, $name)
123126
{
124-
if (!$container->hasDefinition('uecode_qpush.provider.aws')) {
127+
$service = sprintf('uecode_qpush.provider.%s', $name);
128+
129+
if (!$container->hasDefinition($service)) {
125130

126131
if (!class_exists('Aws\Common\Aws')) {
127132
throw new \RuntimeException(
@@ -140,24 +145,27 @@ private function createAwsClient($config, ContainerBuilder $container)
140145
]
141146
]);
142147

143-
$container->setDefinition('uecode_qpush.provider.aws', $aws)
148+
$container->setDefinition($service, $aws)
144149
->setPublic(false);
145150
}
146151

147-
return new Reference('uecode_qpush.provider.aws');
152+
return new Reference($service);
148153
}
149154

150155
/**
151156
* Creates a definition for the IronMQ provider
152157
*
153158
* @param array $config A Configuration array for the provider
154159
* @param ContainerBuilder $container The container
160+
* @param string $name The provider key
155161
*
156-
* return Reference
162+
* @return Reference
157163
*/
158-
private function createIronMQClient($config, ContainerBuilder $container)
164+
private function createIronMQClient($config, ContainerBuilder $container, $name)
159165
{
160-
if (!$container->hasDefinition('uecode_qpush.provider.ironmq')) {
166+
$service = sprintf('uecode_qpush.provider.%s', $name);
167+
168+
if (!$container->hasDefinition($service)) {
161169

162170
if (!class_exists('IronMQ')) {
163171
throw new \RuntimeException(
@@ -176,11 +184,11 @@ private function createIronMQClient($config, ContainerBuilder $container)
176184
]
177185
]);
178186

179-
$container->setDefinition('uecode_qpush.provider.ironmq', $ironmq)
187+
$container->setDefinition($service, $ironmq)
180188
->setPublic(false);
181189
}
182190

183-
return new Reference('uecode_qpush.provider.ironmq');
191+
return new Reference($service);
184192
}
185193

186194
private function createSyncClient()

tests/DependencyInjection/UecodeQPushExtensionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,19 @@ public function testConfiguration()
6565
$this->container->compile();
6666

6767
$this->assertTrue($this->container->has('uecode_qpush'));
68+
6869
$this->assertTrue($this->container->has('uecode_qpush.test_aws'));
70+
$this->assertTrue($this->container->has('uecode_qpush.test_secondary_aws'));
71+
$this->assertNotSame(
72+
$this->container->get('uecode_qpush.test_aws'),
73+
$this->container->get('uecode_qpush.test_secondary_aws')
74+
);
75+
6976
$this->assertTrue($this->container->has('uecode_qpush.test_ironmq'));
77+
$this->assertTrue($this->container->has('uecode_qpush.test_secondary_ironmq'));
78+
$this->assertNotSame(
79+
$this->container->get('uecode_qpush.test_ironmq'),
80+
$this->container->get('uecode_qpush.test_secondary_ironmq')
81+
);
7082
}
7183
}

tests/Fixtures/config_test.yml

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ uecode_qpush:
33
logging_enabled: true
44
providers:
55
aws:
6+
driver: aws
67
key: 123
78
secret: 123
89
region: us-east-1
10+
secondary_aws:
11+
driver: aws
12+
key: 234
13+
secret: 432
14+
region: eu-west-1
915
ironmq:
16+
driver: ironmq
1017
token: 123
1118
project_id: 123
19+
secondary_ironmq:
20+
driver: ironmq
21+
token: 234
22+
project_id: 234
1223
queues:
1324
test_aws:
1425
provider: aws
@@ -22,6 +33,18 @@ uecode_qpush:
2233
receive_wait_time: 3
2334
subscribers:
2435
- { endpoint: http://example.com/qpush, protocol: http }
36+
test_secondary_aws:
37+
provider: secondary_aws
38+
options:
39+
push_notifications: true
40+
notification_retries: 3
41+
message_delay: 0
42+
message_timeout: 30
43+
message_expiration: 604800
44+
messages_to_receive: 1
45+
receive_wait_time: 3
46+
subscribers:
47+
- { endpoint: http://example.com/qpush, protocol: http }
2548
test_ironmq:
2649
provider: ironmq
2750
options:
@@ -34,9 +57,23 @@ uecode_qpush:
3457
receive_wait_time: 3
3558
subscribers:
3659
- { endpoint: http://example.com/qpush, protocol: http }
60+
test_secondary_ironmq:
61+
provider: secondary_ironmq
62+
options:
63+
push_notifications: true
64+
notification_retries: 3
65+
message_delay: 0
66+
message_timeout: 30
67+
message_expiration: 604800
68+
messages_to_receive: 1
69+
receive_wait_time: 3
70+
subscribers:
71+
- { endpoint: http://example.com/qpush, protocol: http }
3772

3873
services:
3974
event_dispatcher:
40-
class: stdObject
75+
class: stdClass
4176
logger:
42-
class: stdObject
77+
class: Symfony\Bridge\Monolog\Logger
78+
arguments:
79+
- 'test'

0 commit comments

Comments
 (0)