Skip to content

Commit da72ee8

Browse files
authored
Merge pull request #7 from symfony-bundles/2.0
Release v2.0.0
2 parents a3aed4e + 3b54878 commit da72ee8

File tree

21 files changed

+175
-107
lines changed

21 files changed

+175
-107
lines changed

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ build:
1212
tests:
1313
override:
1414
-
15-
command: phpunit --coverage-clover ./clover.xml
15+
command: ./vendor/bin/phpunit --coverage-clover ./clover.xml
1616
coverage:
1717
file: clover.xml
1818
format: clover

.travis.yml

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
language: php
2-
32
sudo: false
43

5-
cache:
6-
directories:
7-
- $HOME/.composer/cache
8-
94
php:
105
- 5.6
116
- 7.0
7+
- 7.1
8+
- 7.2
129
- hhvm
13-
- nightly
10+
11+
env:
12+
- SYMFONY_VERSION=2.7.*
13+
- SYMFONY_VERSION=2.8.*
14+
- SYMFONY_VERSION=3.0.*
15+
- SYMFONY_VERSION=3.1.*
16+
- SYMFONY_VERSION=3.2.*
17+
- SYMFONY_VERSION=3.3.*
1418

1519
services:
1620
- redis-server
1721

22+
cache:
23+
directories:
24+
- $HOME/.composer/cache
25+
1826
before_install:
1927
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" < "7.1" ]; then phpenv config-rm xdebug.ini; fi
2028
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
@@ -26,22 +34,4 @@ install:
2634
- composer install
2735

2836
script:
29-
- phpunit
30-
31-
matrix:
32-
fast_finish: true
33-
include:
34-
- php: 5.6
35-
env: SYMFONY_VERSION=2.7.*
36-
- php: 5.6
37-
env: SYMFONY_VERSION=2.8.*
38-
- php: 5.6
39-
env: SYMFONY_VERSION=3.0.*
40-
- php: 7.0
41-
env: SYMFONY_VERSION=3.0.*
42-
allow_failures:
43-
- php: hhvm
44-
- php: nightly
45-
46-
notifications:
47-
email: false
37+
- ./vendor/bin/phpunit

DependencyInjection/Configuration.php

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,98 @@
22

33
namespace SymfonyBundles\QueueBundle\DependencyInjection;
44

5+
use SymfonyBundles\QueueBundle\Service;
56
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
67
use Symfony\Component\Config\Definition\ConfigurationInterface;
8+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
79

810
class Configuration implements ConfigurationInterface
911
{
10-
1112
/**
1213
* {@inheritdoc}
1314
*/
1415
public function getConfigTreeBuilder()
1516
{
1617
$builder = new TreeBuilder();
18+
$rootNode = $builder->root('sb_queue');
19+
20+
$this->addServiceSection($rootNode);
21+
$this->addSettingsSection($rootNode);
22+
$this->addStoragesSection($rootNode);
23+
24+
return $builder;
25+
}
1726

18-
$builder->root('sb_queue')
19-
->addDefaultsIfNotSet()
27+
/**
28+
* Adds the sb_queue.service configuration.
29+
*
30+
* @param ArrayNodeDefinition $node
31+
*/
32+
private function addServiceSection(ArrayNodeDefinition $node)
33+
{
34+
$node
2035
->children()
21-
->scalarNode('service_name')
22-
->defaultValue('queue')
23-
->cannotBeEmpty()
24-
->end()
25-
->scalarNode('default_name')
26-
->defaultValue('queue:default')
27-
->cannotBeEmpty()
36+
->arrayNode('service')
37+
->addDefaultsIfNotSet()
38+
->children()
39+
->scalarNode('class')
40+
->defaultValue(Service\Queue::class)->cannotBeEmpty()
41+
->end()
42+
->scalarNode('alias')
43+
->defaultValue('queue')->cannotBeEmpty()
44+
->end()
45+
->scalarNode('storage')
46+
->defaultValue('redis')->cannotBeEmpty()
47+
->end()
48+
->end()
2849
->end()
2950
->end();
51+
}
3052

31-
return $builder;
53+
/**
54+
* Adds the sb_queue.settings configuration.
55+
*
56+
* @param ArrayNodeDefinition $node
57+
*/
58+
private function addSettingsSection(ArrayNodeDefinition $node)
59+
{
60+
$node
61+
->children()
62+
->arrayNode('settings')
63+
->addDefaultsIfNotSet()
64+
->children()
65+
->scalarNode('queue_default_name')
66+
->defaultValue('queue:default')->cannotBeEmpty()
67+
->end()
68+
->end()
69+
->end()
70+
->end();
3271
}
3372

73+
/**
74+
* Adds the sb_queue.storages configuration.
75+
*
76+
* @param ArrayNodeDefinition $node
77+
*/
78+
private function addStoragesSection(ArrayNodeDefinition $node)
79+
{
80+
$node
81+
->children()
82+
->arrayNode('storages')
83+
->addDefaultChildrenIfNoneSet('redis')
84+
->requiresAtLeastOneElement()
85+
->useAttributeAsKey('redis')
86+
->prototype('array')
87+
->children()
88+
->scalarNode('class')
89+
->defaultValue(Service\Storage\RedisStorage::class)->cannotBeEmpty()
90+
->end()
91+
->scalarNode('client')
92+
->defaultValue('sb_redis.client.default')->cannotBeEmpty()
93+
->end()
94+
->end()
95+
->end()
96+
->end()
97+
->end();
98+
}
3499
}

