Subversion-Projekte lars-tiefland.laravel_shop

Revision

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