Skip to content

Commit e280db8

Browse files
committed
Merge pull request #7 from php-cache/adapters
Added factories for the new adapters
2 parents fef0685 + 779f376 commit e280db8

File tree

12 files changed

+299
-8
lines changed

12 files changed

+299
-8
lines changed

src/DependencyInjection/CacheAdapterExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public function load(array $configs, ContainerBuilder $container)
4747
$factoryClass = $container->getDefinition($arguments['factory'])->getClass();
4848
$factoryClass::validate($arguments['options'], $name);
4949

50+
// See if any option has a service reference
51+
foreach ($arguments['options'] as $key => $value) {
52+
if (substr($key, -8) === '_service') {
53+
$arguments['options'][$key] = new Reference($value);
54+
}
55+
}
56+
5057
$def = $container->register('cache.provider.'.$name, DummyAdapter::class);
5158
$def->setFactory([new Reference($arguments['factory']), 'createAdapter'])
5259
->addArgument($arguments['options']);

src/Factory/AbstractAdapterFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function createAdapter(array $options = [])
4949
/**
5050
* {@inheritdoc}
5151
*/
52-
public static function validate(array $options = [], $adapterName)
52+
public static function validate(array $options, $adapterName)
5353
{
5454
static::verifyDependencies();
5555

src/Factory/AdapterFactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ public function createAdapter(array $options = []);
3939
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException If a lazy option reads an unavailable option
4040
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException If called from a lazy option or normalizer
4141
*/
42-
public static function validate(array $options = [], $adapterName);
42+
public static function validate(array $options, $adapterName);
4343
}

src/Factory/ApcFactory.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Adapter\Apc\ApcCachePool;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ApcFactory extends AbstractAdapterFactory
20+
{
21+
protected static $dependencies = [
22+
['requiredClass' => 'Cache\Adapter\Apc\ApcCachePool', 'packageName' => 'cache/apc-adapter'],
23+
];
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function getAdapter(array $config)
29+
{
30+
return new ApcCachePool();
31+
}
32+
}

src/Factory/ApcuFactory.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Adapter\Apcu\ApcuCachePool;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ApcuFactory extends AbstractAdapterFactory
20+
{
21+
protected static $dependencies = [
22+
['requiredClass' => 'Cache\Adapter\Apcu\ApcuCachePool', 'packageName' => 'cache/apcu-adapter'],
23+
];
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function getAdapter(array $config)
29+
{
30+
return new ApcuCachePool();
31+
}
32+
}

src/Factory/ArrayFactory.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Adapter\PHPArray\ArrayCachePool;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ArrayFactory extends AbstractAdapterFactory
20+
{
21+
protected static $dependencies = [
22+
['requiredClass' => 'Cache\Adapter\PHPArray\ArrayCachePool', 'packageName' => 'cache/array-adapter'],
23+
];
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function getAdapter(array $config)
29+
{
30+
return new ArrayCachePool();
31+
}
32+
}

src/Factory/DoctrineRedisFactory.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ class DoctrineRedisFactory extends AbstractAdapterFactory
2929
*/
3030
public function getAdapter(array $config)
3131
{
32-
if (!class_exists('Cache\Adapter\Doctrine\DoctrineCachePool')) {
33-
throw new \LogicException('You must install the "cache/doctrine-adapter" package to use the "doctrine_redis" provider.');
34-
}
35-
3632
$redis = new \Redis();
3733
$redis->connect($config['host'], $config['port']);
3834

src/Factory/FilesystemFactory.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Adapter\Filesystem\FilesystemCachePool;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
class FilesystemFactory extends AbstractAdapterFactory
21+
{
22+
protected static $dependencies = [
23+
['requiredClass' => 'Cache\Adapter\Filesystem\FilesystemCachePool', 'packageName' => 'cache/filesystem-adapter'],
24+
];
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getAdapter(array $config)
30+
{
31+
return new FilesystemCachePool($config['flysystem']);
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
protected static function configureOptionResolver(OptionsResolver $resolver)
38+
{
39+
$resolver->setDefaults([
40+
'flysystem_service' => null,
41+
]);
42+
43+
$resolver->setAllowedTypes('host', ['string', 'object']);
44+
}
45+
}

src/Factory/MemcachedFactory.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Adapter\Memcached\MemcachedCachePool;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
class MemcachedFactory extends AbstractAdapterFactory
21+
{
22+
protected static $dependencies = [
23+
['requiredClass' => 'Cache\Adapter\Memcached\MemcachedCachePool', 'packageName' => 'cache/memcached-adapter'],
24+
];
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getAdapter(array $config)
30+
{
31+
$client = new \Memcached();
32+
$client->addServer($config['host'], $config['port']);
33+
34+
return new MemcachedCachePool($client);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected static function configureOptionResolver(OptionsResolver $resolver)
41+
{
42+
$resolver->setDefaults([
43+
'host' => '127.0.0.1',
44+
'port' => '6379',
45+
]);
46+
47+
$resolver->setAllowedTypes('host', ['string']);
48+
$resolver->setAllowedTypes('port', ['string', 'int']);
49+
}
50+
}

src/Factory/PhpRedisFactory.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Adapter\PhpRedis\PhpRedisCachePool;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
class PhpRedisFactory extends AbstractAdapterFactory
21+
{
22+
protected static $dependencies = [
23+
['requiredClass' => 'Cache\Adapter\PhpRedis\PhpRedisCachePool', 'packageName' => 'cache/phpredis-adapter'],
24+
];
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getAdapter(array $config)
30+
{
31+
$client = new \Redis();
32+
$client->client($config['host'], $config['port']);
33+
34+
return new PhpRedisCachePool($client);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected static function configureOptionResolver(OptionsResolver $resolver)
41+
{
42+
$resolver->setDefaults([
43+
'host' => '127.0.0.1',
44+
'port' => '6379',
45+
]);
46+
47+
$resolver->setAllowedTypes('host', ['string']);
48+
$resolver->setAllowedTypes('port', ['string', 'int']);
49+
}
50+
}

0 commit comments

Comments
 (0)