1: <?php
2:
3: namespace Charcoal\App\ServiceProvider;
4:
5:
6: use Pimple\ServiceProviderInterface;
7: use Pimple\Container;
8:
9:
10: use Stash\DriverList;
11: use Stash\Pool;
12:
13:
14: use Charcoal\App\Config\CacheConfig;
15: use Charcoal\App\Middleware\CacheMiddleware;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: class CacheServiceProvider implements ServiceProviderInterface
36: {
37: 38: 39: 40: 41: 42: 43: 44: 45:
46: public function register(Container $container)
47: {
48: 49: 50: 51:
52: $container['cache/config'] = function (Container $container) {
53: $appConfig = $container['config'];
54:
55: $cacheConfig = new CacheConfig($appConfig['cache']);
56: return $cacheConfig;
57: };
58:
59: $container['cache/available-drivers'] = DriverList::getAvailableDrivers();
60:
61: 62: 63: 64:
65: $container['cache/drivers'] = function (Container $container) {
66: $drivers = new Container();
67:
68: $parentContainer = $container;
69:
70: 71: 72: 73:
74: $drivers['apc'] = function () use ($parentContainer) {
75: $drivers = $parentContainer['cache/available-drivers'];
76: if (!isset($drivers['Apc'])) {
77:
78: return null;
79: }
80: return new $drivers['Apc']();
81: };
82:
83: 84: 85: 86:
87: $drivers['db'] = function () use ($parentContainer) {
88: $drivers = $parentContainer['cache/available-drivers'];
89: if (!isset($drivers['SQLite'])) {
90:
91: return null;
92: }
93: return new $drivers['SQLite']();
94: };
95:
96: 97: 98: 99:
100: $drivers['file'] = function () use ($parentContainer) {
101: $drivers = $parentContainer['cache/available-drivers'];
102: return new $drivers['FileSystem']();
103: };
104:
105: 106: 107: 108:
109: $drivers['memcache'] = function () use ($parentContainer) {
110: $drivers = $parentContainer['cache/available-drivers'];
111: if (!isset($drivers['Memcache'])) {
112:
113: return null;
114: }
115:
116: $cacheConfig = $parentContainer['cache/config'];
117: $driverOptions = [
118: 'servers' => []
119: ];
120:
121: if (isset($cacheConfig['servers'])) {
122: $servers = [];
123: foreach ($cacheConfig['servers'] as $server) {
124: $servers[] = array_values($server);
125: }
126: $driverOptions['servers'] = $servers;
127: } else {
128:
129: $driverOptions['servers'][] = [ '127.0.0.1', 11211 ];
130: }
131:
132: return new $drivers['Memcache']($driverOptions);
133: };
134:
135: 136: 137: 138:
139: $drivers['memory'] = function () use ($parentContainer) {
140: $drivers = $parentContainer['cache/available-drivers'];
141: return new $drivers['Ephemeral']();
142: };
143:
144: 145: 146: 147:
148: $drivers['noop'] = function () use ($parentContainer) {
149: $drivers = $parentContainer['cache/available-drivers'];
150: return new $drivers['BlackHole']();
151: };
152:
153: 154: 155: 156:
157: $drivers['redis'] = function () use ($parentContainer) {
158: $drivers = $parentContainer['cache/available-drivers'];
159: if (!isset($drivers['Redis'])) {
160:
161: return null;
162: }
163: return new $drivers['Redis']();
164: };
165:
166: return $drivers;
167: };
168:
169: 170: 171: 172:
173: $container['cache/driver'] = function (Container $container) {
174:
175: $cacheConfig = $container['cache/config'];
176: $types = $cacheConfig['types'];
177:
178: foreach ($types as $type) {
179: if (isset($container['cache/drivers'][$type])) {
180: return $container['cache/drivers'][$type];
181: }
182: }
183:
184:
185: return $container['cache/drivers']['memory'];
186: };
187:
188: 189: 190: 191: 192: 193:
194: $container['cache'] = function (Container $container) {
195:
196: $cacheConfig = $container['cache/config'];
197: $driver = $container['cache/driver'];
198:
199: $pool = new Pool($driver);
200: $pool->setLogger($container['logger']);
201:
202:
203: $namespace = preg_replace('/[^A-Za-z0-9 ]/', '', $cacheConfig['prefix']);
204: $pool->setNamespace($namespace);
205:
206: return $pool;
207: };
208:
209: 210: 211: 212: 213: 214:
215: $container['cache/middleware'] = function (Container $container) {
216: $cacheConfig = $container['cache/config'];
217: $middlewareConfig = $cacheConfig['middleware'];
218: return new CacheMiddleware([
219: 'cache' => $container['cache'],
220: 'included_path' => $middlewareConfig['included_path'],
221: 'excluded_path' => $middlewareConfig['excluded_path'],
222: 'methods' => $middlewareConfig['methods'],
223: 'status_codes' => $middlewareConfig['status_codes'],
224: 'ttl' => $middlewareConfig['ttl'],
225: 'included_query' => $middlewareConfig['included_query'],
226: 'excluded_query' => $middlewareConfig['excluded_query'],
227: 'ignored_query' => $middlewareConfig['ignored_query']
228: ]);
229: };
230: }
231: }
232: