|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/manager package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Manager\Cache; |
|
13
|
|
|
|
|
14
|
|
|
use Puli\Manager\Api\Cache\CacheFile; |
|
15
|
|
|
use Puli\Manager\Api\Cache\CacheFileSerializer; |
|
16
|
|
|
use Puli\Manager\Api\FileNotFoundException; |
|
17
|
|
|
use Puli\Manager\Api\InvalidConfigException; |
|
18
|
|
|
use Puli\Manager\Api\Storage\ReadException; |
|
19
|
|
|
use Puli\Manager\Api\Storage\Storage; |
|
20
|
|
|
use Puli\Manager\Api\Storage\WriteException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Loads and saves cache files. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @author Mateusz Sojda <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class CacheFileStorage |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Storage |
|
33
|
|
|
*/ |
|
34
|
|
|
private $storage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var CacheFileSerializer |
|
38
|
|
|
*/ |
|
39
|
|
|
private $serializer; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a new storage. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Storage $storage The file storage. |
|
45
|
|
|
* @param CacheFileSerializer $serializer The cache file serializer. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(Storage $storage, CacheFileSerializer $serializer) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->storage = $storage; |
|
50
|
|
|
$this->serializer = $serializer; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Loads a cache file from a file path. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $path The path to the cache file. |
|
57
|
|
|
* |
|
58
|
|
|
* @return CacheFile The loaded cache file. |
|
59
|
|
|
* |
|
60
|
|
|
* @throws FileNotFoundException If the file does not exist. |
|
61
|
|
|
* @throws ReadException If the file cannot be read. |
|
62
|
|
|
* @throws InvalidConfigException If the file contains invalid |
|
63
|
|
|
* configuration. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function loadCacheFile($path) |
|
66
|
|
|
{ |
|
67
|
|
|
$serialized = $this->storage->read($path); |
|
68
|
|
|
|
|
69
|
|
|
return $this->serializer->unserializeCacheFile($serialized, $path); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Saves a cache file. |
|
74
|
|
|
* |
|
75
|
|
|
* The cache file is saved to the same path that it was read from. |
|
76
|
|
|
* |
|
77
|
|
|
* @param CacheFile $cacheFile The cache file to save. |
|
78
|
|
|
* |
|
79
|
|
|
* @throws WriteException If the file cannot be written. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function saveCacheFile(CacheFile $cacheFile) |
|
82
|
|
|
{ |
|
83
|
|
|
$serialized = $this->serializer->serializeCacheFile($cacheFile); |
|
84
|
|
|
|
|
85
|
|
|
$this->storage->write($cacheFile->getPath(), $serialized); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns whether a cache file exists. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $path The cache file path. |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool Returns `true` if the cache file exists and `false` otherwise. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function cacheFileExists($path) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->storage->exists($path); |
|
98
|
|
|
} |
|
99
|
|
|
} |