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\Resource; |
13
|
|
|
|
14
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
15
|
|
|
use Puli\Repository\Api\Resource\ResourceMetadata; |
16
|
|
|
use Puli\Repository\Api\ResourceNotFoundException; |
17
|
|
|
use Puli\Repository\Api\ResourceRepository; |
18
|
|
|
use Puli\Repository\ChangeStream\ResourceStack; |
19
|
|
|
use Puli\Repository\Resource\Collection\ArrayResourceCollection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A generic resource. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class GenericResource implements PuliResource |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ResourceRepository |
32
|
|
|
*/ |
33
|
|
|
private $repo; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $path; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $repoPath; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a new resource. |
47
|
|
|
* |
48
|
|
|
* @param string|null $path The path of the resource. |
49
|
|
|
*/ |
50
|
754 |
|
public function __construct($path = null) |
51
|
|
|
{ |
52
|
754 |
|
$this->path = $path; |
53
|
754 |
|
$this->repoPath = $path; |
54
|
754 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
336 |
|
public function getPath() |
60
|
|
|
{ |
61
|
336 |
|
return $this->path; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
317 |
|
public function getName() |
68
|
|
|
{ |
69
|
317 |
|
return $this->path ? basename($this->path) : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
4 |
View Code Duplication |
public function getChild($relPath) |
76
|
|
|
{ |
77
|
4 |
|
if (!$this->getRepository()) { |
78
|
1 |
|
throw ResourceNotFoundException::forPath($this->getRepositoryPath().'/'.$relPath); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
return $this->getRepository()->get($this->getRepositoryPath().'/'.$relPath); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
3 |
|
public function hasChild($relPath) |
88
|
|
|
{ |
89
|
3 |
|
if (!$this->getRepository()) { |
90
|
1 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
return $this->getRepository()->contains($this->getRepositoryPath().'/'.$relPath); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
3 |
|
public function hasChildren() |
100
|
|
|
{ |
101
|
3 |
|
if (!$this->getRepository()) { |
102
|
1 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return $this->getRepository()->hasChildren($this->getRepositoryPath()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
6 |
View Code Duplication |
public function listChildren() |
112
|
|
|
{ |
113
|
6 |
|
$children = new ArrayResourceCollection(); |
114
|
|
|
|
115
|
6 |
|
if (!$this->getRepository()) { |
116
|
1 |
|
return $children; |
117
|
|
|
} |
118
|
|
|
|
119
|
5 |
|
foreach ($this->getRepository()->listChildren($this->getRepositoryPath()) as $child) { |
120
|
2 |
|
$children[$child->getName()] = $child; |
121
|
5 |
|
} |
122
|
|
|
|
123
|
5 |
|
return $children; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getStack() |
130
|
|
|
{ |
131
|
|
|
if (!$this->getRepository()) { |
132
|
|
|
return new ResourceStack(array($this)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->getRepository()->getStack($this->getRepositoryPath()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function getMetadata() |
142
|
|
|
{ |
143
|
|
|
return new ResourceMetadata(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
621 |
|
public function attachTo(ResourceRepository $repo, $path = null) |
150
|
|
|
{ |
151
|
621 |
|
$this->repo = $repo; |
152
|
|
|
|
153
|
621 |
|
if (null !== $path) { |
154
|
549 |
|
$this->path = $path; |
155
|
549 |
|
$this->repoPath = $path; |
156
|
549 |
|
} |
157
|
621 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
25 |
|
public function detach() |
163
|
|
|
{ |
164
|
25 |
|
$this->repo = null; |
165
|
25 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritdoc} |
169
|
|
|
*/ |
170
|
264 |
|
public function getRepository() |
171
|
|
|
{ |
172
|
264 |
|
return $this->repo; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
142 |
|
public function getRepositoryPath() |
179
|
|
|
{ |
180
|
142 |
|
return $this->repoPath; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
581 |
|
public function isAttached() |
187
|
|
|
{ |
188
|
581 |
|
return null !== $this->repo; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
56 |
|
public function createReference($path) |
195
|
|
|
{ |
196
|
56 |
|
$ref = clone $this; |
197
|
56 |
|
$ref->path = $path; |
198
|
|
|
|
199
|
56 |
|
return $ref; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
84 |
|
public function isReference() |
206
|
|
|
{ |
207
|
84 |
|
return $this->path !== $this->repoPath; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
27 |
|
public function serialize() |
214
|
|
|
{ |
215
|
27 |
|
$data = array(); |
216
|
|
|
|
217
|
27 |
|
$this->preSerialize($data); |
218
|
|
|
|
219
|
27 |
|
return serialize($data); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
27 |
|
public function unserialize($string) |
226
|
|
|
{ |
227
|
27 |
|
$data = unserialize($string); |
228
|
|
|
|
229
|
27 |
|
$this->postUnserialize($data); |
230
|
27 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Invoked before serializing a resource. |
234
|
|
|
* |
235
|
|
|
* Override this method if you want to serialize custom data in subclasses. |
236
|
|
|
* |
237
|
|
|
* @param array $data The data to serialize. Add custom data at the end of |
238
|
|
|
* the array. |
239
|
|
|
*/ |
240
|
27 |
|
protected function preSerialize(array &$data) |
241
|
|
|
{ |
242
|
27 |
|
$data[] = $this->path; |
243
|
27 |
|
$data[] = $this->repoPath; |
244
|
27 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Invoked after unserializing a resource. |
248
|
|
|
* |
249
|
|
|
* Override this method if you want to unserialize custom data in |
250
|
|
|
* subclasses. |
251
|
|
|
* |
252
|
|
|
* @param array $data The unserialized data. Pop your custom data from the |
253
|
|
|
* end of the array before calling the parent method. |
254
|
|
|
*/ |
255
|
27 |
|
protected function postUnserialize(array $data) |
256
|
|
|
{ |
257
|
27 |
|
$this->repoPath = array_pop($data); |
258
|
27 |
|
$this->path = array_pop($data); |
259
|
27 |
|
} |
260
|
|
|
} |
261
|
|
|
|