@@ -21,6 +21,9 @@ final class DefinitionNameFactory implements DefinitionNameFactoryInterface
21
21
{
22
22
use ResourceClassInfoTrait;
23
23
24
+ private const GLUE = '. ' ;
25
+ private array $ prefixCache = [];
26
+
24
27
public function __construct (private ?array $ distinctFormats )
25
28
{
26
29
}
@@ -32,18 +35,18 @@ public function create(string $className, string $format = 'json', ?string $inpu
32
35
}
33
36
34
37
if (!isset ($ prefix )) {
35
- $ prefix = ( new \ ReflectionClass ( $ className ))-> getShortName ( );
38
+ $ prefix = $ this -> createPrefixFromClass ( $ className );
36
39
}
37
40
38
41
if (null !== $ inputOrOutputClass && $ className !== $ inputOrOutputClass ) {
39
42
$ parts = explode ('\\' , $ inputOrOutputClass );
40
43
$ shortName = end ($ parts );
41
- $ prefix .= ' . ' .$ shortName ;
44
+ $ prefix .= self :: GLUE .$ shortName ;
42
45
}
43
46
44
47
if ('json ' !== $ format && ($ this ->distinctFormats [$ format ] ?? false )) {
45
48
// JSON is the default, and so isn't included in the definition name
46
- $ prefix .= ' . ' .$ format ;
49
+ $ prefix .= self :: GLUE .$ format ;
47
50
}
48
51
49
52
$ definitionName = $ serializerContext [SchemaFactory::OPENAPI_DEFINITION_NAME ] ?? null ;
@@ -61,4 +64,22 @@ private function encodeDefinitionName(string $name): string
61
64
{
62
65
return preg_replace ('/[^a-zA-Z0-9.\-_]/ ' , '. ' , $ name );
63
66
}
67
+
68
+ private function createPrefixFromClass (string $ fullyQualifiedClassName , int $ namespaceParts = 1 ): string
69
+ {
70
+ $ parts = explode ('\\' , $ fullyQualifiedClassName );
71
+ $ name = implode (self ::GLUE , \array_slice ($ parts , -$ namespaceParts ));
72
+
73
+ if (!isset ($ this ->prefixCache [$ name ])) {
74
+ $ this ->prefixCache [$ name ] = $ fullyQualifiedClassName ;
75
+
76
+ return $ name ;
77
+ }
78
+
79
+ if ($ this ->prefixCache [$ name ] !== $ fullyQualifiedClassName ) {
80
+ $ name = $ this ->createPrefixFromClass ($ fullyQualifiedClassName , ++$ namespaceParts );
81
+ }
82
+
83
+ return $ name ;
84
+ }
64
85
}
0 commit comments