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