29
29
*/
30
30
class Reference implements SpecObjectInterface, DocumentContextInterface
31
31
{
32
+ /** @var array<string, mixed> */
33
+ private static $ relativeReferencesCache = [];
34
+
32
35
/**
33
36
* @var string
34
37
*/
@@ -296,14 +299,19 @@ private function resolveTransitiveReference(Reference $referencedObject, Referen
296
299
*/
297
300
private function adjustRelativeReferences ($ referencedDocument , $ basePath , $ baseDocument = null , $ oContext = null )
298
301
{
299
- $ context = new ReferenceContext (null , $ basePath );
300
302
if ($ baseDocument === null ) {
301
303
$ baseDocument = $ referencedDocument ;
302
304
}
303
305
304
306
foreach ($ referencedDocument as $ key => $ value ) {
305
307
// adjust reference URLs
306
308
if ($ key === '$ref ' && is_string ($ value )) {
309
+
310
+ $ fullPath = $ basePath . $ value ;
311
+ if (array_key_exists ($ fullPath , self ::$ relativeReferencesCache )) {
312
+ return self ::$ relativeReferencesCache [$ fullPath ];
313
+ }
314
+
307
315
if (isset ($ value [0 ]) && $ value [0 ] === '# ' ) {
308
316
// direcly inline references in the same document,
309
317
// these are not going to be valid in the new context anymore
@@ -315,8 +323,10 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
315
323
$ this ->_recursingInsideFile = true ;
316
324
$ return = $ this ->adjustRelativeReferences ($ inlineDocument , $ basePath , $ baseDocument , $ oContext );
317
325
$ this ->_recursingInsideFile = false ;
326
+ self ::$ relativeReferencesCache [$ fullPath ] = $ return ;
318
327
return $ return ;
319
328
}
329
+ $ context = new ReferenceContext (null , $ basePath );
320
330
$ referencedDocument [$ key ] = $ context ->resolveRelativeUri ($ value );
321
331
$ parts = explode ('# ' , $ referencedDocument [$ key ], 2 );
322
332
if ($ parts [0 ] === $ oContext ->getUri ()) {
@@ -329,6 +339,7 @@ private function adjustRelativeReferences($referencedDocument, $basePath, $baseD
329
339
// adjust URLs for 'externalValue' references in Example Objects
330
340
// https://spec.openapis.org/oas/v3.0.3#example-object
331
341
if ($ key === 'externalValue ' && is_string ($ value )) {
342
+ $ context = new ReferenceContext (null , $ basePath );
332
343
$ referencedDocument [$ key ] = $ this ->makeRelativePath ($ oContext ->getUri (), $ context ->resolveRelativeUri ($ value ));
333
344
continue ;
334
345
}
0 commit comments