Skip to content

Commit

Permalink
Merge pull request #10 from xima-media/fix-full-ts-cached-pages
Browse files Browse the repository at this point in the history
Fix full ts cached pages
  • Loading branch information
jackd248 authored Dec 12, 2024
2 parents 18d1072 + 616dc86 commit b8b6878
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 38 deletions.
12 changes: 3 additions & 9 deletions Classes/Middleware/EditInformationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use Xima\XimaTypo3FrontendEdit\Configuration;
use Xima\XimaTypo3FrontendEdit\Service\MenuGenerator;
use Xima\XimaTypo3FrontendEdit\Utility\UrlUtility;
Expand All @@ -25,20 +23,16 @@ class EditInformationMiddleware implements MiddlewareInterface

public function __construct(protected readonly MenuGenerator $menuGenerator, private readonly ExtensionConfiguration $extensionConfiguration)
{
$this->configuration = $this->extensionConfiguration->get(Configuration::EXT_KEY);
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
$params = $request->getQueryParams();

if (
!($response instanceof NullResponse)
&& $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
&& isset($params['type'])
&& $params['type'] === Configuration::TYPE
) {
if (isset($params['type'])&& $params['type'] === Configuration::TYPE) {
$this->configuration = $this->extensionConfiguration->get(Configuration::EXT_KEY);

$pid = $request->getAttribute('routing')->getPageId();
$languageUid = $request->getAttribute('language')->getLanguageId();
$returnUrl = ($request->getHeaderLine('Referer') === '' || (array_key_exists('forceReturnUrlGeneration', $this->configuration) && $this->configuration['forceReturnUrlGeneration'])) ? UrlUtility::getUrl($pid, $languageUid) : $request->getHeaderLine('Referer');
Expand Down
10 changes: 4 additions & 6 deletions Classes/Middleware/ToolRendererMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use Xima\XimaTypo3FrontendEdit\Controller\EditController;
use Xima\XimaTypo3FrontendEdit\Utility\ResourceRenderer;

class ToolRendererMiddleware implements MiddlewareInterface
{
public function __construct(protected readonly EditController $editController)
public function __construct(protected readonly ResourceRenderer $resourceRenderer)
{
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (
$GLOBALS['TSFE'] instanceof TypoScriptFrontendController
&& $GLOBALS['BE_USER']
$GLOBALS['BE_USER']
&& (!array_key_exists('tx_ximatypo3frontendedit_disable', $GLOBALS['BE_USER']->user) || !$GLOBALS['BE_USER']->user['tx_ximatypo3frontendedit_disable'])
) {
$body = $response->getBody();
$body->rewind();
$contents = $response->getBody()->getContents();
$content = str_ireplace(
'</body>',
$this->editController->render() . '</body>',
$this->resourceRenderer->render() . '</body>',
$contents
);
$body = new Stream('php://temp', 'rw');
Expand Down
20 changes: 3 additions & 17 deletions Classes/Service/MenuGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Xima\XimaTypo3FrontendEdit\Service;

use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\TypoScriptAspect;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
Expand All @@ -24,11 +22,12 @@ final class MenuGenerator

public function __construct(protected readonly IconFactory $iconFactory, protected readonly EventDispatcher $eventDispatcher)
{
$this->getSettings();
}

public function getDropdown(int $pid, string $returnUrl, int $languageUid, array $data = []): array
{
$this->getSettings();

$ignoredPids = array_key_exists('ignorePids', $this->configuration) ? explode(',', $this->configuration['ignorePids']) : [];
foreach ($ignoredPids as $ignoredPid) {
if ($this->isSubpageOf($pid, (int)$ignoredPid)) {
Expand Down Expand Up @@ -297,20 +296,7 @@ private function shortenString(string $string, int $maxLength = 30): string
private function getSettings(): void
{
$request = $GLOBALS['TYPO3_REQUEST'];
try {
$fullTypoScript = $request->getAttribute('frontend.typoscript')->getSetupArray();
} catch (\Exception $e) {
// An exception is thrown, when TypoScript setup array is not available. This is usually the case,
// when the current page request is cached. Therefore, the TSFE TypoScript parsing is forced here.
// Set a TypoScriptAspect which forces template parsing
GeneralUtility::makeInstance(Context::class)
->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, true));
// Call TSFE getFromCache, which re-processes TypoScript respecting $forcedTemplateParsing property
// from TypoScriptAspect
$tsfe = $request->getAttribute('frontend.controller');
$requestWithFullTypoScript = $tsfe->getFromCache($request);
$fullTypoScript = $requestWithFullTypoScript->getAttribute('frontend.typoscript')->getSetupArray();
}
$fullTypoScript = $request->getAttribute('frontend.typoscript')->getSetupArray();
$settings = $fullTypoScript['plugin.']['tx_ximatypo3frontendedit.']['settings.'] ?? [];
$this->configuration = GeneralUtility::removeDotsFromTS($settings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

declare(strict_types=1);

namespace Xima\XimaTypo3FrontendEdit\Controller;
namespace Xima\XimaTypo3FrontendEdit\Utility;

use TYPO3\CMS\Backend\Attribute\AsController;
use TYPO3\CMS\Core\Core\RequestId;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Fluid\View\StandaloneView;
use Xima\XimaTypo3FrontendEdit\Configuration;
use Xima\XimaTypo3FrontendEdit\Utility\ResourceUtility;

#[AsController]
final class EditController extends ActionController
class ResourceRenderer
{
public function __construct(private readonly RequestId $requestId)
{
Expand Down
1 change: 1 addition & 0 deletions Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ tx_ximatypo3frontendedit_editinformationen {
typeNum = 1729341864
config {
contentType = application/json
no_cache = 1
}
}

0 comments on commit b8b6878

Please sign in to comment.