1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_cache\Files; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_cache\CacheException; |
7
|
|
|
use kalanis\kw_cache\Interfaces\ICache; |
8
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
9
|
|
|
use kalanis\kw_files\FilesException; |
10
|
|
|
use kalanis\kw_files\Traits\TToString; |
11
|
|
|
use kalanis\kw_paths\ArrayPath; |
12
|
|
|
use kalanis\kw_paths\PathsException; |
13
|
|
|
use kalanis\kw_paths\Stuff; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Basic |
18
|
|
|
* @package kalanis\kw_cache\Files |
19
|
|
|
* Caching content by files |
20
|
|
|
*/ |
21
|
|
|
class Basic implements ICache |
22
|
|
|
{ |
23
|
|
|
use TToString; |
24
|
|
|
|
25
|
|
|
/** @var CompositeAdapter */ |
26
|
|
|
protected $cacheStorage = null; |
27
|
|
|
/** @var string[] */ |
28
|
|
|
protected $cachePath = []; |
29
|
|
|
|
30
|
5 |
|
public function __construct(CompositeAdapter $cacheStorage) |
31
|
|
|
{ |
32
|
5 |
|
$this->cacheStorage = $cacheStorage; |
33
|
5 |
|
} |
34
|
|
|
|
35
|
5 |
|
public function init(string $what): void |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
5 |
|
$arr = new ArrayPath(); |
39
|
5 |
|
$arr->setString($what); |
40
|
5 |
|
$this->cachePath = array_merge( |
41
|
5 |
|
$arr->getArrayDirectory(), |
42
|
5 |
|
[$arr->getFileName() . ICache::EXT_CACHE] |
43
|
|
|
); |
44
|
|
|
// @codeCoverageIgnoreStart |
45
|
|
|
} catch (PathsException $ex) { |
46
|
|
|
// just when it has problems with separating the path |
47
|
|
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
48
|
|
|
} |
49
|
|
|
// @codeCoverageIgnoreEnd |
50
|
5 |
|
} |
51
|
|
|
|
52
|
3 |
|
public function exists(): bool |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
3 |
|
return $this->cacheStorage->exists($this->cachePath); |
56
|
1 |
|
} catch (FilesException | PathsException $ex) { |
57
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function set(string $content): bool |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
2 |
|
return $this->cacheStorage->saveFile($this->cachePath, $content); |
65
|
1 |
|
} catch (FilesException | PathsException $ex) { |
66
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function get(): string |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
2 |
|
return $this->exists() |
74
|
2 |
|
? $this->toString( |
75
|
2 |
|
Stuff::arrayToPath($this->cachePath), |
76
|
2 |
|
$this->cacheStorage->readFile($this->cachePath) |
77
|
|
|
) |
78
|
1 |
|
: ''; |
79
|
1 |
|
} catch (FilesException | PathsException $ex) { |
80
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
public function clear(): void |
85
|
|
|
{ |
86
|
|
|
try { |
87
|
2 |
|
$this->cacheStorage->deleteFile($this->cachePath); |
88
|
1 |
|
} catch (FilesException | PathsException $ex) { |
89
|
1 |
|
throw new CacheException($ex->getMessage(), $ex->getCode(), $ex); |
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
} |
93
|
|
|
|