1 | <?php namespace Comodojo\Cache\Components; |
||
31 | class ConfigurationParser { |
||
32 | |||
33 | protected static $algorithms = array( |
||
34 | 'PICK_FIRST' => 1, |
||
35 | 'PICK_LAST' => 2, |
||
36 | 'PICK_RANDOM' => 3, |
||
37 | 'PICK_BYWEIGHT' => 4, |
||
38 | 'PICK_ALL' => 4, |
||
39 | 'PICK_TRAVERSE' => 6 |
||
40 | ); |
||
41 | |||
42 | 2 | public static function parse(Configuration $configuration, LoggerInterface $logger) { |
|
43 | |||
44 | 2 | list($enable, $manager) = self::parseManagerConfiguration($configuration, $logger); |
|
45 | 2 | $providers = self::buildProviders($configuration, $logger); |
|
46 | |||
47 | return [ |
||
48 | $enable, |
||
49 | $manager, |
||
50 | $providers |
||
51 | ]; |
||
52 | |||
53 | } |
||
54 | |||
55 | 1 | protected static function BuildApcProvider(LoggerInterface $logger) { |
|
60 | |||
61 | protected static function BuildApcuProvider(LoggerInterface $logger) { |
||
62 | |||
63 | return new CacheApcu([], $logger); |
||
64 | |||
65 | } |
||
66 | |||
67 | protected static function BuildFilesystemProvider(array $properties, LoggerInterface $logger) { |
||
68 | |||
69 | return new CacheFilesystem($properties, $logger); |
||
70 | |||
71 | } |
||
72 | |||
73 | protected static function BuildMemcachedProvider(array $properties, LoggerInterface $logger) { |
||
74 | |||
75 | return new CacheMemcached($properties, $logger); |
||
76 | |||
77 | } |
||
78 | |||
79 | protected static function BuildMemoryProvider(LoggerInterface $logger) { |
||
80 | |||
81 | return new CacheMemory([], $logger); |
||
82 | |||
83 | } |
||
84 | |||
85 | protected static function BuildPhpRedisProvider(array $properties, LoggerInterface $logger) { |
||
86 | |||
87 | return new CachePhpRedis($properties, $logger); |
||
88 | |||
89 | } |
||
90 | |||
91 | protected static function BuildVacuumProvider(LoggerInterface $logger) { |
||
96 | |||
97 | 2 | protected static function parseManagerConfiguration(Configuration $configuration, LoggerInterface $logger) { |
|
122 | |||
123 | 2 | protected static function buildProviders(Configuration $configuration, LoggerInterface $logger) { |
|
124 | |||
125 | 2 | $cache = $configuration->get('cache'); |
|
126 | 2 | $build = []; |
|
127 | |||
128 | 2 | if ( $cache === null ) return $build; |
|
129 | |||
130 | 2 | $lower_cache = array_change_key_case($cache, CASE_LOWER); |
|
131 | |||
132 | 2 | if ( !isset($lower_cache['providers']) || !is_array($lower_cache['providers']) ) return $build; |
|
133 | |||
134 | 2 | $providers = $lower_cache['providers']; |
|
135 | |||
136 | 2 | foreach ( $providers as $name => $specs ) { |
|
137 | |||
138 | 2 | if ( !is_array($specs) ) { |
|
139 | $logger->error("Invalid specs for cache provider: $name"); |
||
140 | continue; |
||
141 | } |
||
142 | |||
143 | 2 | $spec = array_change_key_case($specs, CASE_LOWER); |
|
144 | |||
145 | 2 | if ( empty($spec['type']) ) { |
|
146 | $logger->error("Missing type for cache provider: $name"); |
||
147 | continue; |
||
148 | } |
||
149 | |||
150 | 2 | $type = strtoupper($spec['type']); |
|
151 | |||
152 | switch ( $type ) { |
||
153 | |||
154 | 2 | case 'APC': |
|
155 | 2 | $provider = static::BuildApcProvider($logger); |
|
156 | break; |
||
157 | |||
158 | case 'APCU': |
||
159 | $provider = static::BuildApcuProvider($logger); |
||
160 | break; |
||
161 | |||
162 | case 'FILESYSTEM': |
||
|
|||
163 | |||
164 | $stdConfig = [ |
||
165 | 'cache_folder' => null |
||
166 | ]; |
||
167 | |||
168 | if ( isset($spec['cache_folder']) ) { |
||
169 | if ( $spec['cache_folder'][0] == "/" ) { |
||
170 | $stdConfig['cache_folder'] = $spec['cache_folder']; |
||
171 | } else { |
||
172 | $stdConfig['cache_folder'] = $configuration->get('base-path')."/".$spec['cache_folder']; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $provider = static::BuildFilesystemProvider($stdConfig, $logger); |
||
177 | |||
178 | break; |
||
179 | |||
180 | case 'MEMCACHED': |
||
181 | |||
182 | $valid_values = [ |
||
183 | 'server', |
||
184 | 'port', |
||
185 | 'weight', |
||
186 | 'persistent_id', |
||
187 | 'username', |
||
188 | 'password' |
||
189 | ]; |
||
190 | |||
191 | $stdConfig = ArrayOps::filterByKeys($valid_values, $spec); |
||
192 | |||
193 | $provider = static::BuildMemcachedProvider($stdConfig, $logger); |
||
194 | break; |
||
195 | |||
196 | case 'MEMORY': |
||
197 | $provider = static::BuildMemoryProvider($logger); |
||
198 | break; |
||
199 | |||
200 | case 'PHPREDIS': |
||
201 | |||
202 | $valid_values = [ |
||
203 | 'server', |
||
204 | 'port', |
||
205 | 'timeout', |
||
206 | 'logger', |
||
207 | 'password' |
||
208 | ]; |
||
209 | |||
210 | $stdConfig = ArrayOps::filterByKeys($valid_values, $spec); |
||
211 | |||
212 | $provider = static::BuildPhpRedisProvider($stdConfig, $logger); |
||
213 | break; |
||
214 | |||
215 | case 'VACUUM': |
||
216 | $provider = static::BuildVacuumProvider($logger); |
||
217 | break; |
||
218 | |||
219 | default: |
||
220 | $logger->error("Unknown type $type for cache provider: $name"); |
||
221 | continue 2; |
||
222 | break; |
||
223 | |||
224 | } |
||
225 | |||
226 | $build[$name] = (object) [ |
||
227 | "instance" => $provider, |
||
228 | "weight" => isset($spec['weight']) ? |
||
229 | DataFilter::filterInteger($spec['weight'], 0, 100, 0) : 0 |
||
230 | ]; |
||
231 | |||
232 | } |
||
233 | |||
234 | return $build; |
||
235 | |||
236 | } |
||
237 | |||
238 | 2 | protected static function getPickMode($algorithm = null) { |
|
247 | |||
248 | } |
||
249 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.