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