@@ -65,6 +65,11 @@ class DocumentParser
65
65
*/
66
66
private $ properties = [];
67
67
68
+ /**
69
+ * @var array Local cache for documents
70
+ */
71
+ private $ documents = [];
72
+
68
73
/**
69
74
* @param Reader $reader Used for reading annotations.
70
75
* @param DocumentFinder $finder Used for resolving namespaces.
@@ -86,35 +91,40 @@ public function __construct(Reader $reader, DocumentFinder $finder)
86
91
*/
87
92
public function parse (\ReflectionClass $ class )
88
93
{
89
- /** @var Document $document */
90
- $ document = $ this ->reader ->getClassAnnotation ($ class , self ::DOCUMENT_ANNOTATION );
91
-
92
- if ($ document === null ) {
93
- throw new MissingDocumentAnnotationException (
94
- sprintf (
95
- '"%s" class cannot be parsed as document because @Document annotation is missing. ' ,
96
- $ class ->getName ()
97
- )
98
- );
99
- }
94
+ $ className = $ class ->getName ();
100
95
101
- $ fields = [];
102
-
103
- return [
104
- 'type ' => $ document ->type ?: Caser::snake ($ class ->getShortName ()),
105
- 'properties ' => $ this ->getProperties ($ class ),
106
- 'fields ' => array_filter (
107
- array_merge (
108
- $ document ->dump (),
109
- $ fields
110
- )
111
- ),
112
- 'aliases ' => $ this ->getAliases ($ class , $ fields ),
113
- 'analyzers ' => $ this ->getAnalyzers ($ class ),
114
- 'objects ' => $ this ->getObjects (),
115
- 'namespace ' => $ class ->getName (),
116
- 'class ' => $ class ->getShortName (),
117
- ];
96
+ if (!isset ($ this ->documents [$ className ])) {
97
+ /** @var Document $document */
98
+ $ document = $ this ->reader ->getClassAnnotation ($ class , self ::DOCUMENT_ANNOTATION );
99
+
100
+ if ($ document === null ) {
101
+ throw new MissingDocumentAnnotationException (
102
+ sprintf (
103
+ '"%s" class cannot be parsed as document because @Document annotation is missing. ' ,
104
+ $ class ->getName ()
105
+ )
106
+ );
107
+ }
108
+
109
+ $ fields = [];
110
+
111
+ $ this ->documents [$ className ] = [
112
+ 'type ' => $ document ->type ?: Caser::snake ($ class ->getShortName ()),
113
+ 'properties ' => $ this ->getProperties ($ class ),
114
+ 'fields ' => array_filter (
115
+ array_merge (
116
+ $ document ->dump (),
117
+ $ fields
118
+ )
119
+ ),
120
+ 'aliases ' => $ this ->getAliases ($ class , $ fields ),
121
+ 'analyzers ' => $ this ->getAnalyzers ($ class ),
122
+ 'objects ' => $ this ->getObjects (),
123
+ 'namespace ' => $ class ->getName (),
124
+ 'class ' => $ class ->getShortName (),
125
+ ];
126
+ }
127
+ return $ this ->documents [$ className ];
118
128
}
119
129
120
130
/**
0 commit comments