1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema; |
4
|
|
|
|
5
|
|
|
use PhpLang\ScopeExit; |
6
|
|
|
use Swaggest\JsonDiff\JsonPointer; |
7
|
|
|
use Swaggest\JsonSchema\Constraint\Ref; |
8
|
|
|
use Swaggest\JsonSchema\RemoteRef\BasicFetcher; |
9
|
|
|
|
10
|
|
|
class RefResolver |
11
|
|
|
{ |
12
|
|
|
public $resolutionScope = ''; |
13
|
|
|
public $url; |
14
|
|
|
/** @var RefResolver */ |
15
|
|
|
private $rootResolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $resolutionScope |
19
|
|
|
* @return string previous value |
20
|
|
|
*/ |
21
|
598 |
|
public function setResolutionScope($resolutionScope) |
22
|
|
|
{ |
23
|
598 |
|
$rootResolver = $this->rootResolver ? $this->rootResolver : $this; |
24
|
598 |
|
if ($resolutionScope === $rootResolver->resolutionScope) { |
25
|
598 |
|
return $resolutionScope; |
26
|
|
|
} |
27
|
461 |
|
$prev = $rootResolver->resolutionScope; |
28
|
461 |
|
$rootResolver->resolutionScope = $resolutionScope; |
29
|
461 |
|
return $prev; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
597 |
|
public function getResolutionScope() |
36
|
|
|
{ |
37
|
597 |
|
$rootResolver = $this->rootResolver ? $this->rootResolver : $this; |
38
|
597 |
|
return $rootResolver->resolutionScope; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $id |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
416 |
|
public function updateResolutionScope($id) |
47
|
|
|
{ |
48
|
416 |
|
$id = rtrim($id, '#'); // safe to trim because # in hashCode must be urlencoded to %23 |
49
|
416 |
|
$rootResolver = $this->rootResolver ? $this->rootResolver : $this; |
50
|
416 |
|
if (strpos($id, '://') !== false) { |
51
|
416 |
|
$prev = $rootResolver->setResolutionScope($id); |
52
|
|
|
} else { |
53
|
364 |
|
$prev = $rootResolver->setResolutionScope(Helper::resolveURI($rootResolver->resolutionScope, $id)); |
54
|
|
|
} |
55
|
|
|
|
56
|
416 |
|
return $prev; |
57
|
|
|
} |
58
|
|
|
|
59
|
407 |
|
public function setupResolutionScope($id, $data) |
60
|
|
|
{ |
61
|
407 |
|
$rootResolver = $this->rootResolver ? $this->rootResolver : $this; |
62
|
|
|
|
63
|
407 |
|
$prev = $rootResolver->updateResolutionScope($id); |
64
|
|
|
|
65
|
407 |
|
$refParts = explode('#', $rootResolver->resolutionScope, 2); |
66
|
|
|
|
67
|
407 |
|
if ($refParts[0]) { // external uri |
68
|
407 |
|
$resolver = &$rootResolver->remoteRefResolvers[$refParts[0]]; |
69
|
407 |
|
if ($resolver === null) { |
70
|
407 |
|
$resolver = new RefResolver(); |
71
|
407 |
|
$resolver->rootResolver = $rootResolver; |
72
|
407 |
|
$resolver->url = $refParts[0]; |
73
|
407 |
|
$this->remoteRefResolvers[$refParts[0]] = $resolver; |
74
|
|
|
} |
75
|
|
|
} else { // local uri |
76
|
6 |
|
$resolver = $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
407 |
|
if (empty($refParts[1])) { |
80
|
407 |
|
$resolver->rootData = $data; |
81
|
|
|
} else { |
82
|
12 |
|
$refPath = '#' . $refParts[1]; |
83
|
12 |
|
$resolver->refs[$refPath] = new Ref($refPath, $data); |
84
|
|
|
} |
85
|
|
|
|
86
|
407 |
|
return $prev; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private $rootData; |
90
|
|
|
|
91
|
|
|
/** @var Ref[] */ |
92
|
|
|
private $refs = array(); |
93
|
|
|
|
94
|
|
|
/** @var RefResolver[]|null[] */ |
95
|
|
|
private $remoteRefResolvers = array(); |
96
|
|
|
|
97
|
|
|
/** @var RemoteRefProvider */ |
98
|
|
|
private $refProvider; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* RefResolver constructor. |
102
|
|
|
* @param JsonSchema $rootData |
103
|
|
|
*/ |
104
|
3208 |
|
public function __construct($rootData = null) |
105
|
|
|
{ |
106
|
3208 |
|
$this->rootData = $rootData; |
107
|
3208 |
|
} |
108
|
|
|
|
109
|
1732 |
|
public function setRootData($rootData) |
110
|
|
|
{ |
111
|
1732 |
|
$this->rootData = $rootData; |
112
|
1732 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
3138 |
|
public function setRemoteRefProvider(RemoteRefProvider $provider) |
117
|
|
|
{ |
118
|
3138 |
|
$this->refProvider = $provider; |
119
|
3138 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
126 |
|
private function getRefProvider() |
123
|
|
|
{ |
124
|
126 |
|
if (null === $this->refProvider) { |
125
|
4 |
|
$this->refProvider = new BasicFetcher(); |
126
|
|
|
} |
127
|
126 |
|
return $this->refProvider; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $referencePath |
132
|
|
|
* @return Ref |
133
|
|
|
* @throws InvalidValue |
134
|
|
|
*/ |
135
|
409 |
|
public function resolveReference($referencePath) |
136
|
|
|
{ |
137
|
409 |
|
if ($this->resolutionScope) { |
138
|
186 |
|
$referencePath = Helper::resolveURI($this->resolutionScope, $referencePath); |
139
|
|
|
} |
140
|
|
|
|
141
|
409 |
|
$refParts = explode('#', $referencePath, 2); |
142
|
409 |
|
$url = rtrim($refParts[0], '#'); |
143
|
409 |
|
$refLocalPath = isset($refParts[1]) ? '#' . $refParts[1] : '#'; |
144
|
|
|
|
145
|
409 |
|
if ($url === $this->url) { |
146
|
|
|
$referencePath = $refLocalPath; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** @var null|Ref $ref */ |
150
|
409 |
|
$ref = &$this->refs[$referencePath]; |
151
|
|
|
|
152
|
409 |
|
$refResolver = $this; |
153
|
|
|
|
154
|
409 |
|
if (null === $ref) { |
155
|
403 |
|
if ($referencePath[0] === '#') { |
156
|
397 |
|
if ($referencePath === '#') { |
157
|
156 |
|
$ref = new Ref($referencePath, $refResolver->rootData); |
158
|
|
|
} else { |
159
|
316 |
|
$ref = new Ref($referencePath); |
160
|
|
|
try { |
161
|
316 |
|
$path = JsonPointer::splitPath($referencePath); |
162
|
|
|
} catch (\Swaggest\JsonDiff\Exception $e) { |
163
|
|
|
throw new InvalidValue('Invalid reference: ' . $referencePath . ', ' . $e->getMessage()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** @var JsonSchema $branch */ |
167
|
316 |
|
$branch = &$refResolver->rootData; |
168
|
316 |
|
while (!empty($path)) { |
169
|
316 |
|
if (isset($branch->{Schema::PROP_ID_D4}) && is_string($branch->{Schema::PROP_ID_D4})) { |
170
|
27 |
|
$refResolver->updateResolutionScope($branch->{Schema::PROP_ID_D4}); |
171
|
|
|
} |
172
|
316 |
|
if (isset($branch->{Schema::PROP_ID}) && is_string($branch->{Schema::PROP_ID})) { |
173
|
75 |
|
$refResolver->updateResolutionScope($branch->{Schema::PROP_ID}); |
174
|
|
|
} |
175
|
|
|
|
176
|
316 |
|
$folder = array_shift($path); |
177
|
|
|
|
178
|
316 |
|
if ($branch instanceof \stdClass && isset($branch->$folder)) { |
179
|
316 |
|
$branch = &$branch->$folder; |
180
|
12 |
|
} elseif (is_array($branch) && isset($branch[$folder])) { |
181
|
12 |
|
$branch = &$branch[$folder]; |
182
|
|
|
} else { |
183
|
|
|
throw new InvalidValue('Could not resolve ' . $referencePath . '@' . $this->getResolutionScope() . ': ' . $folder); |
184
|
|
|
} |
185
|
|
|
} |
186
|
397 |
|
$ref->setData($branch); |
187
|
|
|
} |
188
|
|
|
} else { |
189
|
224 |
|
if ($url !== $this->url) { |
190
|
224 |
|
$rootResolver = $this->rootResolver ? $this->rootResolver : $this; |
191
|
|
|
/** @var null|RefResolver $refResolver */ |
192
|
224 |
|
$refResolver = &$rootResolver->remoteRefResolvers[$url]; |
193
|
224 |
|
$this->setResolutionScope($url); |
194
|
224 |
|
if (null === $refResolver) { |
195
|
126 |
|
$rootData = $rootResolver->getRefProvider()->getSchemaData($url); |
196
|
126 |
|
$refResolver = new RefResolver($rootData); |
197
|
126 |
|
$refResolver->rootResolver = $rootResolver; |
198
|
126 |
|
$refResolver->refProvider = $this->refProvider; |
199
|
126 |
|
$refResolver->url = $url; |
200
|
126 |
|
$rootResolver->setResolutionScope($url); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
224 |
|
$ref = $refResolver->resolveReference($refLocalPath); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
409 |
|
return $ref; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param mixed $data |
214
|
|
|
* @param Context $options |
215
|
|
|
* @param int $nestingLevel |
216
|
|
|
* @throws Exception |
217
|
|
|
*/ |
218
|
3207 |
|
public function preProcessReferences($data, Context $options, $nestingLevel = 0) |
219
|
|
|
{ |
220
|
3207 |
|
if ($nestingLevel > 200) { |
221
|
|
|
throw new Exception('Too deep nesting level', Exception::DEEP_NESTING); |
222
|
|
|
} |
223
|
3207 |
|
if (is_array($data)) { |
224
|
1347 |
|
foreach ($data as $key => $item) { |
225
|
1347 |
|
$this->preProcessReferences($item, $options, $nestingLevel + 1); |
226
|
|
|
} |
227
|
3205 |
|
} elseif ($data instanceof \stdClass) { |
228
|
|
|
/** @var JsonSchema $data */ |
229
|
|
|
if ( |
230
|
3189 |
|
isset($data->{Schema::PROP_ID_D4}) |
231
|
3189 |
|
&& is_string($data->{Schema::PROP_ID_D4}) |
232
|
3189 |
|
&& (($options->version === Schema::VERSION_AUTO) || $options->version === Schema::VERSION_DRAFT_04) |
233
|
|
|
) { |
234
|
361 |
|
$prev = $this->setupResolutionScope($data->{Schema::PROP_ID_D4}, $data); |
235
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
236
|
|
|
$_ = new ScopeExit(function () use ($prev) { |
|
|
|
|
237
|
361 |
|
$this->setResolutionScope($prev); |
238
|
361 |
|
}); |
239
|
|
|
} |
240
|
|
|
|
241
|
3189 |
|
if (isset($data->{Schema::PROP_ID}) |
242
|
3189 |
|
&& is_string($data->{Schema::PROP_ID}) |
243
|
3189 |
|
&& (($options->version === Schema::VERSION_AUTO) || $options->version >= Schema::VERSION_DRAFT_06) |
244
|
|
|
) { |
245
|
365 |
|
$prev = $this->setupResolutionScope($data->{Schema::PROP_ID}, $data); |
246
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
247
|
365 |
|
$_ = new ScopeExit(function () use ($prev) { |
248
|
365 |
|
$this->setResolutionScope($prev); |
249
|
365 |
|
}); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
3189 |
|
foreach ((array)$data as $key => $value) { |
254
|
3187 |
|
$this->preProcessReferences($value, $options, $nestingLevel + 1); |
255
|
|
|
} |
256
|
|
|
} |
257
|
3207 |
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
} |