1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PublishingKit\Cache\Factories; |
6
|
|
|
|
7
|
|
|
use Cache\Adapter\Doctrine\DoctrineCachePool; |
8
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
9
|
|
|
use Doctrine\Common\Cache\Cache; |
10
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
11
|
|
|
use Doctrine\Common\Cache\PhpFileCache; |
12
|
|
|
use Doctrine\Common\Cache\VoidCache; |
13
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
14
|
|
|
use PublishingKit\Cache\Contracts\Factories\CacheFactory; |
15
|
|
|
use PublishingKit\Cache\Exceptions\Factories\PathNotSet; |
16
|
|
|
|
17
|
|
|
final class DoctrineCacheFactory implements CacheFactory |
18
|
|
|
{ |
19
|
|
|
public function make(array $config): CacheItemPoolInterface |
20
|
|
|
{ |
21
|
|
|
return new DoctrineCachePool($this->createAdapter($config)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function createAdapter(array $config): Cache |
25
|
|
|
{ |
26
|
|
|
if (!isset($config['driver'])) { |
27
|
|
|
$config['driver'] = 'filesystem'; |
28
|
|
|
} |
29
|
|
|
switch ($config['driver']) { |
30
|
|
|
case 'array': |
31
|
|
|
$driver = $this->createArrayAdapter(); |
32
|
|
|
break; |
33
|
|
|
case 'php-files': |
34
|
|
|
$driver = $this->createPhpFilesAdapter($config); |
35
|
|
|
break; |
36
|
|
|
case 'void': |
37
|
|
|
$driver = $this->createVoidAdapter(); |
38
|
|
|
break; |
39
|
|
|
default: |
40
|
|
|
$driver = $this->createFilesystemAdapter($config); |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
return $driver; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function createFilesystemAdapter(array $config): FilesystemCache |
47
|
|
|
{ |
48
|
|
|
if (!isset($config['path'])) { |
49
|
|
|
throw new PathNotSet('Path must be set for the filesystem adapter'); |
50
|
|
|
} |
51
|
|
|
return new FilesystemCache($config['path']); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function createArrayAdapter(): ArrayCache |
55
|
|
|
{ |
56
|
|
|
return new ArrayCache(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function createPhpFilesAdapter(array $config): PhpFileCache |
60
|
|
|
{ |
61
|
|
|
if (!isset($config['path'])) { |
62
|
|
|
throw new PathNotSet('Path must be set for the PHP file adapter'); |
63
|
|
|
} |
64
|
|
|
return new PhpFileCache($config['path']); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function createVoidAdapter(): VoidCache |
68
|
|
|
{ |
69
|
|
|
return new VoidCache(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|