1
|
|
|
<?php namespace Comodojo\Dispatcher\Cache;
|
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
|
4
|
|
|
use \comodojo\Dispatcher\Components\Configuration;
|
5
|
|
|
use \Comodojo\Cache\CacheInterface\CacheInterface;
|
6
|
|
|
use \Comodojo\Cache\CacheManager;
|
7
|
|
|
use \Comodojo\Cache\FileCache;
|
8
|
|
|
use \Psr\Log\LoggerInterface;
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* @package Comodojo Dispatcher
|
12
|
|
|
* @author Marco Giovinazzi <[email protected]>
|
13
|
|
|
* @author Marco Castiello <[email protected]>
|
14
|
|
|
* @license GPL-3.0+
|
15
|
|
|
*
|
16
|
|
|
* LICENSE:
|
17
|
|
|
*
|
18
|
|
|
* This program is free software: you can redistribute it and/or modify
|
19
|
|
|
* it under the terms of the GNU Affero General Public License as
|
20
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
21
|
|
|
* License, or (at your option) any later version.
|
22
|
|
|
*
|
23
|
|
|
* This program is distributed in the hope that it will be useful,
|
24
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
25
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
26
|
|
|
* GNU Affero General Public License for more details.
|
27
|
|
|
*
|
28
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
29
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
30
|
|
|
*/
|
31
|
|
|
|
32
|
|
|
class DispatcherCache extends DispatcherClassModel{
|
33
|
|
|
|
34
|
|
|
public function init() {
|
35
|
|
|
|
36
|
|
|
$cache = $this->configuration()->get('cache');
|
37
|
|
|
|
38
|
|
|
if ( empty($cache) ) {
|
39
|
|
|
|
40
|
|
|
$manager = new CacheManager(self::getAlgorithm(), $this->logger);
|
41
|
|
|
|
42
|
|
|
} else {
|
43
|
|
|
|
44
|
|
|
$enabled = ( empty($cache['enabled']) || $cache['enabled'] === true ) ? true : false;
|
45
|
|
|
|
46
|
|
|
$algorithm = self::getAlgorithm( empty($cache['algorithm']) ? null : $cache['algorithm']);
|
47
|
|
|
|
48
|
|
|
$manager = new CacheManager($algorithm, $this->logger);
|
49
|
|
|
|
50
|
|
|
if ( $enabled && !empty($cache['providers']) ) {
|
51
|
|
|
|
52
|
|
|
foreach ($cache['providers'] as $provider => $parameters) {
|
53
|
|
|
|
54
|
|
|
$handler = $this->getHandler($provider, $parameters);
|
55
|
|
|
|
56
|
|
|
if ( $handler instanceof CacheInterface ) $manager->addProvider($handler);
|
57
|
|
|
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
return $manager;
|
65
|
|
|
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Create the Cache Manager
|
70
|
|
|
*
|
71
|
|
|
* @return \Comodojo\Cache\CacheManager
|
72
|
|
|
*/
|
73
|
|
|
public static function create(Configuration $configuration, LoggerInterface $logger) {
|
74
|
|
|
|
75
|
|
|
$cache = new DispatcherCache($configuration, $logger);
|
|
|
|
|
76
|
|
|
|
77
|
|
|
return $cache->init();
|
78
|
|
|
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
protected function getHandler($provider, $parameters) {
|
82
|
|
|
|
83
|
|
|
switch ( strtolower($parameters['type']) ) {
|
84
|
|
|
|
85
|
|
|
case 'file':
|
|
|
|
|
86
|
|
|
|
87
|
|
|
$base = $this->configuration->get('base-path');
|
88
|
|
|
|
89
|
|
|
if ( empty($parameters['folder']) || empty($base) ) {
|
90
|
|
|
$this->logger->warning("Wrong cache provider, disabling $provider", $parameters);
|
91
|
|
|
break;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
$target = $base.'/'.$parameters['folder'];
|
95
|
|
|
|
96
|
|
|
$handler = new FileCache($target);
|
97
|
|
|
|
98
|
|
|
break;
|
99
|
|
|
|
100
|
|
|
case 'memcached':
|
|
|
|
|
101
|
|
|
|
102
|
|
|
if ( empty($parameters['host']) ) {
|
103
|
|
|
$this->logger->warning("Wrong cache provider, disabling $provider", $parameters);
|
104
|
|
|
break;
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
$port = empty($parameters['port']) ? 11211 : intval($parameters['port']);
|
108
|
|
|
|
109
|
|
|
$weight = empty($parameters['weight']) ? 0 : intval($parameters['weight']);
|
110
|
|
|
|
111
|
|
|
$persistentid = empty($parameters['persistent-id']) ? null : boolval($parameters['persistentid']);
|
112
|
|
|
|
113
|
|
|
$handler = new MemcachedCache($host, $port, $weight, $persistentid);
|
|
|
|
|
114
|
|
|
|
115
|
|
|
break;
|
116
|
|
|
|
117
|
|
|
default:
|
118
|
|
|
$handler = null;
|
119
|
|
|
break;
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
return $handler;
|
|
|
|
|
123
|
|
|
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
/**
|
127
|
|
|
* Map provided log level to level code
|
128
|
|
|
*
|
129
|
|
|
* @param string $algorithm
|
130
|
|
|
*
|
131
|
|
|
* @return integer
|
132
|
|
|
*/
|
133
|
|
|
protected static function getAlgorithm($algorithm = null) {
|
134
|
|
|
|
135
|
|
|
switch ( strtoupper($algorithm) ) {
|
136
|
|
|
|
137
|
|
|
case 'PICK_LAST':
|
138
|
|
|
$selected = CacheManager::PICK_LAST;
|
139
|
|
|
break;
|
140
|
|
|
|
141
|
|
|
case 'PICK_RANDOM':
|
142
|
|
|
$selected = CacheManager::PICK_RANDOM;
|
143
|
|
|
break;
|
144
|
|
|
|
145
|
|
|
case 'PICK_BYWEIGHT':
|
146
|
|
|
$selected = CacheManager::PICK_BYWEIGHT;
|
147
|
|
|
break;
|
148
|
|
|
|
149
|
|
|
case 'PICK_ALL':
|
150
|
|
|
$selected = CacheManager::PICK_ALL;
|
151
|
|
|
break;
|
152
|
|
|
|
153
|
|
|
case 'PICK_FIRST':
|
154
|
|
|
default:
|
155
|
|
|
$selected = CacheManager::PICK_FIRST;
|
156
|
|
|
break;
|
157
|
|
|
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
return $selected;
|
161
|
|
|
|
162
|
|
|
}
|
163
|
|
|
|
164
|
|
|
}
|
165
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.