|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-cache organization. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 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
|
3 |
|
public function createAdapter(array $options = []) |
|
39
|
|
|
{ |
|
40
|
3 |
|
$this->verifyDependencies(); |
|
41
|
|
|
|
|
42
|
3 |
|
$resolver = new OptionsResolver(); |
|
43
|
3 |
|
static::configureOptionResolver($resolver); |
|
44
|
3 |
|
$config = $resolver->resolve($options); |
|
45
|
|
|
|
|
46
|
3 |
|
return $this->getAdapter($config); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
6 |
|
public static function validate(array $options, $adapterName) |
|
53
|
|
|
{ |
|
54
|
6 |
|
static::verifyDependencies(); |
|
55
|
|
|
|
|
56
|
6 |
|
$resolver = new OptionsResolver(); |
|
57
|
6 |
|
static::configureOptionResolver($resolver); |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
6 |
|
$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
|
6 |
|
} |
|
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
|
6 |
|
protected static function verifyDependencies() |
|
79
|
|
|
{ |
|
80
|
6 |
|
foreach (static::$dependencies as $dependency) { |
|
81
|
6 |
|
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
|
6 |
|
} |
|
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
|
6 |
|
protected static function configureOptionResolver(OptionsResolver $resolver) |
|
100
|
|
|
{ |
|
101
|
6 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|