|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Framework\ServiceProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Cache\Adapter\Filesystem\FilesystemCachePool; |
|
6
|
|
|
use Cache\Adapter\PHPArray\ArrayCachePool; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use League\Flysystem\Filesystem; |
|
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use Venta\Cache\Cache; |
|
13
|
|
|
use Venta\Contracts\Cache\Cache as CacheContract; |
|
14
|
|
|
use Venta\Contracts\Config\Config; |
|
15
|
|
|
use Venta\Contracts\Debug\ErrorHandler; |
|
16
|
|
|
use Venta\ServiceProvider\AbstractServiceProvider; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class CacheServiceProvider |
|
20
|
|
|
* |
|
21
|
|
|
* @package Venta\Framework\ServiceProvider |
|
22
|
|
|
*/ |
|
23
|
|
|
class CacheServiceProvider extends AbstractServiceProvider |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param $name |
|
27
|
|
|
* @param $arguments |
|
28
|
|
|
* @return void |
|
|
|
|
|
|
29
|
|
|
* @throws InvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __call($name, $arguments) |
|
32
|
|
|
{ |
|
33
|
|
|
throw new InvalidArgumentException( |
|
34
|
|
|
sprintf('Unknown cache driver "%s" specified.', substr($name, 0, strlen($name) - 16)) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function boot() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->container()->bindClass(CacheContract::class, Cache::class); |
|
44
|
|
|
/** @var Config $config */ |
|
45
|
|
|
$config = $this->container()->get(Config::class); |
|
46
|
|
|
try { |
|
47
|
|
|
$cacheDriver = $config->get('cache.driver'); |
|
48
|
|
|
if ($cacheDriver === null) { |
|
49
|
|
|
throw new RuntimeException('Undefined cache driver.'); |
|
50
|
|
|
} |
|
51
|
|
|
switch ($cacheDriver) { |
|
52
|
|
|
case 'array': |
|
53
|
|
|
case 'memory': |
|
54
|
|
|
$this->container()->bindClass(CacheItemPoolInterface::class, ArrayCachePool::class, true); |
|
55
|
|
|
break; |
|
56
|
|
|
case 'void': |
|
57
|
|
|
case 'null': |
|
58
|
|
|
$this->container() |
|
59
|
|
|
->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Void\VoidCachePool', true); |
|
60
|
|
|
break; |
|
61
|
|
|
case 'file': |
|
62
|
|
|
case 'files': |
|
63
|
|
|
case 'filesystem': |
|
64
|
|
|
$this->container() |
|
65
|
|
|
->bindFactory(CacheItemPoolInterface::class, $this->filesystemCachePoolFactory(), true); |
|
66
|
|
|
break; |
|
67
|
|
|
case 'redis': |
|
68
|
|
|
$this->container() |
|
69
|
|
|
->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Redis\RedisCachePool', true); |
|
70
|
|
|
break; |
|
71
|
|
|
case 'predis': |
|
72
|
|
|
$this->container() |
|
73
|
|
|
->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Predis\PredisCachePool', true); |
|
74
|
|
|
break; |
|
75
|
|
|
case 'memcached': |
|
76
|
|
|
$this->container() |
|
77
|
|
|
->bindClass(CacheItemPoolInterface::class, 'Cache\Adapter\Memcached\MemcachedCachePool', true); |
|
78
|
|
|
break; |
|
79
|
|
|
default: |
|
80
|
|
|
$this->container()->bindClass( |
|
81
|
|
|
CacheItemPoolInterface::class, |
|
82
|
|
|
([$this, $config->cache->driver . 'CachePoolFactory'])(), |
|
|
|
|
|
|
83
|
|
|
true |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} catch (Throwable $e) { |
|
87
|
|
|
$this->container()->get(ErrorHandler::class)->handleThrowable($e); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return callable |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function filesystemCachePoolFactory(): callable |
|
95
|
|
|
{ |
|
96
|
|
|
return function (Filesystem $flysystem) { |
|
97
|
|
|
return new FilesystemCachePool($flysystem, 'storage/cache'); |
|
98
|
|
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
} |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.