-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheServiceProvider.php
308 lines (271 loc) · 9.97 KB
/
CacheServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
namespace Charcoal\Cache\ServiceProvider;
// From Pimple
use Charcoal\Cache\Facade\CachePoolFacade;
use Pimple\ServiceProviderInterface;
use Pimple\Container;
// From 'tedivm/stash'
use Stash\DriverList;
use Stash\Interfaces\DriverInterface;
use Stash\Pool;
// From 'charcoal-cache'
use Charcoal\Cache\CacheBuilder;
use Charcoal\Cache\CacheConfig;
use Charcoal\Cache\Middleware\CacheMiddleware;
/**
* Cache Service Provider
*
* Provides a Stash cache pool (PSR-6 compatible).
*
* ## Dependencies
*
* - `config`: An {@link https://packagist.org/packages/locomotivemtl/charcoal-app application configset}.
* - `logger` A {@link https://packagist.org/providers/psr/log-implementation PSR-3 logging client}.
*
* ## Services
*
* - `cache`: The default PSR-6 cache pool
*
* ## Helpers
*
* - `cache/config`: The cache configset.
* - `cache/driver`: The cache driver of the default pool.
* - `cache/builder`: An advacned cache pool factory.
*
* ## Middleware
*
* - `middlewares/charcoal/cache/middleware/cache`: For caching HTTP responses.
*
*/
class CacheServiceProvider implements ServiceProviderInterface
{
/**
* @param Container $container A container instance.
* @return void
*/
public function register(Container $container)
{
$this->registerDrivers($container);
$this->registerService($container);
$this->registerMiddleware($container);
}
/**
* @param Container $container A container instance.
* @return void
*/
public function registerDrivers(Container $container)
{
/**
* The collection of cache drivers that are supported by this system.
*
* @var array An associative array structured as `"Driver Name" => "Class Name"`.
*/
$container['cache/available-drivers'] = DriverList::getAvailableDrivers();
/**
* The collection of cache driver instances.
*
* @param Container $container The service container.
* @return Container Service container of cache drivers from Stash.
*/
$container['cache/drivers'] = function (Container $container) {
$drivers = new Container();
/**
* @param Container $container The service container.
* @return \Stash\Driver\Apc|null
*/
$drivers['apc'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
if (!isset($drivers['Apc'])) {
// Apc is not available on system
return null;
}
$cacheConfig = $container['cache/config'];
$driverOptions = [
'ttl' => $cacheConfig['default_ttl'],
'namespace' => $cacheConfig['prefix'],
];
return new $drivers['Apc']($driverOptions);
};
/**
* @param Container $container A container instance.
* @return \Stash\Driver\Sqlite|null
*/
$drivers['db'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
if (!isset($drivers['SQLite'])) {
// SQLite is not available on system
return null;
}
return new $drivers['SQLite']();
};
/**
* @param Container $container A container instance.
* @return \Stash\Driver\FileSystem
*/
$drivers['file'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
return new $drivers['FileSystem']();
};
/**
* @param Container $container A container instance.
* @return \Stash\Driver\Memcache|null
*/
$drivers['memcache'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
if (!isset($drivers['Memcache'])) {
// Memcache is not available on system
return null;
}
$cacheConfig = $container['cache/config'];
$driverOptions = [
'servers' => [],
];
if (isset($cacheConfig['servers'])) {
$servers = [];
foreach ($cacheConfig['servers'] as $server) {
$servers[] = array_values($server);
}
$driverOptions['servers'] = $servers;
} else {
// Default Memcache options: locahost:11211
$driverOptions['servers'][] = [ '127.0.0.1', 11211 ];
}
return new $drivers['Memcache']($driverOptions);
};
/**
* @param Container $container A container instance.
* @return \Stash\Driver\Ephemeral
*/
$drivers['memory'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
return new $drivers['Ephemeral']();
};
/**
* @param Container $container A container instance.
* @return \Stash\Driver\BlackHole
*/
$drivers['noop'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
return new $drivers['BlackHole']();
};
/**
* @param Container $container A container instance.
* @return \Stash\Driver\Redis|null
*/
$drivers['redis'] = function () use ($container) {
$drivers = $container['cache/available-drivers'];
if (!isset($drivers['Redis'])) {
// Redis is not available on system
return null;
}
return new $drivers['Redis']();
};
return $drivers;
};
}
/**
* @param Container $container A container instance.
* @return void
*/
public function registerService(Container $container)
{
/**
* The cache configset.
*
* @param Container $container The service container.
* @return CacheConfig
*/
$container['cache/config'] = function (Container $container) {
$appConfig = isset($container['config']) ? $container['config'] : [];
$cacheConfig = isset($appConfig['cache']) ? $appConfig['cache'] : null;
return new CacheConfig($cacheConfig);
};
/**
* A cache pool builder, using Stash.
*
* @param Container $container A Pimple DI container.
* @return CacheBuilder
*/
$container['cache/builder'] = function (Container $container) {
$cacheConfig = $container['cache/config'];
return new CacheBuilder([
'logger' => $container['logger'],
'drivers' => $container['cache/drivers'],
'namespace' => $cacheConfig['prefix'],
]);
};
/**
* The driver of the main cache pool "cache".
*
* @param Container $container The service container.
* @return DriverInterface Primary cache driver from Stash.
*/
$container['cache/driver'] = $container->factory(function (Container $container) {
return $container['cache']->getDriver();
});
/**
* The main cache item pool.
*
* @param Container $container The service container.
* @return Pool The cache item pool from Stash.
*/
$container['cache'] = function (Container $container) {
$cacheBuilder = $container['cache/builder'];
$cacheConfig = $container['cache/config'];
if ($cacheConfig['active'] === true) {
$cacheDrivers = $cacheConfig['types'];
} else {
$cacheDrivers = $cacheConfig['default_types'];
}
return $cacheBuilder($cacheDrivers);
};
/**
* The facade for the main cache pool.
*
* @param Container $container The service container.
* @return CachePoolFacade The facade for the main cache pool.
*/
$container['cache/facade'] = function (Container $container) {
$args = [
'cache' => $container['cache'],
];
$cacheConfig = $container['cache/config'];
if (isset($cacheConfig['default_ttl'])) {
$args['default_ttl'] = $cacheConfig['default_ttl'];
}
return new CachePoolFacade($args);
};
}
/**
* @param Container $container A container instance.
* @return void
*/
private function registerMiddleware(Container $container)
{
/**
* The cache middleware configset.
*
* @param Container $container The service container.
* @return array
*/
$container['cache/middleware/config'] = function (Container $container) {
$appConfig = isset($container['config']) ? $container['config'] : [];
if (isset($appConfig['middlewares']['charcoal/cache/middleware/cache'])) {
$wareConfig = $appConfig['middlewares']['charcoal/cache/middleware/cache'];
} else {
$wareConfig = [];
}
$wareConfig['cache'] = $container['cache'];
return $wareConfig;
};
/**
* The middleware for caching HTTP responses.
*
* @param Container $container A container instance.
* @return CacheMiddleware
*/
$container['middlewares/charcoal/cache/middleware/cache'] = function (Container $container) {
return new CacheMiddleware($container['cache/middleware/config']);
};
}
}