Skip to content

Commit

Permalink
[TASK] Check for NoSuchCacheException in TypeRegistry
Browse files Browse the repository at this point in the history
Related: #38
  • Loading branch information
brotkrueml committed Apr 5, 2020
1 parent c6eacd0 commit 56e5244
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions Classes/Registry/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Brotkrueml\Schema\Core\Model\WebPageElementTypeInterface;
use Brotkrueml\Schema\Core\Model\WebPageTypeInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\SingletonInterface;
Expand Down Expand Up @@ -49,7 +50,11 @@ public function __construct(FrontendInterface $cache = null, PackageManager $pac
{
if (!$cache) {
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
$this->cache = $cacheManager->getCache(static::CACHE_IDENTIFIER);
try {
$this->cache = $cacheManager->getCache(static::CACHE_IDENTIFIER);
} catch (NoSuchCacheException $e) {
// Ignore
}
} else {
$this->cache = $cache;
}
Expand Down Expand Up @@ -78,8 +83,8 @@ private function getTypesWithModels(): array

private function loadConfiguration(): array
{
if ($this->cache->has(static::CACHE_ENTRY_IDENTIFIER_TYPES)) {
return $this->cache->require(static::CACHE_ENTRY_IDENTIFIER_TYPES);
if (($cacheEntry = $this->requireCacheEntry(static::CACHE_ENTRY_IDENTIFIER_TYPES)) !== null) {
return $cacheEntry;
}

$packages = $this->packageManager->getActivePackages();
Expand All @@ -96,11 +101,27 @@ private function loadConfiguration(): array
$typeModels = \array_replace_recursive(...$allTypeModels);
\ksort($typeModels);

$this->cache->set(static::CACHE_ENTRY_IDENTIFIER_TYPES, 'return ' . \var_export($typeModels, true) . ';');
$this->setCacheEntry(static::CACHE_ENTRY_IDENTIFIER_TYPES, $typeModels);

return $typeModels;
}

private function requireCacheEntry(string $identifier): ?array
{
if ($this->cache instanceof FrontendInterface && $this->cache->has($identifier)) {
return $this->cache->require($identifier);
}

return null;
}

private function setCacheEntry(string $identifier, array $data): void
{
if ($this->cache instanceof FrontendInterface) {
$this->cache->set($identifier, 'return ' . \var_export($data, true) . ';');
}
}

private function enrichTypeModelsArrayWithTypeKey(array $typeModels): array
{
$typeModelsWithTypeKey = [];
Expand Down Expand Up @@ -133,8 +154,8 @@ public function getWebPageTypes(): array

private function loadSpecialTypes(string $cacheEntryIdentifier, string $typeInterface): array
{
if ($this->cache->has($cacheEntryIdentifier)) {
return $this->cache->require($cacheEntryIdentifier);
if (($cacheEntry = $this->requireCacheEntry($cacheEntryIdentifier)) !== null) {
return $cacheEntry;
}

$specialTypes = [];
Expand All @@ -151,8 +172,7 @@ private function loadSpecialTypes(string $cacheEntryIdentifier, string $typeInte
}

\sort($specialTypes);

$this->cache->set($cacheEntryIdentifier, 'return ' . \var_export($specialTypes, true) . ';');
$this->setCacheEntry($cacheEntryIdentifier, $specialTypes);

return $specialTypes;
}
Expand Down

0 comments on commit 56e5244

Please sign in to comment.