|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/craft-psr6/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/craft-psr6 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\psr6\events; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\ArrayHelper; |
|
13
|
|
|
use craft\helpers\Json; |
|
14
|
|
|
use Stash\Interfaces\PoolInterface; |
|
15
|
|
|
use yii\base\Event; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Flipbox Factory <[email protected]> |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class RegisterCachePools extends Event |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PoolInterface[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $pools = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $pools |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setPools(array $pools) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($pools as $handle => $pool) { |
|
35
|
|
|
$this->addPool($handle, $pool); |
|
36
|
|
|
} |
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $handle |
|
42
|
|
|
* @param $pool |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function addPool(string $handle, $pool) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->pools[$handle] = $pool; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return PoolInterface[] |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getPools() |
|
55
|
|
|
{ |
|
56
|
|
|
$pools = []; |
|
57
|
|
|
foreach ($this->pools as $handle => $pool) { |
|
58
|
|
|
if (!$pool = $this->resolvePool($pool)) { |
|
|
|
|
|
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
$pools[$handle] = $pool; |
|
62
|
|
|
} |
|
63
|
|
|
return $pools; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $pool |
|
68
|
|
|
* @return null |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function resolvePool($pool) |
|
71
|
|
|
{ |
|
72
|
|
|
if (is_callable($pool)) { |
|
73
|
|
|
$pool = $pool(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($pool instanceof PoolInterface) { |
|
77
|
|
|
return $pool; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!$class = $this->findClassFromConfig($pool)) { |
|
81
|
|
|
Craft::error( |
|
82
|
|
|
sprintf( |
|
83
|
|
|
"Could find cache pool class from config: %s", |
|
84
|
|
|
Json::encode($pool) |
|
85
|
|
|
), |
|
86
|
|
|
'PSR-6' |
|
87
|
|
|
); |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Make sure we have a valid class |
|
92
|
|
|
if (!is_subclass_of($class, PoolInterface::class)) { |
|
|
|
|
|
|
93
|
|
|
Craft::error( |
|
94
|
|
|
sprintf( |
|
95
|
|
|
"Cache pool class '%s' is not an instances of %s", |
|
96
|
|
|
(string)$class, |
|
97
|
|
|
PoolInterface::class |
|
98
|
|
|
), |
|
99
|
|
|
'PSR-6' |
|
100
|
|
|
); |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $class($pool); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Find a class from a config |
|
109
|
|
|
* |
|
110
|
|
|
* @param $config |
|
111
|
|
|
* @param bool $removeClass |
|
112
|
|
|
* @return null|string |
|
113
|
|
|
*/ |
|
114
|
|
|
private function findClassFromConfig(&$config, bool $removeClass = false) |
|
115
|
|
|
{ |
|
116
|
|
|
if (is_string($config)) { |
|
117
|
|
|
// Set as class |
|
118
|
|
|
$class = $config; |
|
119
|
|
|
|
|
120
|
|
|
// Clear class from config |
|
121
|
|
|
$config = ''; |
|
122
|
|
|
} elseif (is_object($config)) { |
|
123
|
|
|
return get_class($config); |
|
124
|
|
|
} else { |
|
125
|
|
|
// Force Array |
|
126
|
|
|
if (!is_array($config)) { |
|
127
|
|
|
$config = ArrayHelper::toArray($config, [], false); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($removeClass) { |
|
131
|
|
|
if (!$class = ArrayHelper::remove($config, 'class')) { |
|
132
|
|
|
$class = ArrayHelper::remove($config, 'type'); |
|
133
|
|
|
} |
|
134
|
|
|
} else { |
|
135
|
|
|
$class = ArrayHelper::getValue( |
|
136
|
|
|
$config, |
|
137
|
|
|
'class', |
|
138
|
|
|
ArrayHelper::getValue($config, 'type') |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $class; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.