1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/repository 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\Repository\ChangeStream; |
13
|
|
|
|
14
|
|
|
use Puli\Repository\Api\ChangeStream\ChangeStream; |
15
|
|
|
use Puli\Repository\Api\ChangeStream\VersionList; |
16
|
|
|
use Puli\Repository\Api\NoVersionFoundException; |
17
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
18
|
|
|
use Puli\Repository\Api\ResourceRepository; |
19
|
|
|
use Webmozart\Json\JsonDecoder; |
20
|
|
|
use Webmozart\Json\JsonEncoder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A change stream backed by a JSON file. |
24
|
|
|
* |
25
|
|
|
* @since 1.0 |
26
|
|
|
* |
27
|
|
|
* @author Bernhard Schussek <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class JsonChangeStream implements ChangeStream |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $path; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $json; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var JsonEncoder |
43
|
|
|
*/ |
44
|
|
|
private $encoder; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $path The path to the JSON file. |
48
|
|
|
*/ |
49
|
20 |
|
public function __construct($path) |
50
|
|
|
{ |
51
|
20 |
|
$this->path = $path; |
52
|
20 |
|
$this->encoder = new JsonEncoder(); |
53
|
20 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
18 |
|
public function append(PuliResource $resource) |
59
|
|
|
{ |
60
|
18 |
|
if (null === $this->json) { |
61
|
17 |
|
$this->load(); |
62
|
17 |
|
} |
63
|
|
|
|
64
|
18 |
|
if (!isset($this->json[$resource->getPath()])) { |
65
|
18 |
|
$this->json[$resource->getPath()] = array(); |
66
|
18 |
|
} |
67
|
|
|
|
68
|
18 |
|
$this->json[$resource->getPath()][] = serialize($resource); |
69
|
|
|
|
70
|
18 |
|
$this->flush(); |
71
|
18 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
6 |
|
public function purge($path) |
77
|
|
|
{ |
78
|
6 |
|
if (null === $this->json) { |
79
|
|
|
$this->load(); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
unset($this->json[$path]); |
83
|
|
|
|
84
|
6 |
|
$this->flush(); |
85
|
6 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
|
public function clear() |
91
|
|
|
{ |
92
|
2 |
|
if (null === $this->json) { |
93
|
|
|
$this->load(); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$this->json = array(); |
97
|
|
|
|
98
|
2 |
|
$this->flush(); |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
9 |
|
public function contains($path) |
105
|
|
|
{ |
106
|
8 |
|
if (null === $this->json) { |
107
|
5 |
|
$this->load(); |
108
|
5 |
|
} |
109
|
|
|
|
110
|
9 |
|
return isset($this->json[$path]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
14 |
|
public function getVersions($path, ResourceRepository $repository = null) |
117
|
|
|
{ |
118
|
14 |
|
if (null === $this->json) { |
119
|
7 |
|
$this->load(); |
120
|
7 |
|
} |
121
|
|
|
|
122
|
14 |
|
if (!isset($this->json[$path])) { |
123
|
4 |
|
throw NoVersionFoundException::forPath($path); |
124
|
|
|
} |
125
|
|
|
|
126
|
10 |
|
$versions = array(); |
127
|
|
|
|
128
|
10 |
View Code Duplication |
foreach ($this->json[$path] as $resource) { |
|
|
|
|
129
|
10 |
|
$resource = unserialize($resource); |
130
|
|
|
|
131
|
10 |
|
if (null !== $repository) { |
132
|
4 |
|
$resource->attachTo($repository, $path); |
133
|
4 |
|
} |
134
|
|
|
|
135
|
10 |
|
$versions[] = $resource; |
136
|
10 |
|
} |
137
|
|
|
|
138
|
10 |
|
return new VersionList($path, $versions); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Loads the JSON file. |
143
|
|
|
*/ |
144
|
20 |
|
private function load() |
145
|
|
|
{ |
146
|
20 |
|
$decoder = new JsonDecoder(); |
147
|
20 |
|
$decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY); |
148
|
|
|
|
149
|
20 |
|
$this->json = file_exists($this->path) |
|
|
|
|
150
|
20 |
|
? $decoder->decodeFile($this->path) |
151
|
20 |
|
: array(); |
152
|
20 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Writes the JSON file. |
156
|
|
|
*/ |
157
|
18 |
|
private function flush() |
158
|
|
|
{ |
159
|
18 |
|
$this->encoder->encodeFile($this->json, $this->path); |
160
|
18 |
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.