1 | <?php |
||
13 | class Dereferencer |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $loaders; |
||
19 | |||
20 | /** |
||
21 | * Create a new Dereferencer. |
||
22 | */ |
||
23 | 168 | public function __construct() |
|
28 | |||
29 | /** |
||
30 | * Return the schema with all references resolved. |
||
31 | * |
||
32 | * @param string|object $schema Either a valid path like "http://json-schema.org/draft-03/schema#" |
||
33 | * or the object resulting from a json_decode call. |
||
34 | * |
||
35 | * @return object |
||
36 | */ |
||
37 | 166 | public function dereference($schema) |
|
38 | { |
||
39 | 166 | if (is_string($schema)) { |
|
40 | 30 | $uri = $schema; |
|
41 | 30 | $schema = $this->loadExternalRef($uri); |
|
42 | 28 | $schema = $this->resolveFragment($uri, $schema); |
|
43 | |||
44 | 28 | return $this->crawl($schema, strip_fragment($uri)); |
|
45 | } |
||
46 | |||
47 | 136 | return $this->crawl($schema); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Register a Loader for the given prefix. |
||
52 | * |
||
53 | * @param Loader $loader |
||
54 | * @param string $prefix |
||
55 | */ |
||
56 | 122 | public function registerLoader(Loader $loader, $prefix) |
|
60 | |||
61 | /** |
||
62 | * Get all registered loaders, keyed by the prefix they are registered to load schemas for. |
||
63 | * |
||
64 | * @return Loader[] |
||
65 | */ |
||
66 | 2 | public function getLoaders() |
|
70 | |||
71 | /** |
||
72 | * Get the loader for the given prefix. |
||
73 | * |
||
74 | * @param string $prefix |
||
75 | * |
||
76 | * @return Loader |
||
77 | * @throws \InvalidArgumentException |
||
78 | */ |
||
79 | 46 | public function getLoader($prefix) |
|
80 | { |
||
81 | 46 | if (!array_key_exists($prefix, $this->loaders)) { |
|
82 | 2 | throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix)); |
|
83 | } |
||
84 | |||
85 | 44 | return $this->loaders[$prefix]; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Register the default file loader. |
||
90 | */ |
||
91 | 168 | private function registerFileLoader() |
|
95 | |||
96 | /** |
||
97 | * Register the default web loaders. If the curl extension is loaded, |
||
98 | * the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
||
99 | * will be used. You can override this by registering your own loader |
||
100 | * for the 'http' and 'https' protocols. |
||
101 | */ |
||
102 | 168 | private function registerDefaultWebLoaders() |
|
112 | |||
113 | /** |
||
114 | * Crawl the schema and resolve any references. |
||
115 | * |
||
116 | * @param object $schema |
||
117 | * @param string|null $currentUri |
||
118 | * |
||
119 | * @return object |
||
120 | */ |
||
121 | 164 | private function crawl($schema, $currentUri = null) |
|
122 | { |
||
123 | $references = schema_extract($schema, function ($keyword, $value) { |
||
124 | 160 | return $this->isRef($keyword, $value); |
|
125 | 164 | }); |
|
126 | |||
127 | 164 | foreach ($references as $path => $ref) { |
|
128 | 54 | $this->resolveReference($schema, $path, $ref, $currentUri); |
|
129 | 162 | } |
|
130 | |||
131 | 162 | return $schema; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param object $schema |
||
136 | * @param string $path |
||
137 | * @param string $ref |
||
138 | * @param string $currentUri |
||
139 | */ |
||
140 | 54 | private function resolveReference($schema, $path, $ref, $currentUri) |
|
141 | { |
||
142 | // resolve |
||
143 | 54 | if (!is_internal_ref($ref)) { |
|
144 | 26 | $resolved = new Reference(function () use ($schema, $path, $ref, $currentUri) { |
|
145 | 26 | return $this->resolveExternalReference($schema, $path, $ref, $currentUri); |
|
146 | 26 | }, $ref); |
|
147 | 26 | } else { |
|
148 | 44 | $resolved = new Reference($schema, $ref); |
|
149 | } |
||
150 | |||
151 | // handle any fragments |
||
152 | 54 | $resolved = $this->resolveFragment($ref, $resolved); |
|
153 | |||
154 | // merge |
||
155 | 54 | $this->mergeResolvedReference($schema, $resolved, $path); |
|
156 | 52 | } |
|
157 | |||
158 | /** |
||
159 | * Resolve the external reference at the given path. |
||
160 | * |
||
161 | * @param object $schema The JSON Schema |
||
162 | * @param string $path A JSON pointer to the $ref's location in the schema. |
||
163 | * @param string $ref The JSON reference |
||
164 | * @param string|null $currentUri The URI of the schema, or null if the schema was loaded from an object. |
||
165 | * |
||
166 | * @return object The schema with the reference resolved. |
||
167 | */ |
||
168 | 26 | private function resolveExternalReference($schema, $path, $ref, $currentUri) |
|
169 | { |
||
170 | 26 | $ref = $this->makeReferenceAbsolute($schema, $path, $ref, $currentUri); |
|
171 | 26 | $resolved = $this->loadExternalRef($ref); |
|
172 | |||
173 | 24 | return $this->crawl($resolved, strip_fragment($ref)); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Merge the resolved reference with the schema, at the given path. |
||
178 | * |
||
179 | * @param object $schema The schema to merge the resolved reference with |
||
180 | * @param object $resolved The resolved schema |
||
181 | * @param string $path A JSON pointer to the path where the reference should be merged. |
||
182 | * |
||
183 | * @return void |
||
184 | */ |
||
185 | 54 | private function mergeResolvedReference($schema, $resolved, $path) |
|
186 | { |
||
187 | 54 | if ($path === '') { |
|
188 | // Immediately resolve any root references. |
||
189 | 18 | while ($resolved instanceof Reference) { |
|
190 | 18 | $resolved = $resolved->resolve(); |
|
191 | 16 | } |
|
192 | 16 | $this->mergeRootRef($schema, $resolved); |
|
193 | 16 | } else { |
|
194 | 50 | $pointer = new Pointer($schema); |
|
195 | 50 | if ($pointer->has($path)) { |
|
196 | 50 | $pointer->set($path, $resolved); |
|
197 | 50 | } |
|
198 | } |
||
199 | 52 | } |
|
200 | |||
201 | /** |
||
202 | * Check if the reference contains a fragment and resolve |
||
203 | * the pointer. Otherwise returns the original schema. |
||
204 | * |
||
205 | * @param string $ref |
||
206 | * @param object $schema |
||
207 | * |
||
208 | * @return object |
||
209 | */ |
||
210 | 56 | private function resolveFragment($ref, $schema) |
|
211 | { |
||
212 | 56 | $fragment = parse_url($ref, PHP_URL_FRAGMENT); |
|
213 | 56 | if (!is_internal_ref($ref) && is_string($fragment)) { |
|
214 | 10 | if ($schema instanceof Reference) { |
|
215 | 6 | $schema = $schema->resolve(); |
|
216 | 6 | } |
|
217 | 10 | $pointer = new Pointer($schema); |
|
218 | 10 | return $pointer->get($fragment); |
|
219 | } |
||
220 | |||
221 | 56 | return $schema; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param string $attribute |
||
226 | * @param mixed $attributeValue |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | 160 | private function isRef($attribute, $attributeValue) |
|
234 | |||
235 | /** |
||
236 | * Load an external ref and return the JSON object. |
||
237 | * |
||
238 | * @param string $reference |
||
239 | * |
||
240 | * @return object |
||
241 | */ |
||
242 | 48 | private function loadExternalRef($reference) |
|
254 | |||
255 | /** |
||
256 | * Merge a resolved reference into the root of the given schema. |
||
257 | * |
||
258 | * @param object $rootSchema |
||
259 | * @param object $resolvedRef |
||
260 | */ |
||
261 | 16 | private function mergeRootRef($rootSchema, $resolvedRef) |
|
269 | |||
270 | /** |
||
271 | * Validate an absolute path is valid. |
||
272 | * |
||
273 | * @param string $path |
||
274 | */ |
||
275 | 48 | private function validateAbsolutePath($path) |
|
287 | |||
288 | /** |
||
289 | * Take a relative reference, and prepend the id of the schema and any |
||
290 | * sub schemas to get the absolute url. |
||
291 | * |
||
292 | * @param object $schema |
||
293 | * @param string $path |
||
294 | * @param string $ref |
||
295 | * @param string|null $currentUri |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | 26 | private function makeReferenceAbsolute($schema, $path, $ref, $currentUri = null) |
|
311 | |||
312 | /** |
||
313 | * Get the resolved resolution scope by walking the schema and resolving |
||
314 | * every `id` against the most immediate parent scope. |
||
315 | * |
||
316 | * @see http://json-schema.org/latest/json-schema-core.html#anchor27 |
||
317 | * |
||
318 | * @param object $schema |
||
319 | * @param string $path |
||
320 | * @param string $scope |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | 18 | private function getResolvedResolutionScope($schema, $path, $scope) |
|
343 | } |
||
344 |