Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TYPO3 13 compatibility #1917

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Classes/Middleware/AssetInclusion.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

/** @var AssetService $assetService */
$assetService = GeneralUtility::makeInstance(AssetService::class);
$assetService->buildAllUncached([], $GLOBALS['TSFE'], $contents);
$assetService->buildAllUncached([], $request->getAttribute('frontend.controller', null) ?? $GLOBALS['TSFE'], $contents);

if ($contentsBefore === $contents) {
// Content is unchanged, return the original response since there is no need to modify it, or the
Expand Down
36 changes: 23 additions & 13 deletions Classes/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use FluidTYPO3\Vhs\Asset;
use FluidTYPO3\Vhs\Utility\CoreUtility;
use FluidTYPO3\Vhs\Utility\TsfeUtility;
use FluidTYPO3\Vhs\ViewHelpers\Asset\AssetInterface;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Log\LogManager;
Expand Down Expand Up @@ -42,12 +43,18 @@ class AssetService implements SingletonInterface
protected static ?array $settingsCache = null;
protected static array $cachedDependencies = [];
protected static bool $cacheCleared = false;
protected TsfeUtility $tsfeUtility;

public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
}

public function __construct()
{
$this->tsfeUtility = new TsfeUtility();
}

public function usePageCache(object $caller, bool $shouldUsePageCache): bool
{
$this->buildAll([], $caller);
Expand All @@ -61,7 +68,7 @@ public function buildAll(array $parameters, object $caller, bool $cached = true,
}

$settings = $this->getSettings();
$buildTypoScriptAssets = (!static::$typoScriptAssetsBuilt && ($cached || $GLOBALS['TSFE']->no_cache));
$buildTypoScriptAssets = (!static::$typoScriptAssetsBuilt && ($cached || $this->tsfeUtility->isNoCache()));
if ($buildTypoScriptAssets && isset($settings['asset']) && is_array($settings['asset'])) {
foreach ($settings['asset'] as $name => $typoScriptAsset) {
if (!isset($GLOBALS['VhsAssets'][$name]) && is_array($typoScriptAsset)) {
Expand Down Expand Up @@ -272,8 +279,11 @@ protected function writeCachedMergedFileAndReturnTag(array $assets, string $type
sort($keys);
$assetName = implode('-', $keys);
unset($keys);
if (isset($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['assets.']['mergedAssetsUseHashedFilename'])) {
if ($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['assets.']['mergedAssetsUseHashedFilename']) {

$typoscript = $this->tsfeUtility->getTyposcriptSetupArray();

if (isset($typoscript['plugin.']['tx_vhs.']['assets.']['mergedAssetsUseHashedFilename'])) {
if ($typoscript['plugin.']['tx_vhs.']['assets.']['mergedAssetsUseHashedFilename']) {
$assetName = md5($assetName);
}
}
Expand All @@ -282,8 +292,8 @@ protected function writeCachedMergedFileAndReturnTag(array $assets, string $type
if (!file_exists($fileAbsolutePathAndFilename)
|| 0 === filemtime($fileAbsolutePathAndFilename)
|| isset($GLOBALS['BE_USER'])
|| ($GLOBALS['TSFE']->no_cache ?? false)
|| ($GLOBALS['TSFE']->page['no_cache'] ?? false)
|| $this->isNoCache()
|| ($this->getPageRecordFromRequest()['no_cache'] ?? false)
) {
foreach ($assets as $name => $asset) {
$assetSettings = $this->extractAssetSettings($asset);
Expand Down Expand Up @@ -346,6 +356,9 @@ protected function generateTagForAssetType(
$file = PathUtility::getAbsoluteWebPath($file);
$file = $this->prefixPath($file);
}

$typoscript = $this->tsfeUtility->getTyposcriptSetupArray();

switch ($type) {
case 'js':
$tagBuilder->setTagName('script');
Expand All @@ -357,7 +370,7 @@ protected function generateTagForAssetType(
$tagBuilder->addAttribute('src', (string) $file);
}
if (!empty($integrity)) {
if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
if (!empty($typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$tagBuilder->addAttribute('crossorigin', 'anonymous');
}
$tagBuilder->addAttribute('integrity', $integrity);
Expand Down Expand Up @@ -385,7 +398,7 @@ protected function generateTagForAssetType(
$tagBuilder->addAttribute('href', $file);
}
if (!empty($integrity)) {
if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
if (!empty($typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$tagBuilder->addAttribute('crossorigin', 'anonymous');
}
$tagBuilder->addAttribute('integrity', $integrity);
Expand Down Expand Up @@ -723,7 +736,7 @@ protected function mergeArrays(array $array1, array $array2): array

protected function getFileIntegrity(string $file): ?string
{
$typoScript = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.'] ?? null;
$typoScript = $this->tsfeUtility->getTyposcriptSetupArray()['plugin.']['tx_vhs.'] ?? null;
if (isset($typoScript['assets.']['tagsAddSubresourceIntegrity'])) {
// Note: 3 predefined hashing strategies (the ones suggestes in the rfc sheet)
if (0 < $typoScript['assets.']['tagsAddSubresourceIntegrity']
Expand All @@ -733,9 +746,6 @@ protected function getFileIntegrity(string $file): ?string
return null;
}

/** @var TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];

$integrity = null;
$integrityMethod = ['sha256','sha384','sha512'][
$typoScript['assets.']['tagsAddSubresourceIntegrity'] - 1
Expand All @@ -749,8 +759,8 @@ protected function getFileIntegrity(string $file): ?string
if (!file_exists($integrityFile)
|| 0 === filemtime($integrityFile)
|| isset($GLOBALS['BE_USER'])
|| $typoScriptFrontendController->no_cache
|| $typoScriptFrontendController->page['no_cache']
|| $this->isNoCache()
|| $this->getPageRecordFromRequest()['no_cache']
) {
if (extension_loaded('hash') && function_exists('hash_file')) {
$integrity = base64_encode((string) hash_file($integrityMethod, $file, true));
Expand Down
25 changes: 21 additions & 4 deletions Classes/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Utility\CoreUtility;
use FluidTYPO3\Vhs\Utility\TsfeUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
Expand All @@ -17,6 +19,7 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
* Page Service
Expand All @@ -32,6 +35,12 @@ class PageService implements SingletonInterface

protected static array $cachedPages = [];
protected static array $cachedMenus = [];
protected TsfeUtility $tsfeUtility;

public function __construct()
{
$this->tsfeUtility = new TsfeUtility();
}

public function getMenu(
int $pageUid,
Expand Down Expand Up @@ -77,7 +86,7 @@ public function getRootLine(
bool $reverse = false
): array {
if (null === $pageUid) {
$pageUid = $GLOBALS['TSFE']->id;
$pageUid = $this->tsfeUtility->getPageId();
}
/** @var RootlineUtility $rootLineUtility */
$rootLineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageUid);
Expand Down Expand Up @@ -127,11 +136,11 @@ public function hidePageForLanguageUid($page = null, int $languageUid = -1, bool
$pageUid = $page['uid'];
$pageRecord = $page;
} else {
$pageUid = (0 === (integer) $page) ? $GLOBALS['TSFE']->id : (integer) $page;
$pageUid = (0 === (integer)$page) ? $this->tsfeUtility->getPageId() : (integer)$page;
$pageRecord = $this->getPage($pageUid);
}
if (-1 === $languageUid) {
$languageUid = $GLOBALS['TSFE']->sys_language_uid;
$languageUid = $this->tsfeUtility->getPageRecordFromRequest()['sys_language_uid'];
if (class_exists(LanguageAspect::class)) {
/** @var Context $context */
$context = GeneralUtility::makeInstance(Context::class);
Expand Down Expand Up @@ -181,6 +190,14 @@ public function getItemLink(array $page, bool $forceAbsoluteUrl = false): string
'forceAbsoluteUrl' => $forceAbsoluteUrl,
];

if (CoreUtility::getTypo3MajorVersion() > 12) {
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$request = $this->tsfeUtility->getRequest();
$cObj->setRequest($request);
$cObj->start($request->getAttribute('frontend.page.information')->getPageRecord(), 'pages');
return $cObj->createUrl($config);
}

return $GLOBALS['TSFE']->cObj->typoLink('', $config);
}

Expand Down Expand Up @@ -209,7 +226,7 @@ public function isAccessGranted(array $page): bool

public function isCurrent(int $pageUid): bool
{
return ($pageUid === (integer) $GLOBALS['TSFE']->id);
return ($pageUid === $this->tsfeUtility->getPageId());
}

public function isActive(int $pageUid): bool
Expand Down
10 changes: 10 additions & 0 deletions Classes/Utility/CoreUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public static function getCurrentCoreVersion(): string
{
return VersionNumberUtility::getCurrentTypo3Version();
}

public static function getTypo3Version(): string
{
return (new \TYPO3\CMS\Core\Information\Typo3Version())->getVersion();
}

public static function getTypo3MajorVersion(): int
{
return (new \TYPO3\CMS\Core\Information\Typo3Version())->getMajorVersion();
}
}
73 changes: 73 additions & 0 deletions Classes/Utility/TsfeUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace FluidTYPO3\Vhs\Utility;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Page\PageInformation;

/**
* Class TsfeUtility
*/
class TsfeUtility
{
/**
* @return ServerRequestInterface
*/
public function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}

public function getPageId(): int {
if(CoreUtility::getTypo3MajorVersion() > 12) {
$pageInformation = $this->getRequest()->getAttribute('frontend.page.information');
return $pageInformation->getId();
}

return $GLOBALS['TSFE']->id;
}

/**
* @return array
* @throws \Doctrine\DBAL\Exception
*/
public function getPageRecordFromRequest(): array
{
if (CoreUtility::getTypo3MajorVersion() > 12) {
/** @var PageInformation $pageInformation */
$pageInformation = $this->getRequest()->getAttribute('frontend.page.information');
return $pageInformation->getPageRecord();
}

$pageArguments = $request->getAttribute('routing');
$pageId = $pageArguments->getPageId();

$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
$pageRecord = $connection->select('*', 'pages', ['uid' => $pageId])->fetchAssociative();

return $pageRecord;
}

/**
* @return array|null
*/
public function getTyposcriptSetupArray(): ?array
{
$setup = CoreUtility::getTypo3MajorVersion() > 11
? $this->getRequest()->getAttribute('frontend.typoscript')->getSetupArray()
: $GLOBALS['TSFE']->tmpl->setup;

return $setup;
}

public function isNoCache(): bool {
if(CoreUtility::getTypo3MajorVersion() > 12) {
$instructions = $this->getRequest()->getAttribute('frontend.cache.instruction');
return !empty($instructions) ? $instructions->isCachingAllowed() === false : false;
}

return (bool)($GLOBALS['TSFE']->no_cache ?? false);
}
}
7 changes: 5 additions & 2 deletions Classes/ViewHelpers/Media/AbstractMediaViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

use FluidTYPO3\Vhs\Utility\ContextUtility;
use FluidTYPO3\Vhs\Utility\TsfeUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
Expand Down Expand Up @@ -49,8 +50,10 @@ public static function preprocessSourceUri(string $src, array $arguments): strin
if (substr($src, 0, 1) !== '/' && substr($src, 0, 4) !== 'http') {
$src = $GLOBALS['TSFE']->absRefPrefix . $src;
}
if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$src = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $src;
$typoscript = (new TsfeUtility())->getTyposcriptSetupArray();

if (!empty($typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$src =$typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'] . $src;
} elseif (ContextUtility::isBackend() || !$arguments['relative']) {
/** @var string $siteUrl */
$siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
Expand Down
6 changes: 4 additions & 2 deletions Classes/ViewHelpers/Media/SourceViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use FluidTYPO3\Vhs\Utility\ContextUtility;
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
use FluidTYPO3\Vhs\Utility\TsfeUtility;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand Down Expand Up @@ -165,8 +166,9 @@ public function render()
*/
public function preprocessSourceUri(string $src): string
{
if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$src = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $src;
$typoscript = (new TsfeUtility())->getTyposcriptSetupArray();
if (!empty($typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$src = $typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'] . $src;
} elseif (ContextUtility::isBackend() || !$this->arguments['relative']) {
if (GeneralUtility::isValidUrl($src)) {
$src = ltrim($src, '/');
Expand Down
3 changes: 2 additions & 1 deletion Classes/ViewHelpers/Page/StaticPrefixViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Utility\TsfeUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
Expand Down Expand Up @@ -38,6 +39,6 @@ public static function renderStatic(
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
return $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] ?? '';
return (new TsfeUtility())->getTyposcriptSetupArray()['plugin.']['tx_vhs.']['settings.']['prependPath'] ?? '';
}
}
6 changes: 4 additions & 2 deletions Classes/ViewHelpers/Resource/AbstractImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use FluidTYPO3\Vhs\Utility\ContextUtility;
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
use FluidTYPO3\Vhs\Utility\ResourceUtility;
use FluidTYPO3\Vhs\Utility\TsfeUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand Down Expand Up @@ -161,8 +162,9 @@ public function preprocessImages(array $files, bool $onlyProperties = false): ?a
*/
public function preprocessSourceUri(string $source): string
{
if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$source = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $source;
$typoscript = (new TsfeUtility())->getTyposcriptSetupArray());
if (!empty($typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$source = $typoscript['plugin.']['tx_vhs.']['settings.']['prependPath'] . $source;
} elseif (ContextUtility::isBackend() || !$this->arguments['relative']) {
/** @var string $siteUrl */
$siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
Expand Down
6 changes: 4 additions & 2 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@

$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['v'] = ['FluidTYPO3\\Vhs\\ViewHelpers'];

// add navigtion hide to fix menu viewHelpers (e.g. breadcrumb)
$GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'] .= (empty($GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']) ? '' : ',') . 'nav_hide,shortcut,shortcut_mode';
if(\FluidTYPO3\Vhs\Utility\CoreUtility::getTypo3MajorVersion() < 13) {
// add navigtion hide to fix menu viewHelpers (e.g. breadcrumb)
$GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields'] .= (empty($GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']) ? '' : ',') . 'nav_hide,shortcut,shortcut_mode';
}

// add and urltype to fix the rendering of external url doktypes
if (isset($GLOBALS['TCA']['pages']['columns']['urltype'])) {
Expand Down