@@ -154,8 +154,16 @@ public static function component(DispatchableComponent $component, $options = []
154154 return $ manager ;
155155 }
156156
157+ //Component Manager Caching
158+ protected static $ cmc = [];
159+
157160 protected static function _componentManager ($ fullClass , Dispatch $ dispatch = null , $ options = []): ResourceManager
158161 {
162+ if (isset (static ::$ cmc [$ fullClass ]))
163+ {
164+ return static ::$ cmc [$ fullClass ];
165+ }
166+
159167 $ class = ltrim ($ fullClass , '\\' );
160168 if (!$ dispatch )
161169 {
@@ -186,6 +194,7 @@ protected static function _componentManager($fullClass, Dispatch $dispatch = nul
186194 $ manager = new static (self ::MAP_COMPONENT , $ parts , $ options );
187195 $ manager ->_componentPath = $ dispatch ->componentClassResourcePath ($ fullClass );
188196 $ manager ->_dispatch = $ dispatch ;
197+ static ::$ cmc [$ fullClass ] = $ manager ;
189198 return $ manager ;
190199 }
191200
@@ -269,6 +278,8 @@ protected function _requireInlineJs($javascript, ?array $options = [], int $prio
269278 return $ this ;
270279 }
271280
281+ protected $ _resourceUriCache = [];
282+
272283 /**
273284 * @param $relativeFullPath
274285 *
@@ -286,38 +297,45 @@ public function getResourceUri($relativeFullPath, bool $allowComponentBubble = t
286297 return $ relativeFullPath ;
287298 }
288299
289- [$ filePath , $ relativeFullPath ] = $ this ->_optimisePath ($ this ->getFilePath ($ relativeFullPath ), $ relativeFullPath );
290- //Do not allow bubbling if the component is a fixed class component
291- if ($ allowComponentBubble && $ this ->_component && $ this ->_component instanceof FixedClassComponent)
292- {
293- $ allowComponentBubble = false ;
294- }
295- if ($ allowComponentBubble && $ this ->_type == self ::MAP_COMPONENT && $ this ->_component && !file_exists ($ filePath ))
300+ $ cacheKey = ($ allowComponentBubble ? '1 ' : '0 ' ) . $ relativeFullPath . $ flags ;
301+ if (!isset ($ this ->_resourceUriCache [$ cacheKey ]))
296302 {
297- $ parent = (new ReflectionClass ($ this ->_component ))->getParentClass ();
298- if ($ parent && !$ parent ->isAbstract () && $ parent ->implementsInterface (DispatchableComponent::class))
303+
304+ [$ filePath , $ relativeFullPath ] = $ this ->_optimisePath ($ this ->getFilePath ($ relativeFullPath ), $ relativeFullPath );
305+ //Do not allow bubbling if the component is a fixed class component
306+ if ($ allowComponentBubble && $ this ->_component && $ this ->_component instanceof FixedClassComponent)
299307 {
300- return self ::componentClass ($ parent ->getName (), $ this ->_options )
301- ->getResourceUri ($ relativeFullPath , $ allowComponentBubble );
308+ $ allowComponentBubble = false ;
302309 }
303- }
304- $ relHash = $ this ->getRelativeHash ($ filePath );
305- $ hash = $ this ->getFileHash ($ filePath );
310+ if ($ allowComponentBubble && $ this ->_type == self ::MAP_COMPONENT && $ this ->_component && !file_exists ($ filePath ))
311+ {
312+ $ parent = (new ReflectionClass ($ this ->_component ))->getParentClass ();
313+ if ($ parent && !$ parent ->isAbstract () && $ parent ->implementsInterface (DispatchableComponent::class))
314+ {
315+ return self ::componentClass ($ parent ->getName (), $ this ->_options )
316+ ->getResourceUri ($ relativeFullPath , $ allowComponentBubble );
317+ }
318+ }
319+ $ relHash = $ this ->getRelativeHash ($ filePath );
320+ $ hash = $ this ->getFileHash ($ filePath );
306321
307- $ bits = Dispatch::instance ()->getBits ();
308- if ($ flags !== null )
309- {
310- $ bits = BitWise::add ($ bits , $ flags );
311- }
322+ $ bits = Dispatch::instance ()->getBits ();
323+ if ($ flags !== null )
324+ {
325+ $ bits = BitWise::add ($ bits , $ flags );
326+ }
312327
313- if (!$ hash )
314- {
315- return null ;
328+ if (!$ hash )
329+ {
330+ return null ;
331+ }
332+
333+ $ uri = $ this ->getBaseUri ();
334+ $ this ->_resourceUriCache [$ cacheKey ] = $ uri . (empty ($ uri ) ? '' : '/ ' ) . $ hash . $ relHash
335+ . ($ bits > 0 ? '- ' . base_convert ($ bits , 10 , 36 ) : '' ) . '/ ' . $ relativeFullPath ;
316336 }
317337
318- $ uri = $ this ->getBaseUri ();
319- return $ uri . (empty ($ uri ) ? '' : '/ ' ) . $ hash . $ relHash . ($ bits > 0 ? '- ' . base_convert ($ bits , 10 , 36 ) : '' )
320- . '/ ' . $ relativeFullPath ;
338+ return $ this ->_resourceUriCache [$ cacheKey ];
321339 }
322340
323341 protected $ _optimizeWebP ;
@@ -506,4 +524,10 @@ protected function _requireInlineCss(
506524 $ this ->getResourceStore ()->requireInlineCss ($ stylesheet , $ options , $ priority );
507525 return $ this ;
508526 }
527+
528+ public static function clearCache ()
529+ {
530+ static ::$ cmc = [];
531+ static ::$ _fileHashCache = [];
532+ }
509533}
0 commit comments