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 Doctrine\Common\Cache\Cache; |
10
|
|
|
use Commercetools\Core\Error\Message; |
11
|
|
|
use Commercetools\Core\Error\InvalidArgumentException; |
12
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @package Commercetools\Core\Cache |
16
|
|
|
*/ |
17
|
|
|
class CacheAdapterFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $callbacks = []; |
23
|
|
|
|
24
|
47 |
|
public function __construct() |
25
|
|
|
{ |
26
|
47 |
|
$this->registerCallback( |
27
|
|
|
function ($cache) { |
28
|
6 |
|
if ($cache instanceof CacheItemPoolInterface) { |
29
|
1 |
|
return new PsrCacheAdapter($cache); |
30
|
|
|
} |
31
|
5 |
|
return null; |
32
|
47 |
|
} |
33
|
|
|
) |
34
|
47 |
|
->registerCallback( |
35
|
|
|
function ($cache) { |
36
|
5 |
|
if ($cache instanceof Cache) { |
37
|
1 |
|
return new DoctrineCacheAdapter($cache); |
38
|
|
|
} |
39
|
4 |
|
return null; |
40
|
47 |
|
} |
41
|
|
|
) |
42
|
47 |
|
->registerCallback( |
43
|
47 |
|
function ($cache) { |
44
|
4 |
|
if ($cache instanceof \Redis) { |
|
|
|
|
45
|
1 |
|
return new PhpRedisCacheAdapter($cache); |
46
|
|
|
} |
47
|
3 |
|
return null; |
48
|
47 |
|
} |
49
|
|
|
); |
50
|
47 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* registers a callback to resolve a cache adapter interface |
54
|
|
|
* |
55
|
|
|
* @param callable $callback |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
47 |
|
public function registerCallback(callable $callback) |
59
|
|
|
{ |
60
|
47 |
|
$this->callbacks[] = $callback; |
61
|
|
|
|
62
|
47 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* returns the cache adapter interface for the application cache |
67
|
|
|
* |
68
|
|
|
* @param $cache |
69
|
|
|
* @return CacheAdapterInterface |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
*/ |
72
|
47 |
|
public function get($cache = null) |
73
|
|
|
{ |
74
|
47 |
|
if (is_null($cache)) { |
75
|
36 |
|
$cache = $this->getDefaultCache(); |
76
|
|
|
} |
77
|
|
|
|
78
|
47 |
|
if ($cache instanceof CacheAdapterInterface) { |
79
|
41 |
|
return $cache; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
foreach ($this->callbacks as $callBack) { |
83
|
6 |
|
$result = call_user_func($callBack, $cache); |
84
|
6 |
|
if ($result instanceof CacheAdapterInterface) { |
85
|
6 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
throw new InvalidArgumentException(Message::INVALID_CACHE_ADAPTER); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* creates a default cache adapter if no cache has been provided |
94
|
|
|
* |
95
|
|
|
* @return CacheAdapterInterface|null |
96
|
|
|
*/ |
97
|
36 |
|
protected function getDefaultCache() |
98
|
|
|
{ |
99
|
36 |
|
if (extension_loaded('apcu')) { |
100
|
36 |
|
return new ApcuCacheAdapter(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (extension_loaded('apc')) { |
104
|
|
|
return new ApcCacheAdapter(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.