Skip to content

Commit

Permalink
Merge pull request #7 from symfony-bundles/2.0
Browse files Browse the repository at this point in the history
Release v2.0.0
  • Loading branch information
khaperets committed Oct 18, 2017
2 parents a3aed4e + 3b54878 commit da72ee8
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
tests:
override:
-
command: phpunit --coverage-clover ./clover.xml
command: ./vendor/bin/phpunit --coverage-clover ./clover.xml
coverage:
file: clover.xml
format: clover
40 changes: 15 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm
- nightly

env:
- SYMFONY_VERSION=2.7.*
- SYMFONY_VERSION=2.8.*
- SYMFONY_VERSION=3.0.*
- SYMFONY_VERSION=3.1.*
- SYMFONY_VERSION=3.2.*
- SYMFONY_VERSION=3.3.*

services:
- redis-server

cache:
directories:
- $HOME/.composer/cache

before_install:
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" < "7.1" ]; then phpenv config-rm xdebug.ini; fi
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
Expand All @@ -26,22 +34,4 @@ install:
- composer install

script:
- phpunit

matrix:
fast_finish: true
include:
- php: 5.6
env: SYMFONY_VERSION=2.7.*
- php: 5.6
env: SYMFONY_VERSION=2.8.*
- php: 5.6
env: SYMFONY_VERSION=3.0.*
- php: 7.0
env: SYMFONY_VERSION=3.0.*
allow_failures:
- php: hhvm
- php: nightly

notifications:
email: false
- ./vendor/bin/phpunit
87 changes: 76 additions & 11 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,98 @@

namespace SymfonyBundles\QueueBundle\DependencyInjection;

use SymfonyBundles\QueueBundle\Service;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class Configuration implements ConfigurationInterface
{

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();
$rootNode = $builder->root('sb_queue');

$this->addServiceSection($rootNode);
$this->addSettingsSection($rootNode);
$this->addStoragesSection($rootNode);

return $builder;
}

$builder->root('sb_queue')
->addDefaultsIfNotSet()
/**
* Adds the sb_queue.service configuration.
*
* @param ArrayNodeDefinition $node
*/
private function addServiceSection(ArrayNodeDefinition $node)
{
$node
->children()
->scalarNode('service_name')
->defaultValue('queue')
->cannotBeEmpty()
->end()
->scalarNode('default_name')
->defaultValue('queue:default')
->cannotBeEmpty()
->arrayNode('service')
->addDefaultsIfNotSet()
->children()
->scalarNode('class')
->defaultValue(Service\Queue::class)->cannotBeEmpty()
->end()
->scalarNode('alias')
->defaultValue('queue')->cannotBeEmpty()
->end()
->scalarNode('storage')
->defaultValue('redis')->cannotBeEmpty()
->end()
->end()
->end()
->end();
}

return $builder;
/**
* Adds the sb_queue.settings configuration.
*
* @param ArrayNodeDefinition $node
*/
private function addSettingsSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('settings')
->addDefaultsIfNotSet()
->children()
->scalarNode('queue_default_name')
->defaultValue('queue:default')->cannotBeEmpty()
->end()
->end()
->end()
->end();
}

/**
* Adds the sb_queue.storages configuration.
*
* @param ArrayNodeDefinition $node
*/
private function addStoragesSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('storages')
->addDefaultChildrenIfNoneSet('redis')
->requiresAtLeastOneElement()
->useAttributeAsKey('redis')
->prototype('array')
->children()
->scalarNode('class')
->defaultValue(Service\Storage\RedisStorage::class)->cannotBeEmpty()
->end()
->scalarNode('client')
->defaultValue('sb_redis.client.default')->cannotBeEmpty()
->end()
->end()
->end()
->end()
->end();
}
}
47 changes: 37 additions & 10 deletions DependencyInjection/QueueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,55 @@

namespace SymfonyBundles\QueueBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;

