@@ -19,6 +19,10 @@ final class Factory
19
19
{
20
20
use Nette \SmartObject;
21
21
22
+ private $ bodyCache = [];
23
+ private $ extractorCache = [];
24
+
25
+
22
26
public function fromClassReflection (\ReflectionClass $ from , bool $ withBodies = false ): ClassType
23
27
{
24
28
if ($ withBodies && $ from ->isAnonymous ()) {
@@ -72,7 +76,7 @@ public function fromClassReflection(\ReflectionClass $from, bool $withBodies = f
72
76
}
73
77
$ class ->setProperties ($ props );
74
78
75
- $ methods = $ bodies = [];
79
+ $ methods = [];
76
80
foreach ($ from ->getMethods () as $ method ) {
77
81
if (
78
82
$ method ->getDeclaringClass ()->name === $ from ->name
@@ -82,9 +86,10 @@ public function fromClassReflection(\ReflectionClass $from, bool $withBodies = f
82
86
if ($ withBodies ) {
83
87
$ srcMethod = Nette \Utils \Reflection::getMethodDeclaringMethod ($ method );
84
88
$ srcClass = $ srcMethod ->getDeclaringClass ();
85
- $ b = $ bodies [$ srcClass ->name ] = $ bodies [$ srcClass ->name ] ?? $ this ->getExtractor ($ srcClass )->extractMethodBodies ($ srcClass ->name );
86
- if (isset ($ b [$ srcMethod ->name ])) {
87
- $ m ->setBody ($ b [$ srcMethod ->name ]);
89
+ $ bodies = &$ this ->bodyCache [$ srcClass ->name ];
90
+ $ bodies = $ bodies ?? $ this ->getExtractor ($ srcClass )->extractMethodBodies ($ srcClass ->name );
91
+ if (isset ($ bodies [$ srcMethod ->name ])) {
92
+ $ m ->setBody ($ bodies [$ srcMethod ->name ]);
88
93
}
89
94
}
90
95
}
@@ -271,7 +276,10 @@ private function getVisibility($from): string
271
276
private function getExtractor ($ from ): Extractor
272
277
{
273
278
$ file = $ from ->getFileName ();
274
- if (!$ file ) {
279
+ $ cache = &$ this ->extractorCache [$ file ];
280
+ if ($ cache !== null ) {
281
+ return $ cache ;
282
+ } elseif (!$ file ) {
275
283
throw new Nette \InvalidStateException ("Source code of $ from ->name not found. " );
276
284
}
277
285
return new Extractor (file_get_contents ($ file ));
0 commit comments