67 lines
2.5 KiB
PHP
67 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* This file is part of the TYPO3 CMS project.
|
|
*
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, either version 2
|
|
* of the License, or any later version.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
*
|
|
* The TYPO3 project - inspiring people to share!
|
|
*/
|
|
|
|
namespace TYPO3\CMS\Core\SystemResource\Http;
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
use TYPO3\CMS\Core\Http\ApplicationType;
|
|
use TYPO3\CMS\Core\Http\Uri;
|
|
use TYPO3\CMS\Core\Resource\File;
|
|
|
|
/**
|
|
* This is subject to change during v14 development. Do not use.
|
|
* @internal Only to be used in TYPO3\CMS\Core\SystemResource namespace
|
|
*/
|
|
class CacheBustingUri extends Uri
|
|
{
|
|
public static function fromFileSystemPath(string $absolutePathToPotentialFile, UriInterface $baseUri, ?ApplicationType $applicationType = null): UriInterface
|
|
{
|
|
try {
|
|
// The absolute path might be appended with a query string and/ or fragment
|
|
// therefore we remove it here before we check for file existence
|
|
$absolutePath = (new Uri($absolutePathToPotentialFile))->getPath();
|
|
if (!is_file($absolutePath)) {
|
|
return $baseUri;
|
|
}
|
|
} catch (\Throwable) {
|
|
// See https://review.typo3.org/75477 for the reason of this try/catch
|
|
// likely we can remove this in the future, but it is kept for now to ensure BC
|
|
return $baseUri;
|
|
}
|
|
$configAccessor = $applicationType?->isFrontend() ? 'FE' : 'BE';
|
|
$rewriteFileName = (bool)($GLOBALS['TYPO3_CONF_VARS'][$configAccessor]['versionNumberInFilename'] ?? false);
|
|
$fileModificationTime = filemtime($absolutePath);
|
|
if ($rewriteFileName) {
|
|
$nameParts = explode('.', $baseUri->getPath());
|
|
$fileExtension = array_pop($nameParts);
|
|
array_push($nameParts, $fileModificationTime, $fileExtension);
|
|
$uri = $baseUri->withPath(implode('.', $nameParts));
|
|
} else {
|
|
$query = $baseUri->getQuery();
|
|
$uri = $baseUri->withQuery($query . ($query !== '' ? '&' : '') . $fileModificationTime);
|
|
}
|
|
|
|
return $uri;
|
|
}
|
|
|
|
public static function fromFile(File $file, UriInterface $baseUri): UriInterface
|
|
{
|
|
$query = $baseUri->getQuery();
|
|
return $baseUri->withQuery($query . ($query !== '' ? '&' : '') . $file->getSha1());
|
|
}
|
|
}
|