class QueueExtension extends ConfigurableExtension
{

/**
* {@inheritdoc}
*/
protected function loadInternal(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader(
$container, new FileLocator(__DIR__ . '/../Resources/config')
);
$this->addService($configs, $container);
$this->addStorage($configs, $container);
}

/**
* @param array $configs
* @param ContainerBuilder $container
*/
private function addService(array $configs, ContainerBuilder $container)
{
$alias = $this->getAlias();
$storageReference = new Reference('sb_queue.storage');

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

$container->setAlias($configs['service_name'], 'sb_queue');
$container->setParameter('sb_queue.default_name', $configs['default_name']);
$container->setDefinition($alias, $service);
$container->setAlias($configs['service']['alias'], $alias);
}

/**
* @param array $configs
* @param ContainerBuilder $container
*/
private function addStorage(array $configs, ContainerBuilder $container)
{
if (false === isset($configs['storages'][$configs['service']['storage']])) {
throw new \InvalidArgumentException(sprintf('Not available storage `%s`.', $configs['service']['storage']));
}

$parameters = $configs['storages'][$configs['service']['storage']];

$storage = new Definition($parameters['class']);
$storage->addArgument(new Reference($parameters['client']));

$container->setDefinition('sb_queue.storage', $storage);
}

/**
Expand All @@ -32,5 +60,4 @@ public function getAlias()
{
return 'sb_queue';
}

}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Symfony Bundles (Dmitry Khaperets)
Copyright (c) 2016-2017 Symfony Bundles (Dmitry Khaperets)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ public function registerBundles()
Defaults configuration:
``` yml
sb_queue:
service_name: "queue"
default_name: "queue:default"
service:
alias: 'queue' # alias for service `sb_queue` (e.g. $this->get('queue'))
class: 'SymfonyBundles\QueueBundle\Service\Queue'
storage: 'redis' # storage key from `queue.storages` section
settings:
queue_default_name: 'queue:default' # default name for queue
storages:
redis:
class: 'SymfonyBundles\QueueBundle\Service\Storage\RedisStorage'
client: 'sb_redis.client.default' # storage client service id
```
* Configure the redis client in your config.yml. Read more about [RedisBundle configuration][redis-bundle-link].
Expand All @@ -60,6 +68,21 @@ $queue->push(new \stdClass);
$queue->count(); // returns integer: 3
```
If you want to change the queue:
```
// adding data to queue `notifications`
$queue->setName('application:notifications');
$queue->push('You have a new message from Jessica');

// adding data to queue `settings`
$queue->setName('account:settings');
$queue->push('User with ID 123 changed password');

// adding data to default queue
$queue->setName('queue:default');
$queue->push('To be or not to be');
```

``` php
// now, we can get the data at any time in the queue order

Expand Down
18 changes: 0 additions & 18 deletions Resources/config/services.yml

This file was deleted.

2 changes: 0 additions & 2 deletions Service/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class Queue implements QueueInterface
{

/**
* @var string
*/
Expand Down Expand Up @@ -62,5 +61,4 @@ public function count()
{
return $this->storage->count($this->name);
}

}
7 changes: 0 additions & 7 deletions Service/QueueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@

interface QueueInterface
{

/**
* Sets the storage client.
*
* @param Storage\StorageInterface $storage
*
* @return void
*/
public function setStorage(Storage\StorageInterface $storage);

/**
* Sets the name of the queue list.
*
* @param string $name
*
* @return void
*/
public function setName($name);

Expand All @@ -41,8 +36,6 @@ public function pop();
* Append the value into the end of list.
*
* @param mixed $value Pushes value of the list.
*
* @return void
*/
public function push($value);

Expand Down
2 changes: 0 additions & 2 deletions Service/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class RedisStorage implements StorageInterface
{

/**
* @var ClientInterface
*/
Expand Down Expand Up @@ -59,5 +58,4 @@ public function count($key)
{
return $this->client->llen($key);
}

}
Loading

0 comments on commit da72ee8

Please sign in to comment.