1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PublishingKit\Cache\Factories; |
6
|
|
|
|
7
|
|
|
use Stash\Pool; |
8
|
|
|
use Stash\Driver\Apc; |
9
|
|
|
use Stash\Driver\BlackHole; |
10
|
|
|
use Stash\Driver\Composite; |
11
|
|
|
use Stash\Driver\Ephemeral; |
12
|
|
|
use Stash\Driver\FileSystem; |
13
|
|
|
use Stash\Driver\Memcache; |
14
|
|
|
use Stash\Driver\Redis; |
15
|
|
|
use Stash\Driver\Sqlite; |
16
|
|
|
use Stash\Interfaces\DriverInterface; |
17
|
|
|
use PublishingKit\Cache\Contracts\Factories\CacheFactory; |
18
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
19
|
|
|
use PublishingKit\Cache\Exceptions\Factories\PathNotSet; |
20
|
|
|
|
21
|
|
|
final class StashCacheFactory implements CacheFactory |
22
|
|
|
{ |
23
|
27 |
|
public function make(array $config): CacheItemPoolInterface |
24
|
|
|
{ |
25
|
27 |
|
$driver = $this->createAdapter($config); |
26
|
24 |
|
return new Pool($driver); |
27
|
|
|
} |
28
|
|
|
|
29
|
27 |
|
private function createAdapter(array $config): DriverInterface |
30
|
|
|
{ |
31
|
27 |
|
if (!isset($config['driver'])) { |
32
|
3 |
|
$config['driver'] = 'filesystem'; |
33
|
|
|
} |
34
|
27 |
|
switch ($config['driver']) { |
35
|
27 |
|
case 'test': |
36
|
3 |
|
$driver = $this->createBlackHoleAdapter(); |
37
|
3 |
|
break; |
38
|
24 |
|
case 'ephemeral': |
39
|
6 |
|
$driver = $this->createEphemeralAdapter(); |
40
|
6 |
|
break; |
41
|
21 |
|
case 'composite': |
42
|
3 |
|
$driver = $this->createCompositeAdapter($config); |
43
|
3 |
|
break; |
44
|
21 |
|
case 'sqlite': |
45
|
3 |
|
$driver = $this->createSqliteAdapter($config); |
46
|
3 |
|
break; |
47
|
18 |
|
case 'apc': |
48
|
3 |
|
$driver = $this->createApcAdapter($config); |
49
|
3 |
|
break; |
50
|
15 |
|
case 'memcache': |
51
|
|
|
$driver = $this->createMemcacheAdapter($config); |
52
|
|
|
break; |
53
|
15 |
|
case 'redis': |
54
|
3 |
|
$driver = $this->createRedisAdapter($config); |
55
|
3 |
|
break; |
56
|
|
|
default: |
57
|
12 |
|
$driver = $this->createFilesystemAdapter($config); |
58
|
9 |
|
break; |
59
|
|
|
} |
60
|
24 |
|
return $driver; |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
private function createFilesystemAdapter(array $config): FileSystem |
64
|
|
|
{ |
65
|
12 |
|
if (!isset($config['path'])) { |
66
|
3 |
|
throw new PathNotSet('Path must be set for the filesystem adapter'); |
67
|
|
|
} |
68
|
|
|
$adapterConfig = [ |
69
|
9 |
|
'path' => $config['path'], |
70
|
|
|
]; |
71
|
9 |
|
if (isset($config['dirSplit'])) { |
72
|
3 |
|
$adapterConfig['dirSplit'] = $config['dirSplit']; |
73
|
|
|
} |
74
|
9 |
|
if (isset($config['filePermissions'])) { |
75
|
3 |
|
$adapterConfig['filePermissions'] = $config['filePermissions']; |
76
|
|
|
} |
77
|
9 |
|
if (isset($config['dirPermissions'])) { |
78
|
3 |
|
$adapterConfig['dirPermissions'] = $config['dirPermissions']; |
79
|
|
|
} |
80
|
9 |
|
return new FileSystem($adapterConfig); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
private function createBlackHoleAdapter(): BlackHole |
84
|
|
|
{ |
85
|
3 |
|
return new BlackHole(); |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
private function createEphemeralAdapter(): Ephemeral |
89
|
|
|
{ |
90
|
6 |
|
return new Ephemeral(); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
private function createCompositeAdapter(array $config): Composite |
94
|
|
|
{ |
95
|
3 |
|
$drivers = []; |
96
|
3 |
|
foreach ($config['subdrivers'] as $driver) { |
97
|
3 |
|
$drivers[] = $this->createAdapter($driver); |
98
|
|
|
} |
99
|
3 |
|
return new Composite([ |
100
|
3 |
|
'drivers' => $drivers |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
private function createSqliteAdapter(array $config): Sqlite |
105
|
|
|
{ |
106
|
3 |
|
return new Sqlite([ |
107
|
3 |
|
'extension' => isset($config['extension']) ? $config['extension'] : null, |
108
|
3 |
|
'version' => isset($config['version']) ? $config['version'] : null, |
109
|
3 |
|
'nesting' => isset($config['nesting']) ? $config['nesting'] : null, |
110
|
3 |
|
'path' => isset($config['path']) ? $config['path'] : null, |
111
|
3 |
|
'filePermissions' => isset($config['filePermissions']) ? $config['filePermissions'] : null, |
112
|
3 |
|
'dirPermissions' => isset($config['dirPermissions']) ? $config['dirPermissions'] : null |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
private function createApcAdapter(array $config): Apc |
117
|
|
|
{ |
118
|
3 |
|
return new Apc([ |
119
|
3 |
|
'ttl' => isset($config['ttl']) ? $config['ttl'] : null, |
120
|
3 |
|
'namespace' => isset($config['namespace']) ? $config['namespace'] : null |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function createMemcacheAdapter(array $config): Memcache |
125
|
|
|
{ |
126
|
|
|
return new Memcache([ |
127
|
|
|
'servers' => isset($config['servers']) ? $config['servers'] : null, |
128
|
|
|
'extension' => 'memcached', |
129
|
|
|
'prefix_key' => isset($config['prefix_key']) ? $config['prefix_key'] : null, |
130
|
|
|
'libketama_compatible' => isset($config['libketama_compatible']) ? $config['libketama_compatible'] : null, |
131
|
|
|
'cache_lookups' => isset($config['cache_lookups']) ? $config['cache_lookups'] : null, |
132
|
|
|
'serializer' => isset($config['serializer']) ? $config['serializer'] : null, |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
private function createRedisAdapter(array $config): Redis |
137
|
|
|
{ |
138
|
3 |
|
return new Redis([ |
139
|
3 |
|
'servers' => isset($config['servers']) ? $config['servers'] : null, |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|