1 | <?php namespace Comodojo\Dispatcher\Components; |
||
35 | class CacheManager extends DispatcherClassModel { |
||
36 | |||
37 | protected static $algorithms = array( |
||
38 | 'PICK_LAST' => Cache::PICK_LAST, |
||
39 | 'PICK_RANDOM' => Cache::PICK_RANDOM, |
||
40 | 'PICK_BYWEIGHT' => Cache::PICK_BYWEIGHT, |
||
41 | 'PICK_ALL' => Cache::PICK_ALL, |
||
42 | 'PICK_TRAVERSE' => Cache::PICK_TRAVERSE, |
||
43 | 'PICK_FIRST' => Cache::PICK_FIRST |
||
44 | ); |
||
45 | |||
46 | public function init() { |
||
47 | |||
48 | $cache = $this->configuration->get('cache'); |
||
49 | |||
50 | $algorithm = self::getAlgorithm( empty($cache['algorithm']) ? null : $cache['algorithm']); |
||
51 | |||
52 | $manager = new Cache($algorithm, $this->logger); |
||
53 | |||
54 | if ( !empty($cache) && !empty($cache['providers']) && is_array($cache['providers']) ) { |
||
55 | |||
56 | foreach ($cache['providers'] as $provider => $parameters) { |
||
57 | |||
58 | list($handler, $weight) = $this->getHandler($provider, $parameters); |
||
59 | |||
60 | if ( $handler instanceof ProviderInterface ) $manager->addProvider($handler, $weight); |
||
61 | |||
62 | } |
||
63 | |||
64 | } |
||
65 | |||
66 | return $manager; |
||
67 | |||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Create the Cache Manager |
||
72 | * |
||
73 | * @return \Comodojo\Cache\Cache |
||
74 | */ |
||
75 | public static function create(Configuration $configuration, LoggerInterface $logger) { |
||
76 | |||
77 | $cache = new CacheManager($configuration, $logger); |
||
78 | |||
79 | return $cache->init(); |
||
80 | |||
81 | } |
||
82 | |||
83 | protected function getHandler($provider, $parameters) { |
||
84 | |||
85 | if ( empty($parameters['type']) ) return array(null, null); |
||
86 | |||
87 | $weight = empty($parameters['weight']) ? 0 : $parameters['weight']; |
||
88 | |||
89 | switch ( $parameters['type'] ) { |
||
90 | |||
91 | case 'Filesystem': |
||
|
|||
92 | |||
93 | $handler = $this->getFilesystemProvider($provider, $parameters); |
||
94 | |||
95 | break; |
||
96 | |||
97 | case 'Apc': |
||
98 | |||
99 | $handler = $this->getApcProvider($provider, $parameters); |
||
100 | |||
101 | break; |
||
102 | |||
103 | case 'Memcached': |
||
104 | |||
105 | $handler = $this->getMemcachedProvider($provider, $parameters); |
||
106 | |||
107 | break; |
||
108 | |||
109 | case 'PhpRedis': |
||
110 | |||
111 | $handler = $this->getPhpRedisProvider($provider, $parameters); |
||
112 | |||
113 | break; |
||
114 | |||
115 | default: |
||
116 | $handler = null; |
||
117 | break; |
||
118 | } |
||
119 | |||
120 | return array($handler, $weight); |
||
121 | |||
122 | } |
||
123 | |||
124 | protected function getFilesystemProvider($provider, $parameters) { |
||
125 | |||
126 | $base = $this->configuration->get('base-path'); |
||
127 | |||
128 | if ( empty($parameters['folder']) || empty($base) ) { |
||
129 | $this->logger->warning("Wrong cache provider, disabling $provider", $parameters); |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | $target = $base.'/'.$parameters['folder']; |
||
134 | |||
135 | return new Filesystem($target); |
||
136 | |||
137 | } |
||
138 | |||
139 | protected function getApcProvider($provider, $parameters) { |
||
144 | |||
145 | protected function getMemcachedProvider($provider, $parameters) { |
||
155 | |||
156 | protected function getPhpRedisProvider($provider, $parameters) { |
||
165 | |||
166 | /** |
||
167 | * Map provided log level to level code |
||
168 | * |
||
169 | * @param string $algorithm |
||
170 | * |
||
171 | * @return integer |
||
172 | */ |
||
173 | protected static function getAlgorithm($algorithm = null) { |
||
182 | |||
183 | } |
||
184 |
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.