Skip to content

Commit fef0685

Browse files
committed
Merge pull request #5 from Nyholm/patch-2
Added abstract adapter factory
2 parents 848b4d2 + a0c1fb8 commit fef0685

File tree

7 files changed

+169
-32
lines changed

7 files changed

+169
-32
lines changed

.travis.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@ php:
55
- "5.6"
66
- "5.5"
77

8+
sudo: required
9+
810
services:
911
- memcached
1012
- redis-server
1113

1214
env:
13-
- SYMFONY_VERSION=2.6.*
14-
- SYMFONY_VERSION=2.7.*
15-
- SYMFONY_VERSION=2.8.*
16-
- SYMFONY_VERSION=3.0.*
15+
global:
16+
- COMPOSER_COMMAND="composer install --prefer-dist"
17+
matrix:
18+
- SYMFONY_VERSION=2.6.*
19+
- SYMFONY_VERSION=2.7.*
20+
- SYMFONY_VERSION=2.8.*
21+
- SYMFONY_VERSION=3.0.*
1722

1823
matrix:
1924
fast_finish: true
20-
21-
sudo: false
25+
include:
26+
- php: 5.5
27+
env: COMPOSER_COMMAND="composer update --prefer-lowest --prefer-stable" && SYMFONY_VERSION=2.7.*
2228

2329
before_script:
2430
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d
2531
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
26-
- echo "extension=mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
2732
- echo "extension=memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
2833
- echo "extension=redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
2934
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && `php-config --vernum` -ge 70000 ]]; then pecl config-set preferred_state beta; printf "yes\n" | pecl install apcu; else echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;
3035
- composer self-update
3136
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
32-
- composer install --dev --prefer-dist --no-interaction
37+
- travis_retry ${COMPOSER_COMMAND} --no-interaction
3338
# must be set after composer to avoid issue with autoloading
3439
- echo "apc.enable_cli=On" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
3540

src/DependencyInjection/CacheAdapterExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function load(array $configs, ContainerBuilder $container)
4444
$first = $name;
4545
}
4646

