From ac418e84fa593cb3a5f8e742f59a7d546bb29a81 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Mon, 7 Sep 2020 22:54:48 +0100 Subject: [PATCH] Cache resource manager common calls (#28) * Cache resource manager common calls * Cache clear --- src/Dispatch.php | 4 +++ src/ResourceManager.php | 74 +++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/Dispatch.php b/src/Dispatch.php index cb76873..0661a6d 100644 --- a/src/Dispatch.php +++ b/src/Dispatch.php @@ -80,11 +80,13 @@ public function __construct($projectRoot, $baseUri = null, ClassLoader $loader = $this->_defaultCacheConfig = new ResponseCacheConfig(); $this->_baseUri = $baseUri; $this->_classLoader = $loader; + ResourceManager::clearCache(); } public static function bind(Dispatch $instance) { self::$_instance = $instance; + ResourceManager::clearCache(); return $instance; } @@ -96,6 +98,7 @@ public static function instance() public static function destroy() { self::$_instance = null; + ResourceManager::clearCache(); } /** @@ -171,6 +174,7 @@ public function getBaseUri() public function addComponentAlias($namespace, $alias) { $this->_componentAliases['_' . $alias] = $namespace; + ResourceManager::clearCache(); return $this; } diff --git a/src/ResourceManager.php b/src/ResourceManager.php index f2e5a23..edc9226 100644 --- a/src/ResourceManager.php +++ b/src/ResourceManager.php @@ -154,8 +154,16 @@ public static function component(DispatchableComponent $component, $options = [] return $manager; } + //Component Manager Caching + protected static $cmc = []; + protected static function _componentManager($fullClass, Dispatch $dispatch = null, $options = []): ResourceManager { + if(isset(static::$cmc[$fullClass])) + { + return static::$cmc[$fullClass]; + } + $class = ltrim($fullClass, '\\'); if(!$dispatch) { @@ -186,6 +194,7 @@ protected static function _componentManager($fullClass, Dispatch $dispatch = nul $manager = new static(self::MAP_COMPONENT, $parts, $options); $manager->_componentPath = $dispatch->componentClassResourcePath($fullClass); $manager->_dispatch = $dispatch; + static::$cmc[$fullClass] = $manager; return $manager; } @@ -269,6 +278,8 @@ protected function _requireInlineJs($javascript, ?array $options = [], int $prio return $this; } + protected $_resourceUriCache = []; + /** * @param $relativeFullPath * @@ -286,38 +297,45 @@ public function getResourceUri($relativeFullPath, bool $allowComponentBubble = t return $relativeFullPath; } - [$filePath, $relativeFullPath] = $this->_optimisePath($this->getFilePath($relativeFullPath), $relativeFullPath); - //Do not allow bubbling if the component is a fixed class component - if($allowComponentBubble && $this->_component && $this->_component instanceof FixedClassComponent) - { - $allowComponentBubble = false; - } - if($allowComponentBubble && $this->_type == self::MAP_COMPONENT && $this->_component && !file_exists($filePath)) + $cacheKey = ($allowComponentBubble ? '1' : '0') . $relativeFullPath . $flags; + if(!isset($this->_resourceUriCache[$cacheKey])) { - $parent = (new ReflectionClass($this->_component))->getParentClass(); - if($parent && !$parent->isAbstract() && $parent->implementsInterface(DispatchableComponent::class)) + + [$filePath, $relativeFullPath] = $this->_optimisePath($this->getFilePath($relativeFullPath), $relativeFullPath); + //Do not allow bubbling if the component is a fixed class component + if($allowComponentBubble && $this->_component && $this->_component instanceof FixedClassComponent) { - return self::componentClass($parent->getName(), $this->_options) - ->getResourceUri($relativeFullPath, $allowComponentBubble); + $allowComponentBubble = false; } - } - $relHash = $this->getRelativeHash($filePath); - $hash = $this->getFileHash($filePath); + if($allowComponentBubble && $this->_type == self::MAP_COMPONENT && $this->_component && !file_exists($filePath)) + { + $parent = (new ReflectionClass($this->_component))->getParentClass(); + if($parent && !$parent->isAbstract() && $parent->implementsInterface(DispatchableComponent::class)) + { + return self::componentClass($parent->getName(), $this->_options) + ->getResourceUri($relativeFullPath, $allowComponentBubble); + } + } + $relHash = $this->getRelativeHash($filePath); + $hash = $this->getFileHash($filePath); - $bits = Dispatch::instance()->getBits(); - if($flags !== null) - { - $bits = BitWise::add($bits, $flags); - } + $bits = Dispatch::instance()->getBits(); + if($flags !== null) + { + $bits = BitWise::add($bits, $flags); + } - if(!$hash) - { - return null; + if(!$hash) + { + return null; + } + + $uri = $this->getBaseUri(); + $this->_resourceUriCache[$cacheKey] = $uri . (empty($uri) ? '' : '/') . $hash . $relHash + . ($bits > 0 ? '-' . base_convert($bits, 10, 36) : '') . '/' . $relativeFullPath; } - $uri = $this->getBaseUri(); - return $uri . (empty($uri) ? '' : '/') . $hash . $relHash . ($bits > 0 ? '-' . base_convert($bits, 10, 36) : '') - . '/' . $relativeFullPath; + return $this->_resourceUriCache[$cacheKey]; } protected $_optimizeWebP; @@ -506,4 +524,10 @@ protected function _requireInlineCss( $this->getResourceStore()->requireInlineCss($stylesheet, $options, $priority); return $this; } + + public static function clearCache() + { + static::$cmc = []; + static::$_fileHashCache = []; + } }