Skip to content

Commit

Permalink
🔥 make RenderIncludeViewHelper non static
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Nov 19, 2024
1 parent 1d6f50d commit 3ec347e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:

- name: Upload EXT:ssi_include to TER
run: |
sed -i 's/\\AUS\\SsiInclude\\Utility\\VersionUtility::getVersion()/'\''${{ steps.get_version.outputs.VERSION }}'\''/g' ext_emconf.php \
sed -i 's/InstalledVersions::getPrettyVersion('\''kanti\/server-timing'\'')/'\''${{ steps.get_version.outputs.VERSION }}'\''/g' ext_emconf.php \
&& git config --global user.email "no@one" \
&& git config --global user.name "No One" \
&& git add ext_emconf.php \
Expand Down
9 changes: 5 additions & 4 deletions Classes/Middleware/InternalSsiRedirectMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\Uri;

class InternalSsiRedirectMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (isset($request->getQueryParams()['ssi_include'])) {
$originalRequestUri = $request->getQueryParams()['originalRequestUri'] ?? '';
$originalRequestUri = new Uri($request->getQueryParams()['originalRequestUri'] ?? '');
$ssiInclude = $request->getQueryParams()['ssi_include'];
if (!preg_match('/^(\w+)$/', (string) $ssiInclude)) {
return new HtmlResponse('ssi_include invalid', 400);
Expand All @@ -26,11 +27,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$cacheFileName = RenderIncludeViewHelper::SSI_INCLUDE_DIR . $ssiInclude;
$absolutePath = Environment::getPublicPath() . $cacheFileName;
if (!file_exists($absolutePath)) {
// ignore response use the content of the file:
$subRequest = $request
->withAttribute('noCache', true)
->withUri($request->getUri()->withPath($originalRequestUri)->withQuery(''))
->withQueryParams([]);
->withQueryParams([])
->withUri($request->getUri()->withPath($originalRequestUri->getPath())->withQuery($originalRequestUri->getQuery()));
// ignore the response and instead use the content of the file
$handler->handle($subRequest);
}

Expand Down
25 changes: 0 additions & 25 deletions Classes/Utility/VersionUtility.php

This file was deleted.

77 changes: 48 additions & 29 deletions Classes/ViewHelpers/RenderIncludeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace AUS\SsiInclude\ViewHelpers;

use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use Webimpress\SafeWriter\Exception\ExceptionInterface;
use Closure;
use AUS\SsiInclude\Event\RenderedEvent;
use Closure;
use Exception;
use Override;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
Expand All @@ -17,6 +18,9 @@
use TYPO3Fluid\Fluid\ViewHelpers\RenderViewHelper;
use Webimpress\SafeWriter\FileWriter;

use function assert;

#[Autoconfigure(public: true)]
class RenderIncludeViewHelper extends RenderViewHelper
{
public const SSI_INCLUDE_DIR = '/typo3temp/tx_ssiinclude/';
Expand All @@ -25,14 +29,10 @@ class RenderIncludeViewHelper extends RenderViewHelper

public const METHOD_ESI = 'esi';

protected static function getContext(): Context
{
return GeneralUtility::makeInstance(Context::class);
}

protected static function getExtensionConfiguration(): ExtensionConfiguration
{
return GeneralUtility::makeInstance(ExtensionConfiguration::class);
public function __construct(
private readonly Context $context,
private readonly ExtensionConfiguration $extensionConfiguration,
) {
}

public function initializeArguments(): void
Expand All @@ -43,22 +43,41 @@ public function initializeArguments(): void
}

/**
* @param array<string, mixed> $arguments
* @throws Exception
* @throws ExceptionInterface
* @deprecated can be removed if parent class dose not have renderStatic anymore.
*/
#[Override]
public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
$name = self::validateName($arguments);
$isDisabled = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('ssi_include', 'disabled');
if ($isDisabled) {
return parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
}

$self = GeneralUtility::makeInstance(self::class);
assert($self instanceof self);
return $self->renderNonStatic($arguments, $renderChildrenClosure, $renderingContext);
}

/**
* @param array<string, mixed>|null $arguments
*/
public function renderNonStatic(?array $arguments = null, ?Closure $renderChildrenClosure = null, ?RenderingContextInterface $renderingContext = null): string
{
$this->arguments = $arguments ?? $this->arguments;
$this->renderingContext = $renderingContext ?? $this->renderingContext;
$renderChildrenClosure ??= $this->buildRenderChildrenClosure();

$filename = static::getSiteName() . '_' . static::getLangauge() . '_' . $name;
$name = $this->validateName($this->arguments);

$filename = $this->getSiteName() . '_' . $this->getLangauge() . '_' . $name;
$reverseProxyPrefix = '/' . trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['reverseProxyPrefix'] ?? '', '/') . '/';
$basePath = self::SSI_INCLUDE_DIR . $filename;
$includePath = rtrim($reverseProxyPrefix, '/') . $basePath;
$absolutePath = Environment::getPublicPath() . $basePath;
if (self::shouldRenderFile($absolutePath, $arguments['cacheLifeTime'])) {
$html = parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
if (self::isBackendUser()) {
if ($this->shouldRenderFile($absolutePath, $this->arguments['cacheLifeTime'])) {
$html = parent::renderStatic($this->arguments, $renderChildrenClosure, $this->renderingContext);

if ($this->isBackendUser()) {
return $html;
}

Expand All @@ -67,12 +86,12 @@ public static function renderStatic(array $arguments, Closure $renderChildrenClo
$eventDispatcher->dispatch($renderedHtmlEvent);
$html = $renderedHtmlEvent->getHtml();

@mkdir(dirname($absolutePath), (int)octdec((string)$GLOBALS['TYPO3_CONF_VARS']['SYS']['folderCreateMask']), true);
GeneralUtility::mkdir_deep(dirname($absolutePath));
FileWriter::writeFile($absolutePath, $html);
GeneralUtility::fixPermissions($absolutePath);
}

$method = self::getExtensionConfiguration()->get('ssi_include', 'method') ?: self::METHOD_SSI;
$method = $this->extensionConfiguration->get('ssi_include', 'method') ?: self::METHOD_SSI;
$reqUrl = $includePath . '?ssi_include=' . $filename . '&originalRequestUri=' . urlencode((string)GeneralUtility::getIndpEnv('REQUEST_URI'));
if ($method === self::METHOD_ESI) {
return '<esi:include src="' . $reqUrl . '" />';
Expand All @@ -81,7 +100,7 @@ public static function renderStatic(array $arguments, Closure $renderChildrenClo
return '<!--# include wait="yes" virtual="' . $reqUrl . '" -->';
}

private static function shouldRenderFile(string $absolutePath, int $cacheLifeTime): bool
private function shouldRenderFile(string $absolutePath, int $cacheLifeTime): bool
{
if (!file_exists($absolutePath)) {
return true;
Expand All @@ -91,14 +110,14 @@ private static function shouldRenderFile(string $absolutePath, int $cacheLifeTim
return true;
}

return self::isBackendUser();
return $this->isBackendUser();
}

/**
* @param array<string, mixed> $arguments
* @throws Exception
*/
private static function validateName(array $arguments): string
private function validateName(array $arguments): string
{
if (ctype_alnum((string)$arguments['name'])) {
return $arguments['name'];
Expand All @@ -107,17 +126,17 @@ private static function validateName(array $arguments): string
throw new Exception(sprintf('Only Alphanumeric characters allowed got: "%s"', $arguments['name']));
}

protected static function getLangauge(): int
protected function getLangauge(): int
{
return self::getContext()->getPropertyFromAspect('language', 'id');
return $this->context->getPropertyFromAspect('language', 'id');
}

protected static function isBackendUser(): bool
protected function isBackendUser(): bool
{
return self::getContext()->getPropertyFromAspect('backend.user', 'isLoggedIn');
return $this->context->getPropertyFromAspect('backend.user', 'isLoggedIn');
}

protected static function getSiteName(): string
protected function getSiteName(): string
{
return $GLOBALS['TYPO3_REQUEST']->getAttribute('site')->getIdentifier();
}
Expand Down
9 changes: 9 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

AUS\SsiInclude\:
resource: '../Classes/*'
exclude: '../Classes/**/{Model,Dto}/*'
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"ocramius/package-versions": "^2.1.0",
"typo3/cms-fluid": "^10.4.0 || ^11.5.0 || ^12.4.0",
"typo3/cms-frontend": "^10.4.0 || ^11.5.0 || ^12.4.0",
"webimpress/safe-writer": "^2.2.0"
Expand Down
2 changes: 2 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# cat=Configuration; type=options[SSI=ssi, ESI=esi]; label=Include method: Select the method of inclusion technique: Edge Side Include (Varnish) or Server Side Include.
method =
# cat=Configuration; type=boolean; label=Completly disable the extension. This will disable the rendering of the ssi/esi tags. and instead render the partial as normal
disabled = false
4 changes: 2 additions & 2 deletions ext_emconf.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use AUS\SsiInclude\Utility\VersionUtility;
use Composer\InstalledVersions;

/** @var string $_EXTKEY */
$EM_CONF[$_EXTKEY] = [
Expand All @@ -12,7 +12,7 @@
'author_company' => 'anders und sehr GmbH',
'state' => 'stable',
'clearCacheOnLoad' => 0,
'version' => VersionUtility::getVersion(),
'version' => InstalledVersions::getPrettyVersion('andersundsehr/ssi-include'),
'constraints' => [
'depends' => [
'typo3' => '10.4.0-11.99.99',
Expand Down

0 comments on commit 3ec347e

Please sign in to comment.