47+
$factoryClass = $container->getDefinition($arguments['factory'])->getClass();
48+
$factoryClass::validate($arguments['options'], $name);
49+
4750
$def = $container->register('cache.provider.'.$name, DummyAdapter::class);
4851
$def->setFactory([new Reference($arguments['factory']), 'createAdapter'])
4952
->addArgument($arguments['options']);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\adapter-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\AdapterBundle\Exception;
13+
14+
/**
15+
* @author Tobias Nyholm <[email protected]>
16+
*/
17+
class ConfigurationException extends \Exception
18+
{
19+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\adapter-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\AdapterBundle\Factory;
13+
14+
use Cache\AdapterBundle\Exception\ConfigurationException;
15+
use Psr\Cache\CacheItemPoolInterface;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
/**
19+
* An abstract factory that makes it easier to implement new factories. A class that extend the AbstractAdapterFactory
20+
* should override AbstractAdapterFactory::$dependencies and AbstractAdapterFactory::configureOptionResolver().
21+
*
22+
* @author Tobias Nyholm <[email protected]>
23+
*/
24+
abstract class AbstractAdapterFactory implements AdapterFactoryInterface
25+
{
26+
protected static $dependencies = [];
27+
28+
/**
29+
* @param array $config
30+
*
31+
* @return CacheItemPoolInterface
32+
*/
33+
abstract protected function getAdapter(array $config);
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function createAdapter(array $options = [])
39+
{
40+
$this->verifyDependencies();
41+
42+
$resolver = new OptionsResolver();
43+
static::configureOptionResolver($resolver);
44+
$config = $resolver->resolve($options);
45+
46+
return $this->getAdapter($config);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public static function validate(array $options = [], $adapterName)
53+
{
54+
static::verifyDependencies();
55+
56+
$resolver = new OptionsResolver();
57+
static::configureOptionResolver($resolver);
58+
59+
try {
60+
$resolver->resolve($options);
61+
} catch (\Exception $e) {
62+
$message = sprintf(
63+
'Error while configure adapter %s. Verify your configuration at "cache_adapter.providers.%s.options". %s',
64+
$adapterName,
65+
$adapterName,
66+
$e->getMessage()
67+
);
68+
69+
throw new ConfigurationException($message, $e->getCode(), $e);
70+
}
71+
}
72+
73+
/**
74+
* Make sure that we have the required class and throw and exception if we don't.
75+
*
76+
* @throws \LogicException
77+
*/
78+
protected static function verifyDependencies()
79+
{
80+
foreach (static::$dependencies as $dependency) {
81+
if (!class_exists($dependency['requiredClass'])) {
82+
throw new \LogicException(
83+
sprintf(
84+
'You must install the "%s" package to use the "%s" factory.',
85+
$dependency['packageName'],
86+
static::class
87+
)
88+
);
89+
}
90+
}
91+
}
92+
93+
/**
94+
* By default we do not have any options to configure. A factory should override this function and confgure
95+
* the options resolver.
96+
*
97+
* @param OptionsResolver $resolver
98+
*/
99+
protected static function configureOptionResolver(OptionsResolver $resolver)
100+
{
101+
}
102+
}

src/Factory/AdapterFactoryInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,20 @@ interface AdapterFactoryInterface
2424
* @return CacheItemPoolInterface
2525
*/
2626
public function createAdapter(array $options = []);
27+
28+
/**
29+
* Make sure the options are valid and the dependencies are met.
30+
*
31+
* @param array $options the options the user has provided
32+
* @param string $adapterName the name the user has chosen for this adapter
33+
*
34+
* @throws \LogicException If the factory has missing dependencies
35+
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException If an option name is undefined
36+
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException If an option doesn't fulfill the specified validation rules
37+
* @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException If a required option is missing
38+
* @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException If there is a cyclic dependency between lazy options and/or normalizers
39+
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException If a lazy option reads an unavailable option
40+
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException If called from a lazy option or normalizer
41+
*/
42+
public static function validate(array $options = [], $adapterName);
2743
}

src/Factory/DoctrineRedisFactory.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
/**
1919
* @author Tobias Nyholm <[email protected]>
2020
*/
21-
class DoctrineRedisFactory implements AdapterFactoryInterface
21+
class DoctrineRedisFactory extends AbstractAdapterFactory
2222
{
23+
protected static $dependencies = [
24+
['requiredClass' => 'Cache\Adapter\Doctrine\DoctrineCachePool', 'packageName' => 'cache/doctrine-adapter'],
25+
];
26+
2327
/**
2428
* {@inheritdoc}
2529
*/
26-
public function createAdapter(array $options = [])
30+
public function getAdapter(array $config)
2731
{
2832
if (!class_exists('Cache\Adapter\Doctrine\DoctrineCachePool')) {
2933
throw new \LogicException('You must install the "cache/doctrine-adapter" package to use the "doctrine_redis" provider.');
3034
}
3135

32-
$config = $this->configureOptions($options);
3336
$redis = new \Redis();
3437
$redis->connect($config['host'], $config['port']);
3538

@@ -40,21 +43,16 @@ public function createAdapter(array $options = [])
4043
}
4144

4245
/**
43-
* @param array $options
44-
*
45-
* @return array
46+
* {@inheritdoc}
4647
*/
47-
private function configureOptions(array $options)
48+
protected static function configureOptionResolver(OptionsResolver $resolver)
4849
{
49-
$resolver = new OptionsResolver();
5050
$resolver->setDefaults([
5151
'host' => '127.0.0.1',
5252
'port' => '6379',
5353
]);
5454

5555
$resolver->setAllowedTypes('host', ['string']);
5656
$resolver->setAllowedTypes('port', ['string', 'int']);
57-
58-
return $resolver->resolve($options);
5957
}
6058
}

src/Factory/RedisFactory.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,27 @@
1818
/**
1919
* @author Tobias Nyholm <[email protected]>
2020
*/
21-
class RedisFactory implements AdapterFactoryInterface
21+
class RedisFactory extends AbstractAdapterFactory
2222
{
23+
protected static $dependencies = [
24+
['requiredClass' => 'Cache\Adapter\Redis\RedisCachePool', 'packageName' => 'cache/redis-adapter'],
25+
];
26+
2327
/**
2428
* {@inheritdoc}
2529
*/
26-
public function createAdapter(array $options = [])
30+
public function getAdapter(array $config)
2731
{
28-
if (!class_exists('Cache\Adapter\Redis\RedisCachePool')) {
29-
throw new \LogicException('You must install the "cache/redis-adapter" package to use the "redis" provider.');
30-
}
31-
32-
$config = $this->configureOptions($options);
3332
$client = new Client(sprintf('%s://%s:%s', $config['protocol'], $config['host'], $config['port']));
3433

3534
return new RedisCachePool($client);
3635
}
3736

3837
/**
39-
* @param array $options
40-
*
41-
* @return array
38+
* {@inheritdoc}
4239
*/
43-
private function configureOptions(array $options)
40+
protected static function configureOptionResolver(OptionsResolver $resolver)
4441
{
45-
$resolver = new OptionsResolver();
4642
$resolver->setDefaults([
4743
'host' => '127.0.0.1',
4844
'port' => '6379',
@@ -52,7 +48,5 @@ private function configureOptions(array $options)
5248
$resolver->setAllowedTypes('host', ['string']);
5349
$resolver->setAllowedTypes('port', ['string', 'int']);
5450
$resolver->setAllowedTypes('protocol', ['string']);
55-
56-
return $resolver->resolve($options);
5751
}
5852
}

0 commit comments

Comments
 (0)