Skip to content

Commit 848b4d2

Browse files
committed
Merge pull request #4 from Nyholm/patch-1
Added options resolver
2 parents ee76b41 + c4307d6 commit 848b4d2

File tree

6 files changed

+77
-23
lines changed

6 files changed

+77
-23
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"require": {
2121
"php": "^5.5|^7",
2222
"symfony/framework-bundle": "^2.6|^3.0",
23-
"cache/doctrine-adapter": "^0.3"
23+
"symfony/options-resolver": "^2.6|^3.0",
24+
"psr/cache": "1.0.0"
2425
},
2526
"require-dev": {
2627
"phpunit/phpunit": "5.0.*|^4.0",
@@ -29,6 +30,11 @@
2930
"cache/redis-adapter": "@stable",
3031
"cache/memcached-adapter": "@stable"
3132
},
33+
"suggest": {
34+
"cache/doctrine-adapter": "To integrate with doctrine/cache",
35+
"cache/redis-adapter": "Redis adapter",
36+
"cache/memcached-adapter": "Memcached adapter"
37+
},
3238
"autoload": {
3339
"psr-4": {
3440
"Cache\\AdapterBundle\\": "src/"

src/DependencyInjection/CacheAdapterExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function load(array $configs, ContainerBuilder $container)
3838
$loader->load('services.yml');
3939

4040
// Configure client services
41-
$first = isset($providers['default']) ? 'default' : null;
41+
$first = isset($config['providers']['default']) ? 'default' : null;
4242
foreach ($config['providers'] as $name => $arguments) {
4343
if ($first === null) {
4444
$first = $name;

src/Factory/AdapterFactoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111

1212
namespace Cache\AdapterBundle\Factory;
1313

14+
use Psr\Cache\CacheItemPoolInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
1419
interface AdapterFactoryInterface
1520
{
21+
/**
22+
* @param array $options
23+
*
24+
* @return CacheItemPoolInterface
25+
*/
1626
public function createAdapter(array $options = []);
1727
}

src/Factory/DoctrineRedisFactory.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,48 @@
1313

1414
use Cache\Adapter\Doctrine\DoctrineCachePool;
1515
use Doctrine\Common\Cache\RedisCache;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
1617

18+
/**
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
1721
class DoctrineRedisFactory implements AdapterFactoryInterface
1822
{
23+
/**
24+
* {@inheritdoc}
25+
*/
1926
public function createAdapter(array $options = [])
2027
{
2128
if (!class_exists('Cache\Adapter\Doctrine\DoctrineCachePool')) {
2229
throw new \LogicException('You must install the "cache/doctrine-adapter" package to use the "doctrine_redis" provider.');
2330
}
2431

25-
// TODO validate the options with symfony options resolver
26-
// TODO get ip, port and protocol from options.
27-
28-
$redis = new \Redis();
29-
$redis->connect('127.0.0.1', '6379');
32+
$config = $this->configureOptions($options);
33+
$redis = new \Redis();
34+
$redis->connect($config['host'], $config['port']);
3035

3136
$client = new RedisCache();
3237
$client->setRedis($redis);
3338

3439
return new DoctrineCachePool($client);
3540
}
41+
42+
/**
43+
* @param array $options
44+
*
45+
* @return array
46+
*/
47+
private function configureOptions(array $options)
48+
{
49+
$resolver = new OptionsResolver();
50+
$resolver->setDefaults([
51+
'host' => '127.0.0.1',
52+
'port' => '6379',
53+
]);
54+
55+
$resolver->setAllowedTypes('host', ['string']);
56+
$resolver->setAllowedTypes('port', ['string', 'int']);
57+
58+
return $resolver->resolve($options);
59+
}
3660
}

src/Factory/RedisFactory.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,46 @@
1313

1414
use Cache\Adapter\Redis\RedisCachePool;
1515
use Predis\Client;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
1617

18+
/**
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
1721
class RedisFactory implements AdapterFactoryInterface
1822
{
23+
/**
24+
* {@inheritdoc}
25+
*/
1926
public function createAdapter(array $options = [])
2027
{
2128
if (!class_exists('Cache\Adapter\Redis\RedisCachePool')) {
2229
throw new \LogicException('You must install the "cache/redis-adapter" package to use the "redis" provider.');
2330
}
2431

25-
// TOOD validate the options with symfony options resolver
26-
// TODO get ip, port and protocol from options.
27-
$client = new Client('tcp:/127.0.0.1:6379');
32+
$config = $this->configureOptions($options);
33+
$client = new Client(sprintf('%s://%s:%s', $config['protocol'], $config['host'], $config['port']));
2834

2935
return new RedisCachePool($client);
3036
}
37+
38+
/**
39+
* @param array $options
40+
*
41+
* @return array
42+
*/
43+
private function configureOptions(array $options)
44+
{
45+
$resolver = new OptionsResolver();
46+
$resolver->setDefaults([
47+
'host' => '127.0.0.1',
48+
'port' => '6379',
49+
'protocol' => 'tcp',
50+
]);
51+
52+
$resolver->setAllowedTypes('host', ['string']);
53+
$resolver->setAllowedTypes('port', ['string', 'int']);
54+
$resolver->setAllowedTypes('protocol', ['string']);
55+
56+
return $resolver->resolve($options);
57+
}
3158
}

src/Resources/config/services.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
2-
parameters:
3-
cache.doctrine_adapter.apc.class: Doctrine\Common\Cache\ApcCache
4-
cache.doctrine_adapter.array.class: Doctrine\Common\Cache\ArrayCache
5-
cache.doctrine_adapter.file_system.class: Doctrine\Common\Cache\FilesystemCache
6-
cache.doctrine_adapter.memcache.class: Doctrine\Common\Cache\MemcacheCache
7-
cache.doctrine_adapter.memcached.class: Doctrine\Common\Cache\MemcachedCache
8-
cache.doctrine_adapter.redis.class: Doctrine\Common\Cache\RedisCache
9-
cache.doctrine_adapter.php_file.class: Doctrine\Common\Cache\PhpFileCache
10-
cache.doctrine_adapter.win_cache.class: Doctrine\Common\Cache\WinCacheCache
11-
cache.doctrine_adapter.xcache.class: Doctrine\Common\Cache\XcacheCache
12-
cache.doctrine_adapter.zend_data.class: Doctrine\Common\Cache\ZendDataCache
13-
141
services:
152
cache.factory.redis:
163
class: Cache\AdapterBundle\Factory\RedisFactory

0 commit comments

Comments
 (0)