forked from winzou/CacheBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheFactory.php
126 lines (106 loc) · 4.15 KB
/
CacheFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
* This file is part of winzouCacheBundle.
*
* winzouCacheBundle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* winzouCacheBundle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace winzou\CacheBundle;
use winzou\CacheBundle\Cache\AbstractCache;
use winzou\CacheBundle\Cache\CacheInterface;
class CacheFactory
{
/** @var array $drivers */
private $drivers;
/** @var array $options */
private $options;
/** @var int $exceptionCode */
const EXCEPTION_CODE = 999;
/**
* Constructor.
*
* @param array $drivers The list of available drivers, key=driver name, value=driver class
* @param array $options Options to pass to the driver
*/
public function __construct(array $drivers, array $options = array())
{
$this->drivers = $drivers;
$this->options = $options;
}
/**
* Builder.
*
* @param string $driver The cache driver to use
* @param array $options Options to pass to the driver
* @param bool $byPassCheck If you want to perform a configuration check, set false (but you should know in advance which driver is supporteed by your configuration)
* @return AbstractCache
*/
public function getCache($driver, array $options = array(), $byPassCheck = true)
{
if (!$this->driverExists($driver)) {
throw new \Exception('The cache driver "'.$driver.'" is not supported by the bundle.', self::EXCEPTION_CODE);
}
$class = $this->drivers[$driver];
if (!$byPassCheck && !$class::isSupported()) {
throw new \Exception('The cache driver "'.$driver.'" is not supported by your running configuration.', self::EXCEPTION_CODE);
}
$options = array_merge($this->options, $options);
$cache = new $class($options);
if (!$cache instanceof CacheInterface) {
throw new \Exception('The cache driver "'.$driver.'" must implement CacheInterface.');
}
return $cache;
}
/**
* Try to initialise any of the requested driver, check if it exists and it's supported. Fallback to File
*
* @param array|string $drivers The ordered list of drivers to try, can be a string if only one
* @param array $options Options to pass to the driver
* @return Cache\AbstractCache
*/
public function getCacheFallback($drivers, array $options = array())
{
// allow single driver
if (!is_array($drivers)) {
$drivers = array($drivers);
}
// fallback to file, this one should work
// we are sure that array will work, but as it's not a persistent driver, we prefer throw an Exception
$drivers[] = 'file';
foreach ($drivers as $driver) {
try {
$cache = $this->getCache($driver, $options, false);
} catch (\Exception $e) {
// if it's not an exception thrown by the getCache method, we rethrow it
if ($e->getCode() != self::EXCEPTION_CODE) {
throw $e;
}
// otherwise do nothing, try next driver
}
}
if (!isset($cache) || !$cache instanceof AbstractCache) {
throw new \Exception('Unable to initialise any of the required drivers ("'.implode('", "', $drivers).'").');
}
return $cache;
}
/**
* Check if the given driver is supported by the bundle
*
* @param string $driver
* @return bool
*/
public function driverExists($driver)
{
return isset($this->drivers[$driver]);
}
}