DependencyInjection/QueueExtension.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,55 @@
22

33
namespace SymfonyBundles\QueueBundle\DependencyInjection;
44

5-
use Symfony\Component\Config\FileLocator;
6-
use Symfony\Component\DependencyInjection\Loader;
5+
use Symfony\Component\DependencyInjection\Reference;
6+
use Symfony\Component\DependencyInjection\Definition;
77
use Symfony\Component\DependencyInjection\ContainerBuilder;
88
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
99

1010
class QueueExtension extends ConfigurableExtension
1111
{
12-
1312
/**
1413
* {@inheritdoc}
1514
*/
1615
protected function loadInternal(array $configs, ContainerBuilder $container)
1716
{
18-
$loader = new Loader\YamlFileLoader(
19-
$container, new FileLocator(__DIR__ . '/../Resources/config')
20-
);
17+
$this->addService($configs, $container);
18+
$this->addStorage($configs, $container);
19+
}
20+
21+
/**
22+
* @param array $configs
23+
* @param ContainerBuilder $container
24+
*/
25+
private function addService(array $configs, ContainerBuilder $container)
26+
{
27+
$alias = $this->getAlias();
28+
$storageReference = new Reference('sb_queue.storage');
2129

22-
$loader->load('services.yml');
30+
$service = new Definition($configs['service']['class']);
31+
$service->addMethodCall('setName', [$configs['settings']['queue_default_name']]);
32+
$service->addMethodCall('setStorage', [$storageReference]);
2333

24-
$container->setAlias($configs['service_name'], 'sb_queue');
25-
$container->setParameter('sb_queue.default_name', $configs['default_name']);
34+
$container->setDefinition($alias, $service);
35+
$container->setAlias($configs['service']['alias'], $alias);
36+
}
37+
38+
/**
39+
* @param array $configs
40+
* @param ContainerBuilder $container
41+
*/
42+
private function addStorage(array $configs, ContainerBuilder $container)
43+
{
44+
if (false === isset($configs['storages'][$configs['service']['storage']])) {
45+
throw new \InvalidArgumentException(sprintf('Not available storage `%s`.', $configs['service']['storage']));
46+
}
47+
48+
$parameters = $configs['storages'][$configs['service']['storage']];
49+
50+
$storage = new Definition($parameters['class']);
51+
$storage->addArgument(new Reference($parameters['client']));
52+
53+
$container->setDefinition('sb_queue.storage', $storage);
2654
}
2755

2856
/**
@@ -32,5 +60,4 @@ public function getAlias()
3260
{
3361
return 'sb_queue';
3462
}
35-
3663
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 Symfony Bundles (Dmitry Khaperets)
3+
Copyright (c) 2016-2017 Symfony Bundles (Dmitry Khaperets)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ public function registerBundles()
3737
Defaults configuration:
3838
``` yml
3939
sb_queue:
40-
service_name: "queue"
41-
default_name: "queue:default"
40+
service:
41+
alias: 'queue' # alias for service `sb_queue` (e.g. $this->get('queue'))
42+
class: 'SymfonyBundles\QueueBundle\Service\Queue'
43+
storage: 'redis' # storage key from `queue.storages` section
44+
settings:
45+
queue_default_name: 'queue:default' # default name for queue
46+
storages:
47+
redis:
48+
class: 'SymfonyBundles\QueueBundle\Service\Storage\RedisStorage'
49+
client: 'sb_redis.client.default' # storage client service id
4250
```
4351
4452
* Configure the redis client in your config.yml. Read more about [RedisBundle configuration][redis-bundle-link].
@@ -60,6 +68,21 @@ $queue->push(new \stdClass);
6068
$queue->count(); // returns integer: 3
6169
```
6270
71+
If you want to change the queue:
72+
```
73+
// adding data to queue `notifications`
74+
$queue->setName('application:notifications');
75+
$queue->push('You have a new message from Jessica');
76+
77+
// adding data to queue `settings`
78+
$queue->setName('account:settings');
79+
$queue->push('User with ID 123 changed password');
80+
81+
// adding data to default queue
82+
$queue->setName('queue:default');
83+
$queue->push('To be or not to be');
84+
```
85+
6386
``` php
6487
// now, we can get the data at any time in the queue order
6588

Resources/config/services.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

Service/Queue.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class Queue implements QueueInterface
66
{
7-
87
/**
98
* @var string
109
*/
@@ -62,5 +61,4 @@ public function count()
6261
{
6362
return $this->storage->count($this->name);
6463
}
65-
6664
}

Service/QueueInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@
44

55
interface QueueInterface
66
{
7-
87
/**
98
* Sets the storage client.
109
*
1110
* @param Storage\StorageInterface $storage
12-
*
13-
* @return void
1411
*/
1512
public function setStorage(Storage\StorageInterface $storage);
1613

1714
/**
1815
* Sets the name of the queue list.
1916
*
2017
* @param string $name
21-
*
22-
* @return void
2318
*/
2419
public function setName($name);
2520

@@ -41,8 +36,6 @@ public function pop();
4136
* Append the value into the end of list.
4237
*
4338
* @param mixed $value Pushes value of the list.
44-
*
45-
* @return void
4639
*/
4740
public function push($value);
4841

Service/Storage/RedisStorage.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
class RedisStorage implements StorageInterface
88
{
9-
109
/**
1110
* @var ClientInterface
1211
*/
@@ -59,5 +58,4 @@ public function count($key)
5958
{
6059
return $this->client->llen($key);
6160
}
62-
6361
}

0 commit comments

Comments
 (0)