Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Cache;
4
 
5
use Aws\DynamoDb\DynamoDbClient;
6
use Closure;
7
use Illuminate\Contracts\Cache\Factory as FactoryContract;
8
use Illuminate\Contracts\Cache\Store;
9
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
10
use Illuminate\Support\Arr;
11
use InvalidArgumentException;
12
 
13
/**
14
 * @mixin \Illuminate\Cache\Repository
15
 * @mixin \Illuminate\Contracts\Cache\LockProvider
16
 */
17
class CacheManager implements FactoryContract
18
{
19
    /**
20
     * The application instance.
21
     *
22
     * @var \Illuminate\Contracts\Foundation\Application
23
     */
24
    protected $app;
25
 
26
    /**
27
     * The array of resolved cache stores.
28
     *
29
     * @var array
30
     */
31
    protected $stores = [];
32
 
33
    /**
34
     * The registered custom driver creators.
35
     *
36
     * @var array
37
     */
38
    protected $customCreators = [];
39
 
40
    /**
41
     * Create a new Cache manager instance.
42
     *
43
     * @param  \Illuminate\Contracts\Foundation\Application  $app
44
     * @return void
45
     */
46
    public function __construct($app)
47
    {
48
        $this->app = $app;
49
    }
50
 
51
    /**
52
     * Get a cache store instance by name, wrapped in a repository.
53
     *
54
     * @param  string|null  $name
55
     * @return \Illuminate\Contracts\Cache\Repository
56
     */
57
    public function store($name = null)
58
    {
59
        $name = $name ?: $this->getDefaultDriver();
60
 
61
        return $this->stores[$name] = $this->get($name);
62
    }
63
 
64
    /**
65
     * Get a cache driver instance.
66
     *
67
     * @param  string|null  $driver
68
     * @return \Illuminate\Contracts\Cache\Repository
69
     */
70
    public function driver($driver = null)
71
    {
72
        return $this->store($driver);
73
    }
74
 
75
    /**
76
     * Attempt to get the store from the local cache.
77
     *
78
     * @param  string  $name
79
     * @return \Illuminate\Contracts\Cache\Repository
80
     */
81
    protected function get($name)
82
    {
83
        return $this->stores[$name] ?? $this->resolve($name);
84
    }
85
 
86
    /**
87
     * Resolve the given store.
88
     *
89
     * @param  string  $name
90
     * @return \Illuminate\Contracts\Cache\Repository
91
     *
92
     * @throws \InvalidArgumentException
93
     */
94
    protected function resolve($name)
95
    {
96
        $config = $this->getConfig($name);
97
 
98
        if (is_null($config)) {
99
            throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
100
        }
101
 
102
        if (isset($this->customCreators[$config['driver']])) {
103
            return $this->callCustomCreator($config);
104
        } else {
105
            $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
106
 
107
            if (method_exists($this, $driverMethod)) {
108
                return $this->{$driverMethod}($config);
109
            } else {
110
                throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
111
            }
112
        }
113
    }
114
 
115
    /**
116
     * Call a custom driver creator.
117
     *
118
     * @param  array  $config
119
     * @return mixed
120
     */
121
    protected function callCustomCreator(array $config)
122
    {
123
        return $this->customCreators[$config['driver']]($this->app, $config);
124
    }
125
 
126
    /**
127
     * Create an instance of the APC cache driver.
128
     *
129
     * @param  array  $config
130
     * @return \Illuminate\Cache\Repository
131
     */
132
    protected function createApcDriver(array $config)
133
    {
134
        $prefix = $this->getPrefix($config);
135
 
136
        return $this->repository(new ApcStore(new ApcWrapper, $prefix));
137
    }
138
 
139
    /**
140
     * Create an instance of the array cache driver.
141
     *
142
     * @param  array  $config
143
     * @return \Illuminate\Cache\Repository
144
     */
145
    protected function createArrayDriver(array $config)
146
    {
147
        return $this->repository(new ArrayStore($config['serialize'] ?? false));
148
    }
149
 
150
    /**
151
     * Create an instance of the file cache driver.
152
     *
153
     * @param  array  $config
154
     * @return \Illuminate\Cache\Repository
155
     */
156
    protected function createFileDriver(array $config)
157
    {
158
        return $this->repository(new FileStore($this->app['files'], $config['path'], $config['permission'] ?? null));
159
    }
160
 
161
    /**
162
     * Create an instance of the Memcached cache driver.
163
     *
164
     * @param  array  $config
165
     * @return \Illuminate\Cache\Repository
166
     */
167
    protected function createMemcachedDriver(array $config)
168
    {
169
        $prefix = $this->getPrefix($config);
170
 
171
        $memcached = $this->app['memcached.connector']->connect(
172
            $config['servers'],
173
            $config['persistent_id'] ?? null,
174
            $config['options'] ?? [],
175
            array_filter($config['sasl'] ?? [])
176
        );
177
 
178
        return $this->repository(new MemcachedStore($memcached, $prefix));
179
    }
180
 
181
    /**
182
     * Create an instance of the Null cache driver.
183
     *
184
     * @return \Illuminate\Cache\Repository
185
     */
186
    protected function createNullDriver()
187
    {
188
        return $this->repository(new NullStore);
189
    }
190
 
191
    /**
192
     * Create an instance of the Redis cache driver.
193
     *
194
     * @param  array  $config
195
     * @return \Illuminate\Cache\Repository
196
     */
197
    protected function createRedisDriver(array $config)
198
    {
199
        $redis = $this->app['redis'];
200
 
201
        $connection = $config['connection'] ?? 'default';
202
 
203
        $store = new RedisStore($redis, $this->getPrefix($config), $connection);
204
 
205
        return $this->repository(
206
            $store->setLockConnection($config['lock_connection'] ?? $connection)
207
        );
208
    }
209
 
210
    /**
211
     * Create an instance of the database cache driver.
212
     *
213
     * @param  array  $config
214
     * @return \Illuminate\Cache\Repository
215
     */
216
    protected function createDatabaseDriver(array $config)
217
    {
218
        $connection = $this->app['db']->connection($config['connection'] ?? null);
219
 
220
        $store = new DatabaseStore(
221
            $connection,
222
            $config['table'],
223
            $this->getPrefix($config),
224
            $config['lock_table'] ?? 'cache_locks',
225
            $config['lock_lottery'] ?? [2, 100]
226
        );
227
 
228
        return $this->repository($store->setLockConnection(
229
            $this->app['db']->connection($config['lock_connection'] ?? $config['connection'] ?? null)
230
        ));
231
    }
232
 
233
    /**
234
     * Create an instance of the DynamoDB cache driver.
235
     *
236
     * @param  array  $config
237
     * @return \Illuminate\Cache\Repository
238
     */
239
    protected function createDynamodbDriver(array $config)
240
    {
241
        $client = $this->newDynamodbClient($config);
242
 
243
        return $this->repository(
244
            new DynamoDbStore(
245
                $client,
246
                $config['table'],
247
                $config['attributes']['key'] ?? 'key',
248
                $config['attributes']['value'] ?? 'value',
249
                $config['attributes']['expiration'] ?? 'expires_at',
250
                $this->getPrefix($config)
251
            )
252
        );
253
    }
254
 
255
    /**
256
     * Create new DynamoDb Client instance.
257
     *
258
     * @return \Aws\DynamoDb\DynamoDbClient
259
     */
260
    protected function newDynamodbClient(array $config)
261
    {
262
        $dynamoConfig = [
263
            'region' => $config['region'],
264
            'version' => 'latest',
265
            'endpoint' => $config['endpoint'] ?? null,
266
        ];
267
 
268
        if (isset($config['key'], $config['secret'])) {
269
            $dynamoConfig['credentials'] = Arr::only(
270
                $config, ['key', 'secret', 'token']
271
            );
272
        }
273
 
274
        return new DynamoDbClient($dynamoConfig);
275
    }
276
 
277
    /**
278
     * Create a new cache repository with the given implementation.
279
     *
280
     * @param  \Illuminate\Contracts\Cache\Store  $store
281
     * @return \Illuminate\Cache\Repository
282
     */
283
    public function repository(Store $store)
284
    {
285
        return tap(new Repository($store), function ($repository) {
286
            $this->setEventDispatcher($repository);
287
        });
288
    }
289
 
290
    /**
291
     * Set the event dispatcher on the given repository instance.
292
     *
293
     * @param  \Illuminate\Cache\Repository  $repository
294
     * @return void
295
     */
296
    protected function setEventDispatcher(Repository $repository)
297
    {
298
        if (! $this->app->bound(DispatcherContract::class)) {
299
            return;
300
        }
301
 
302
        $repository->setEventDispatcher(
303
            $this->app[DispatcherContract::class]
304
        );
305
    }
306
 
307
    /**
308
     * Re-set the event dispatcher on all resolved cache repositories.
309
     *
310
     * @return void
311
     */
312
    public function refreshEventDispatcher()
313
    {
314
        array_map([$this, 'setEventDispatcher'], $this->stores);
315
    }
316
 
317
    /**
318
     * Get the cache prefix.
319
     *
320
     * @param  array  $config
321
     * @return string
322
     */
323
    protected function getPrefix(array $config)
324
    {
325
        return $config['prefix'] ?? $this->app['config']['cache.prefix'];
326
    }
327
 
328
    /**
329
     * Get the cache connection configuration.
330
     *
331
     * @param  string  $name
332
     * @return array|null
333
     */
334
    protected function getConfig($name)
335
    {
336
        if (! is_null($name) && $name !== 'null') {
337
            return $this->app['config']["cache.stores.{$name}"];
338
        }
339
 
340
        return ['driver' => 'null'];
341
    }
342
 
343
    /**
344
     * Get the default cache driver name.
345
     *
346
     * @return string
347
     */
348
    public function getDefaultDriver()
349
    {
350
        return $this->app['config']['cache.default'];
351
    }
352
 
353
    /**
354
     * Set the default cache driver name.
355
     *
356
     * @param  string  $name
357
     * @return void
358
     */
359
    public function setDefaultDriver($name)
360
    {
361
        $this->app['config']['cache.default'] = $name;
362
    }
363
 
364
    /**
365
     * Unset the given driver instances.
366
     *
367
     * @param  array|string|null  $name
368
     * @return $this
369
     */
370
    public function forgetDriver($name = null)
371
    {
372
        $name ??= $this->getDefaultDriver();
373
 
374
        foreach ((array) $name as $cacheName) {
375
            if (isset($this->stores[$cacheName])) {
376
                unset($this->stores[$cacheName]);
377
            }
378
        }
379
 
380
        return $this;
381
    }
382
 
383
    /**
384
     * Disconnect the given driver and remove from local cache.
385
     *
386
     * @param  string|null  $name
387
     * @return void
388
     */
389
    public function purge($name = null)
390
    {
391
        $name ??= $this->getDefaultDriver();
392
 
393
        unset($this->stores[$name]);
394
    }
395
 
396
    /**
397
     * Register a custom driver creator Closure.
398
     *
399
     * @param  string  $driver
400
     * @param  \Closure  $callback
401
     * @return $this
402
     */
403
    public function extend($driver, Closure $callback)
404
    {
405
        $this->customCreators[$driver] = $callback->bindTo($this, $this);
406
 
407
        return $this;
408
    }
409
 
410
    /**
411
     * Dynamically call the default driver instance.
412
     *
413
     * @param  string  $method
414
     * @param  array  $parameters
415
     * @return mixed
416
     */
417
    public function __call($method, $parameters)
418
    {
419
        return $this->store()->$method(...$parameters);
420
    }
421
}