|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of php-cache\adapter-bundle package. |
| 5 | + * |
| 6 | + * (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com> |
| 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 Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 19 | + */ |
| 20 | +abstract class AbstractAdapterFactory implements AdapterFactoryInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param OptionsResolver $resolver |
| 24 | + */ |
| 25 | + abstract protected function configureOptionResolver(OptionsResolver $resolver); |
| 26 | + |
| 27 | + /** |
| 28 | + * @param array $config |
| 29 | + * |
| 30 | + * @return CacheItemPoolInterface |
| 31 | + */ |
| 32 | + abstract protected function getAdapter(array $config); |
| 33 | + |
| 34 | + /** |
| 35 | + * Get the class name that is required for this adapter. This should more often than not be the cache pool. |
| 36 | + * |
| 37 | + * @return string |
| 38 | + */ |
| 39 | + abstract protected function getRequiredClass(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the name of the package where require class lives. |
| 43 | + * |
| 44 | + * @return string |
| 45 | + */ |
| 46 | + abstract protected function getPackageName(); |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the name of the adapter. |
| 50 | + * |
| 51 | + * @return string |
| 52 | + */ |
| 53 | + abstract protected function getName(); |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function createAdapter(array $options = []) |
| 59 | + { |
| 60 | + $this->verifyDependencies(); |
| 61 | + |
| 62 | + $resolver = new OptionsResolver(); |
| 63 | + $this->configureOptionResolver($resolver); |
| 64 | + $config = $resolver->resolve($options); |
| 65 | + |
| 66 | + return $this->getAdapter($config); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Make sure that we have the required class and throws and exception if we dont. |
| 71 | + * |
| 72 | + * @throws \LogicException |
| 73 | + */ |
| 74 | + protected function verifyDependencies() |
| 75 | + { |
| 76 | + if (!class_exists($this->getRequiredClass())) { |
| 77 | + throw new \LogicException( |
| 78 | + sprintf( |
| 79 | + 'You must install the "%s" package to use the "%s" provider.', |
| 80 | + $this->getPackageName(), |
| 81 | + $this->getName() |
| 82 | + ) |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments