|
| 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\Doctrine\DoctrineCachePool; |
| 15 | +use Doctrine\Common\Cache\PredisCache; |
| 16 | +use Predis\Client; |
| 17 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Tobias Nyholm <[email protected]> |
| 21 | + */ |
| 22 | +class DoctrinePredisFactory extends AbstractDoctrineAdapterFactory |
| 23 | +{ |
| 24 | + protected static $dependencies = [ |
| 25 | + ['requiredClass' => 'Cache\Adapter\Doctrine\DoctrineCachePool', 'packageName' => 'cache/doctrine-adapter'], |
| 26 | + ['requiredClass' => 'Predis\Client', 'packageName' => 'predis/predis'], |
| 27 | + ]; |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritdoc} |
| 31 | + */ |
| 32 | + public function getAdapter(array $config) |
| 33 | + { |
| 34 | + $client = new Client([ |
| 35 | + 'scheme' => $config['scheme'], |
| 36 | + 'host' => $config['host'], |
| 37 | + 'port' => $config['port'], |
| 38 | + ]); |
| 39 | + |
| 40 | + return new DoctrineCachePool(new PredisCache($client)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + protected static function configureOptionResolver(OptionsResolver $resolver) |
| 47 | + { |
| 48 | + $resolver->setDefaults([ |
| 49 | + 'host' => '127.0.0.1', |
| 50 | + 'port' => '6379', |
| 51 | + 'scheme' => 'tcp', |
| 52 | + ]); |
| 53 | + |
| 54 | + $resolver->setAllowedTypes('host', ['string']); |
| 55 | + $resolver->setAllowedTypes('port', ['string', 'int']); |
| 56 | + $resolver->setAllowedTypes('scheme', ['string']); |
| 57 | + } |
| 58 | +} |
0 commit comments