Skip to content

Commit ac418e8

Browse files
authored
Cache resource manager common calls (#28)
* Cache resource manager common calls * Cache clear
1 parent fcc9076 commit ac418e8

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

src/Dispatch.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ public function __construct($projectRoot, $baseUri = null, ClassLoader $loader =
8080
$this->_defaultCacheConfig = new ResponseCacheConfig();
8181
$this->_baseUri = $baseUri;
8282
$this->_classLoader = $loader;
83+
ResourceManager::clearCache();
8384
}
8485

8586
public static function bind(Dispatch $instance)
8687
{
8788
self::$_instance = $instance;
89+
ResourceManager::clearCache();
8890
return $instance;
8991
}
9092

@@ -96,6 +98,7 @@ public static function instance()
9698
public static function destroy()
9799
{
98100
self::$_instance = null;
101+
ResourceManager::clearCache();
99102
}
100103

101104
/**
@@ -171,6 +174,7 @@ public function getBaseUri()
171174
public function addComponentAlias($namespace, $alias)
172175
{
173176
$this->_componentAliases['_' . $alias] = $namespace;
177+
ResourceManager::clearCache();
174178
return $this;
175179
}
176180

src/ResourceManager.php

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)