|
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 Venta\Cache\Cache; |
|
12
|
|
|
use Venta\Contracts\Cache\Cache as CacheContract; |
|
13
|
|
|
use Venta\Contracts\Config\Config; |
|
14
|
|
|
use Venta\Contracts\Container\MutableContainer; |
|
15
|
|
|
use Venta\ServiceProvider\AbstractServiceProvider; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class CacheServiceProvider |
|
19
|
|
|
* |
|
20
|
|
|
* @package Venta\Framework\ServiceProvider |
|
21
|
|
|
*/ |
|
22
|
|
|
class CacheServiceProvider extends AbstractServiceProvider |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param $name |
|
26
|
|
|
* @param $arguments |
|
27
|
|
|
* @return void |
|
|
|
|
|
|
28
|
|
|
* @throws InvalidArgumentException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __call($name, $arguments) |
|
31
|
|
|
{ |
|
32
|
|
|
throw new InvalidArgumentException( |
|
33
|
|
|
sprintf('Unknown cache driver "%s" specified.', substr($name, 0, strlen($name) - 16)) |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function bind(MutableContainer $container) |
|
41
|
|
|
{ |
|
42
|
|
|
$container->bind(CacheContract::class, Cache::class); |
|
43
|
|
|
/** @var Config $config */ |
|
44
|
|
|
$config = $container->get(Config::class); |
|
45
|
|
|
|
|
46
|
|
|
$cacheDriver = $config->get('cache.driver'); |
|
47
|
|
|
if ($cacheDriver === null) { |
|
48
|
|
|
throw new RuntimeException('Undefined cache driver.'); |
|
49
|
|
|
} |
|
50
|
|
|
switch ($cacheDriver) { |
|
51
|
|
|
case 'array': |
|
52
|
|
|
case 'memory': |
|
53
|
|
|
$container->bind(CacheItemPoolInterface::class, ArrayCachePool::class); |
|
54
|
|
|
break; |
|
55
|
|
|
case 'void': |
|
56
|
|
|
case 'null': |
|
57
|
|
|
$container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Void\VoidCachePool'); |
|
58
|
|
|
break; |
|
59
|
|
|
case 'file': |
|
60
|
|
|
case 'files': |
|
61
|
|
|
case 'filesystem': |
|
62
|
|
|
$container |
|
63
|
|
|
->factory(CacheItemPoolInterface::class, $this->filesystemCachePoolFactory(), true); |
|
64
|
|
|
break; |
|
65
|
|
|
case 'redis': |
|
66
|
|
|
$container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Redis\RedisCachePool'); |
|
67
|
|
|
break; |
|
68
|
|
|
case 'predis': |
|
69
|
|
|
$container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Predis\PredisCachePool'); |
|
70
|
|
|
break; |
|
71
|
|
|
case 'memcached': |
|
72
|
|
|
$container->bind(CacheItemPoolInterface::class, 'Cache\Adapter\Memcached\MemcachedCachePool'); |
|
73
|
|
|
break; |
|
74
|
|
|
default: |
|
75
|
|
|
$container->bind(CacheItemPoolInterface::class, ([$this, $cacheDriver . 'CachePoolFactory'])()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return callable |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function filesystemCachePoolFactory(): callable |
|
83
|
|
|
{ |
|
84
|
|
|
return function (Filesystem $flysystem) { |
|
85
|
|
|
return new FilesystemCachePool($flysystem, 'storage/cache'); |
|
86
|
|
|
}; |
|
87
|
|
|
} |
|
88
|
|
|
} |
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.