|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jenschude <[email protected]> |
|
4
|
|
|
* @created 19.01.15, 17:17 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Cache; |
|
8
|
|
|
|
|
9
|
|
|
use Cache\Adapter\Apcu\ApcuCachePool; |
|
10
|
|
|
use Cache\Adapter\Doctrine\DoctrineCachePool; |
|
11
|
|
|
use Cache\Adapter\Filesystem\FilesystemCachePool; |
|
12
|
|
|
use Cache\Adapter\Redis\RedisCachePool; |
|
13
|
|
|
use Doctrine\Common\Cache\Cache; |
|
14
|
|
|
use Commercetools\Core\Error\Message; |
|
15
|
|
|
use Commercetools\Core\Error\InvalidArgumentException; |
|
16
|
|
|
use League\Flysystem\Adapter\Local; |
|
17
|
|
|
use League\Flysystem\Filesystem; |
|
18
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
19
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @package Commercetools\Core\Cache |
|
23
|
|
|
*/ |
|
24
|
|
|
class CacheAdapterFactory |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $cacheDir; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $callbacks = []; |
|
35
|
|
|
|
|
36
|
109 |
|
public function __construct($cacheDir = null) |
|
37
|
|
|
{ |
|
38
|
109 |
|
$this->cacheDir = !is_null($cacheDir) ? $cacheDir : realpath(__DIR__ . '/../..'); |
|
39
|
109 |
|
$this->registerCallback( |
|
40
|
109 |
|
function ($cache) { |
|
41
|
5 |
|
if ($cache instanceof Cache) { |
|
42
|
1 |
|
return new DoctrineCachePool($cache); |
|
43
|
|
|
} |
|
44
|
4 |
|
return null; |
|
45
|
109 |
|
} |
|
46
|
|
|
) |
|
47
|
109 |
|
->registerCallback( |
|
48
|
109 |
|
function ($cache) { |
|
49
|
4 |
|
if ($cache instanceof \Redis) { |
|
50
|
1 |
|
return new RedisCachePool($cache); |
|
51
|
|
|
} |
|
52
|
3 |
|
return null; |
|
53
|
109 |
|
} |
|
54
|
|
|
); |
|
55
|
109 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* registers a callback to resolve a cache adapter interface |
|
59
|
|
|
* |
|
60
|
|
|
* @param callable $callback |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
109 |
|
public function registerCallback(callable $callback) |
|
64
|
|
|
{ |
|
65
|
109 |
|
$this->callbacks[] = $callback; |
|
66
|
|
|
|
|
67
|
109 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* returns the cache adapter interface for the application cache |
|
72
|
|
|
* |
|
73
|
|
|
* @param $cache |
|
74
|
|
|
* @return CacheItemPoolInterface|CacheInterface |
|
75
|
|
|
* @throws \InvalidArgumentException |
|
76
|
|
|
*/ |
|
77
|
109 |
|
public function get($cache = null) |
|
78
|
|
|
{ |
|
79
|
109 |
|
if (is_null($cache)) { |
|
80
|
52 |
|
$cache = $this->getDefaultCache(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
109 |
|
if ($cache instanceof CacheItemPoolInterface) { |
|
84
|
101 |
|
return $cache; |
|
85
|
|
|
} |
|
86
|
9 |
|
if ($cache instanceof CacheInterface) { |
|
87
|
4 |
|
return $cache; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
foreach ($this->callbacks as $callBack) { |
|
91
|
5 |
|
$result = call_user_func($callBack, $cache); |
|
92
|
5 |
|
if ($result instanceof CacheItemPoolInterface) { |
|
93
|
3 |
|
return $result; |
|
94
|
|
|
} |
|
95
|
4 |
|
if ($result instanceof CacheInterface) { |
|
96
|
|
|
return $result; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
throw new InvalidArgumentException(Message::INVALID_CACHE_ADAPTER); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* creates a default cache adapter if no cache has been provided |
|
105
|
|
|
* |
|
106
|
|
|
* @return CacheItemPoolInterface|null |
|
107
|
|
|
*/ |
|
108
|
52 |
|
protected function getDefaultCache() |
|
109
|
|
|
{ |
|
110
|
52 |
|
if (extension_loaded('apcu')) { |
|
111
|
52 |
|
return new ApcuCachePool(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (class_exists('\Cache\Adapter\Filesystem\FilesystemCachePool')) { |
|
115
|
|
|
$filesystemAdapter = new Local($this->cacheDir); |
|
116
|
|
|
$filesystem = new Filesystem($filesystemAdapter); |
|
117
|
|
|
return new FilesystemCachePool($filesystem); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|