1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Cache;
|
4
|
|
5
|
use Closure;
|
6
|
use Illuminate\Support\Arr;
|
7
|
use InvalidArgumentException;
|
8
|
use Illuminate\Contracts\Cache\Store;
|
9
|
use Illuminate\Contracts\Cache\Factory as FactoryContract;
|
10
|
|
11
|
class CacheManager implements FactoryContract
|
12
|
{
|
13
|
/**
|
14
|
* The application instance.
|
15
|
*
|
16
|
* @var \Illuminate\Foundation\Application
|
17
|
*/
|
18
|
protected $app;
|
19
|
|
20
|
/**
|
21
|
* The array of resolved cache stores.
|
22
|
*
|
23
|
* @var array
|
24
|
*/
|
25
|
protected $stores = [];
|
26
|
|
27
|
/**
|
28
|
* The registered custom driver creators.
|
29
|
*
|
30
|
* @var array
|
31
|
*/
|
32
|
protected $customCreators = [];
|
33
|
|
34
|
/**
|
35
|
* Create a new Cache manager instance.
|
36
|
*
|
37
|
* @param \Illuminate\Foundation\Application $app
|
38
|
* @return void
|
39
|
*/
|
40
|
public function __construct($app)
|
41
|
{
|
42
|
$this->app = $app;
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Get a cache store instance by name.
|
47
|
*
|
48
|
* @param string|null $name
|
49
|
* @return mixed
|
50
|
*/
|
51
|
public function store($name = null)
|
52
|
{
|
53
|
$name = $name ?: $this->getDefaultDriver();
|
54
|
|
55
|
return $this->stores[$name] = $this->get($name);
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Get a cache driver instance.
|
60
|
*
|
61
|
* @param string $driver
|
62
|
* @return mixed
|
63
|
*/
|
64
|
public function driver($driver = null)
|
65
|
{
|
66
|
return $this->store($driver);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Attempt to get the store from the local cache.
|
71
|
*
|
72
|
* @param string $name
|
73
|
* @return \Illuminate\Contracts\Cache\Repository
|
74
|
*/
|
75
|
protected function get($name)
|
76
|
{
|
77
|
return isset($this->stores[$name]) ? $this->stores[$name] : $this->resolve($name);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Resolve the given store.
|
82
|
*
|
83
|
* @param string $name
|
84
|
* @return \Illuminate\Contracts\Cache\Repository
|
85
|
*
|
86
|
* @throws \InvalidArgumentException
|
87
|
*/
|
88
|
protected function resolve($name)
|
89
|
{
|
90
|
$config = $this->getConfig($name);
|
91
|
|
92
|
if (is_null($config)) {
|
93
|
throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
|
94
|
}
|
95
|
|
96
|
if (isset($this->customCreators[$config['driver']])) {
|
97
|
return $this->callCustomCreator($config);
|
98
|
} else {
|
99
|
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';
|
100
|
|
101
|
if (method_exists($this, $driverMethod)) {
|
102
|
return $this->{$driverMethod}($config);
|
103
|
} else {
|
104
|
throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Call a custom driver creator.
|
111
|
*
|
112
|
* @param array $config
|
113
|
* @return mixed
|
114
|
*/
|
115
|
protected function callCustomCreator(array $config)
|
116
|
{
|
117
|
return $this->customCreators[$config['driver']]($this->app, $config);
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Create an instance of the APC cache driver.
|
122
|
*
|
123
|
* @param array $config
|
124
|
* @return \Illuminate\Cache\ApcStore
|
125
|
*/
|
126
|
protected function createApcDriver(array $config)
|
127
|
{
|
128
|
$prefix = $this->getPrefix($config);
|
129
|
|
130
|
return $this->repository(new ApcStore(new ApcWrapper, $prefix));
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Create an instance of the array cache driver.
|
135
|
*
|
136
|
* @return \Illuminate\Cache\ArrayStore
|
137
|
*/
|
138
|
protected function createArrayDriver()
|
139
|
{
|
140
|
return $this->repository(new ArrayStore);
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Create an instance of the file cache driver.
|
145
|
*
|
146
|
* @param array $config
|
147
|
* @return \Illuminate\Cache\FileStore
|
148
|
*/
|
149
|
protected function createFileDriver(array $config)
|
150
|
{
|
151
|
return $this->repository(new FileStore($this->app['files'], $config['path']));
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Create an instance of the Memcached cache driver.
|
156
|
*
|
157
|
* @param array $config
|
158
|
* @return \Illuminate\Cache\MemcachedStore
|
159
|
*/
|
160
|
protected function createMemcachedDriver(array $config)
|
161
|
{
|
162
|
$prefix = $this->getPrefix($config);
|
163
|
|
164
|
$memcached = $this->app['memcached.connector']->connect($config['servers']);
|
165
|
|
166
|
return $this->repository(new MemcachedStore($memcached, $prefix));
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Create an instance of the Null cache driver.
|
171
|
*
|
172
|
* @return \Illuminate\Cache\NullStore
|
173
|
*/
|
174
|
protected function createNullDriver()
|
175
|
{
|
176
|
return $this->repository(new NullStore);
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Create an instance of the Redis cache driver.
|
181
|
*
|
182
|
* @param array $config
|
183
|
* @return \Illuminate\Cache\RedisStore
|
184
|
*/
|
185
|
protected function createRedisDriver(array $config)
|
186
|
{
|
187
|
$redis = $this->app['redis'];
|
188
|
|
189
|
$connection = Arr::get($config, 'connection', 'default');
|
190
|
|
191
|
return $this->repository(new RedisStore($redis, $this->getPrefix($config), $connection));
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Create an instance of the database cache driver.
|
196
|
*
|
197
|
* @param array $config
|
198
|
* @return \Illuminate\Cache\DatabaseStore
|
199
|
*/
|
200
|
protected function createDatabaseDriver(array $config)
|
201
|
{
|
202
|
$connection = $this->app['db']->connection(Arr::get($config, 'connection'));
|
203
|
|
204
|
return $this->repository(
|
205
|
new DatabaseStore(
|
206
|
$connection, $this->app['encrypter'], $config['table'], $this->getPrefix($config)
|
207
|
)
|
208
|
);
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* Create a new cache repository with the given implementation.
|
213
|
*
|
214
|
* @param \Illuminate\Contracts\Cache\Store $store
|
215
|
* @return \Illuminate\Cache\Repository
|
216
|
*/
|
217
|
public function repository(Store $store)
|
218
|
{
|
219
|
$repository = new Repository($store);
|
220
|
|
221
|
if ($this->app->bound('Illuminate\Contracts\Events\Dispatcher')) {
|
222
|
$repository->setEventDispatcher(
|
223
|
$this->app['Illuminate\Contracts\Events\Dispatcher']
|
224
|
);
|
225
|
}
|
226
|
|
227
|
return $repository;
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Get the cache prefix.
|
232
|
*
|
233
|
* @param array $config
|
234
|
* @return string
|
235
|
*/
|
236
|
protected function getPrefix(array $config)
|
237
|
{
|
238
|
return Arr::get($config, 'prefix') ?: $this->app['config']['cache.prefix'];
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Get the cache connection configuration.
|
243
|
*
|
244
|
* @param string $name
|
245
|
* @return array
|
246
|
*/
|
247
|
protected function getConfig($name)
|
248
|
{
|
249
|
return $this->app['config']["cache.stores.{$name}"];
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Get the default cache driver name.
|
254
|
*
|
255
|
* @return string
|
256
|
*/
|
257
|
public function getDefaultDriver()
|
258
|
{
|
259
|
return $this->app['config']['cache.default'];
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* Set the default cache driver name.
|
264
|
*
|
265
|
* @param string $name
|
266
|
* @return void
|
267
|
*/
|
268
|
public function setDefaultDriver($name)
|
269
|
{
|
270
|
$this->app['config']['cache.default'] = $name;
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* Register a custom driver creator Closure.
|
275
|
*
|
276
|
* @param string $driver
|
277
|
* @param \Closure $callback
|
278
|
* @return $this
|
279
|
*/
|
280
|
public function extend($driver, Closure $callback)
|
281
|
{
|
282
|
$this->customCreators[$driver] = $callback;
|
283
|
|
284
|
return $this;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Dynamically call the default driver instance.
|
289
|
*
|
290
|
* @param string $method
|
291
|
* @param array $parameters
|
292
|
* @return mixed
|
293
|
*/
|
294
|
public function __call($method, $parameters)
|
295
|
{
|
296
|
return call_user_func_array([$this->store(), $method], $parameters);
|
297
|
}
|
298
|
}
|