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\FileNotFoundException; |
16
|
|
|
use Puli\Manager\Api\InvalidConfigException; |
17
|
|
|
use Puli\Manager\Api\Storage\ReadException; |
18
|
|
|
use Puli\Manager\Api\Storage\Storage; |
19
|
|
|
use Puli\Manager\Api\Storage\WriteException; |
20
|
|
|
use stdClass; |
21
|
|
|
use Webmozart\Json\Conversion\ConversionFailedException; |
22
|
|
|
use Webmozart\Json\Conversion\JsonConverter; |
23
|
|
|
use Webmozart\Json\DecodingFailedException; |
24
|
|
|
use Webmozart\Json\EncodingFailedException; |
25
|
|
|
use Webmozart\Json\JsonDecoder; |
26
|
|
|
use Webmozart\Json\JsonEncoder; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Loads and saves cache files. |
30
|
|
|
* |
31
|
|
|
* @since 1.0 |
32
|
|
|
* |
33
|
|
|
* @author Mateusz Sojda <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class CacheFileStorage |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Storage |
39
|
|
|
*/ |
40
|
|
|
private $storage; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var JsonConverter |
44
|
|
|
*/ |
45
|
|
|
private $converter; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates a new storage. |
49
|
|
|
* |
50
|
|
|
* @param Storage $storage The file storage. |
51
|
|
|
* @param JsonConverter $converter The cache file converter. |
52
|
|
|
* @param JsonEncoder $jsonEncoder The JSON encoder. |
53
|
|
|
* @param JsonDecoder $jsonDecoder The JSON decoder. |
54
|
|
|
*/ |
55
|
1 |
|
public function __construct( |
56
|
|
|
Storage $storage, |
57
|
|
|
JsonConverter $converter, |
58
|
|
|
JsonEncoder $jsonEncoder, |
59
|
|
|
JsonDecoder $jsonDecoder |
60
|
|
|
) |
61
|
|
|
{ |
62
|
1 |
|
$this->storage = $storage; |
63
|
1 |
|
$this->converter = $converter; |
64
|
1 |
|
$this->jsonEncoder = $jsonEncoder; |
|
|
|
|
65
|
1 |
|
$this->jsonDecoder = $jsonDecoder; |
|
|
|
|
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Loads a cache file from a file path. |
70
|
|
|
* |
71
|
|
|
* @param string $path The path to the cache file. |
72
|
|
|
* |
73
|
|
|
* @return CacheFile The loaded cache file. |
74
|
|
|
* |
75
|
|
|
* @throws FileNotFoundException If the file does not exist. |
76
|
|
|
* @throws ReadException If the file cannot be read. |
77
|
|
|
* @throws InvalidConfigException If the file contains invalid |
78
|
|
|
* configuration. |
79
|
|
|
*/ |
80
|
|
|
public function loadCacheFile($path) |
81
|
|
|
{ |
82
|
|
|
$serialized = $this->storage->read($path); |
83
|
|
|
$jsonData = $this->decode($serialized, $path); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
return $this->converter->fromJson($jsonData, array( |
87
|
|
|
'path' => $path |
88
|
|
|
)); |
89
|
|
|
} catch (ConversionFailedException $e) { |
90
|
|
|
throw new InvalidConfigException(sprintf( |
91
|
|
|
'The JSON in %s could not be converted: %s', |
92
|
|
|
$path, |
93
|
|
|
$e->getMessage() |
94
|
|
|
), 0, $e); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Saves a cache file. |
100
|
|
|
* |
101
|
|
|
* The cache file is saved to the same path that it was read from. |
102
|
|
|
* |
103
|
|
|
* @param CacheFile $cacheFile The cache file to save. |
104
|
|
|
* |
105
|
|
|
* @throws WriteException If the file cannot be written. |
106
|
|
|
*/ |
107
|
|
|
public function saveCacheFile(CacheFile $cacheFile) |
108
|
|
|
{ |
109
|
|
|
$jsonData = $this->converter->toJson($cacheFile); |
110
|
|
|
$serialized = $this->encode($jsonData, $cacheFile->getPath()); |
111
|
|
|
|
112
|
|
|
$this->storage->write($cacheFile->getPath(), $serialized); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns whether a cache file exists. |
117
|
|
|
* |
118
|
|
|
* @param string $path The cache file path. |
119
|
|
|
* |
120
|
|
|
* @return bool Returns `true` if the cache file exists and `false` otherwise. |
121
|
|
|
*/ |
122
|
|
|
public function fileExists($path) |
123
|
|
|
{ |
124
|
|
|
return $this->storage->exists($path); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
private function encode(stdClass $jsonData, $path) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
return $this->jsonEncoder->encode($jsonData); |
131
|
|
|
} catch (EncodingFailedException $e) { |
132
|
|
|
throw new InvalidConfigException(sprintf( |
133
|
|
|
'The configuration in %s could not be encoded: %s', |
134
|
|
|
$path, |
135
|
|
|
$e->getMessage() |
136
|
|
|
), 0, $e); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
private function decode($json, $path) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
try { |
143
|
|
|
return $this->jsonDecoder->decode($json); |
144
|
|
|
} catch (DecodingFailedException $e) { |
145
|
|
|
throw new InvalidConfigException(sprintf( |
146
|
|
|
'The configuration in %s could not be decoded: %s', |
147
|
|
|
$path, |
148
|
|
|
$e->getMessage() |
149
|
|
|
), 0, $e); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: