diff --git a/.gitignore b/.gitignore index 8219a80..bbe1f95 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ public/ vendor/ var/ .phpunit.result.cache +.idea/ diff --git a/Classes/Middleware/InternalSsiRedirectMiddleware.php b/Classes/Middleware/InternalSsiRedirectMiddleware.php index 78a06d3..9e05106 100644 --- a/Classes/Middleware/InternalSsiRedirectMiddleware.php +++ b/Classes/Middleware/InternalSsiRedirectMiddleware.php @@ -40,7 +40,21 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface ->withQueryParams([]) ->withUri($request->getUri()->withPath($originalRequestUri->getPath())->withQuery($originalRequestUri->getQuery())); $handler->handle($subRequest); - $content = $this->lastRenderedContentRegister->get(); + $keyParts = explode('_', (string) $ssiInclude); + if (isset($keyParts[2])) { + $key = $keyParts[2]; + // may strip the .html extension + $position = strrpos($key, '.'); + if ($position) { + $key = substr($key, 0, $position); + } + + $content = $this->lastRenderedContentRegister->get($key); + } elseif (file_exists($absolutePath)) { + $content = file_get_contents($absolutePath); + } else { + $content = 'EXT:ssi_include error path:' . $ssiInclude . ''; + } } return new HtmlResponse($content ?: 'EXT:ssi_include error path:' . $absolutePath . ''); diff --git a/Classes/Register/LastRenderedContentRegister.php b/Classes/Register/LastRenderedContentRegister.php index 4460f92..b2ecc5d 100644 --- a/Classes/Register/LastRenderedContentRegister.php +++ b/Classes/Register/LastRenderedContentRegister.php @@ -8,15 +8,22 @@ class LastRenderedContentRegister implements SingletonInterface { - private string $lastRenderedContent = ''; + /** + * @var array + */ + private array $lastRenderedContent = []; - public function set(string $content): void + public function set(string $key, string $content): void { - $this->lastRenderedContent = $content; + $this->lastRenderedContent[$key] = $content; } - public function get(): string + public function get(string $key): string { - return $this->lastRenderedContent; + if (!isset($this->lastRenderedContent[$key])) { + return ''; + } + + return $this->lastRenderedContent[$key]; } } diff --git a/Classes/Utility/IsCacheableUtility.php b/Classes/Utility/IsCacheableUtility.php index 056182d..8e5fe05 100644 --- a/Classes/Utility/IsCacheableUtility.php +++ b/Classes/Utility/IsCacheableUtility.php @@ -15,6 +15,10 @@ */ class IsCacheableUtility { + public function __construct(private readonly Context $context) + { + } + public function usePageCache(?TypoScriptFrontendController $typoScriptFrontendController = null, bool $usePageCache = true): bool { if (!$usePageCache) { @@ -26,7 +30,7 @@ public function usePageCache(?TypoScriptFrontendController $typoScriptFrontendCo return $usePageCache; } - $context = GeneralUtility::makeInstance(Context::class); + $context = $this->context; assert($context instanceof Context); $backendUserContext = $context->getAspect('backend.user'); if ($backendUserContext->isLoggedIn()) { diff --git a/Classes/ViewHelpers/RenderIncludeViewHelper.php b/Classes/ViewHelpers/RenderIncludeViewHelper.php index 3b5d780..348adf5 100644 --- a/Classes/ViewHelpers/RenderIncludeViewHelper.php +++ b/Classes/ViewHelpers/RenderIncludeViewHelper.php @@ -14,7 +14,6 @@ use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; use TYPO3\CMS\Core\Cache\CacheManager; use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException; -use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; @@ -85,10 +84,13 @@ public function renderNonStatic(?array $arguments = null, ?Closure $renderChildr $this->renderingContext = $renderingContext ?? $this->renderingContext; $renderChildrenClosure ??= $this->buildRenderChildrenClosure(); + // generate the cache filename + $name = $this->validateName($this->arguments); + if ($this->isBackendUser()) { $content = parent::renderStatic($this->arguments, $renderChildrenClosure, $this->renderingContext); // Put the code to register to use in InternalSsiRedirectMiddleware if the site comes from page cache - $this->lastRenderedContentRegister->set($content); + $this->lastRenderedContentRegister->set($name, $content); return $content; } @@ -100,9 +102,7 @@ public function renderNonStatic(?array $arguments = null, ?Closure $renderChildr $groupString = '_' . implode('-', $frontendUser->getGroupIds()); } - // generate the cache filename - $name = $this->validateName($this->arguments); - $filename = $this->getSiteName() . '_' . $this->getLangauge() . '_' . $name . $groupString . '.html'; + $filename = $this->getSiteName() . '_' . $this->getLanguage() . '_' . $name . $groupString . '.html'; // If the cache has not the proper entry, generate it $cache = $this->cacheManager->getCache('aus_ssi_include_cache'); @@ -116,10 +116,10 @@ public function renderNonStatic(?array $arguments = null, ?Closure $renderChildr $cacheTags = ['tx_ssiinclude_' . $name, ...$this->arguments['cacheTags']]; $cache->set($filename, $html, $cacheTags, $this->arguments['cacheLifeTime']); - $this->lastRenderedContentRegister->set($html); + $this->lastRenderedContentRegister->set($name, $html); } - // generate the variables needed for include commments + // generate the variables needed for include comments $reqUrl = $this->filenameUtility->getReqUrl($filename); $method = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('ssi_include', 'method'); if ($method === self::METHOD_ESI) { @@ -145,7 +145,7 @@ private function validateName(array $arguments): string /** * @throws AspectNotFoundException */ - protected function getLangauge(): int + protected function getLanguage(): int { return $this->context->getPropertyFromAspect('language', 'id'); }