Skip to content

Commit

Permalink
Cache resource manager common calls (#28)
Browse files Browse the repository at this point in the history
* Cache resource manager common calls

* Cache clear
  • Loading branch information
bajb committed Sep 7, 2020
1 parent fcc9076 commit ac418e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -96,6 +98,7 @@ public static function instance()
public static function destroy()
{
self::$_instance = null;
ResourceManager::clearCache();
}

/**
Expand Down Expand Up @@ -171,6 +174,7 @@ public function getBaseUri()
public function addComponentAlias($namespace, $alias)
{
$this->_componentAliases['_' . $alias] = $namespace;
ResourceManager::clearCache();
return $this;
}

Expand Down
74 changes: 49 additions & 25 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -269,6 +278,8 @@ protected function _requireInlineJs($javascript, ?array $options = [], int $prio
return $this;
}

protected $_resourceUriCache = [];

/**
* @param $relativeFullPath
*
Expand All @@ -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;
Expand Down Expand Up @@ -506,4 +524,10 @@ protected function _requireInlineCss(
$this->getResourceStore()->requireInlineCss($stylesheet, $options, $priority);
return $this;
}

public static function clearCache()
{
static::$cmc = [];
static::$_fileHashCache = [];
}
}

0 comments on commit ac418e8

Please sign in to comment.