Skip to content

Commit

Permalink
[!!!][TASK] Always concatenate all given files in ResourceCompressor
Browse files Browse the repository at this point in the history
ResourceCompressor allows to only concatenate files
given in a given "baseDirectory" as separate option, which
was used to only include files that are registered via TBE_STYLES
for backend purposes.

The functionality is removed to always concatenate all CSS files
in TYPO3 Backend.

A heavy dependency to DocumentTemplate of ALL backend
functionality using PageRenderer is therefore removed.

Resolves: #88758
Releases: master
Change-Id: I07e2f15a3dd2298371db87ce2723ded0f6a56f31
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61296
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
  • Loading branch information
bmack authored and andreaskienast committed Jul 16, 2019
1 parent 9aea7a6 commit 51041ae
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 53 deletions.
5 changes: 2 additions & 3 deletions typo3/sysext/backend/Classes/Template/DocumentTemplate.php
Expand Up @@ -559,9 +559,8 @@ public function addStyleSheetDirectory($path)
{
$path = GeneralUtility::getFileAbsFileName($path);
// Read all files in directory and sort them alphabetically
$cssFiles = GeneralUtility::getFilesInDir($path, 'css');
foreach ($cssFiles as $cssFile) {
$this->pageRenderer->addCssFile(PathUtility::getRelativePathTo($path) . $cssFile);
foreach (GeneralUtility::getFilesInDir($path, 'css', true) as $cssFile) {
$this->pageRenderer->addCssFile($cssFile);
}
}

Expand Down
4 changes: 2 additions & 2 deletions typo3/sysext/backend/Classes/Template/ModuleTemplate.php
Expand Up @@ -294,11 +294,11 @@ protected function loadStylesheets()
if (!empty($GLOBALS['TBE_STYLES']['stylesheet2'])) {
$this->pageRenderer->addCssFile($GLOBALS['TBE_STYLES']['stylesheet2']);
}
// @see DocumentTemplate::addStyleSheetDirectory
// Add all *.css files of the directory $path to the stylesheets
foreach ($this->getRegisteredStylesheetFolders() as $folder) {
// Read all files in directory and sort them alphabetically
$cssFiles = GeneralUtility::getFilesInDir($folder, 'css', true);
foreach ($cssFiles as $cssFile) {
foreach (GeneralUtility::getFilesInDir($folder, 'css', true) as $cssFile) {
$this->pageRenderer->addCssFile($cssFile);
}
}
Expand Down
9 changes: 2 additions & 7 deletions typo3/sysext/core/Classes/Page/PageRenderer.php
Expand Up @@ -16,7 +16,6 @@

use TYPO3\CMS\Backend\Routing\Router;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\DocumentTemplate;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Core\Environment;
Expand Down Expand Up @@ -2367,12 +2366,8 @@ protected function doConcatenateCss()
];
GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssConcatenateHandler'], $params, $this);
} else {
$cssOptions = [];
if (TYPO3_MODE === 'BE') {
$cssOptions = ['baseDirectories' => GeneralUtility::makeInstance(DocumentTemplate::class)->getSkinStylesheetDirectories()];
}
$this->cssLibs = $this->getCompressor()->concatenateCssFiles($this->cssLibs, $cssOptions);
$this->cssFiles = $this->getCompressor()->concatenateCssFiles($this->cssFiles, $cssOptions);
$this->cssLibs = $this->getCompressor()->concatenateCssFiles($this->cssLibs);
$this->cssFiles = $this->getCompressor()->concatenateCssFiles($this->cssFiles);
}
}
}
Expand Down
71 changes: 30 additions & 41 deletions typo3/sysext/core/Classes/Resource/ResourceCompressor.php
Expand Up @@ -103,14 +103,10 @@ public function setRootPath($rootPath)
/**
* Concatenates the Stylesheet files
*
* Options:
* baseDirectories If set, only include files below one of the base directories
*
* @param array $cssFiles CSS files to process
* @param array $options Additional options
* @return array CSS files
*/
public function concatenateCssFiles(array $cssFiles, array $options = [])
public function concatenateCssFiles(array $cssFiles)
{
$filesToIncludeByType = ['all' => []];
foreach ($cssFiles as $key => $fileOptions) {
Expand All @@ -119,45 +115,34 @@ public function concatenateCssFiles(array $cssFiles, array $options = [])
continue;
}
$filenameFromMainDir = $this->getFilenameFromMainDir($fileOptions['file']);
// if $options['baseDirectories'] set, we only include files below these directories
if (
!isset($options['baseDirectories'])
|| $this->checkBaseDirectory(
$filenameFromMainDir,
array_merge($options['baseDirectories'], [$this->targetDirectory])
)
) {
$type = isset($fileOptions['media']) ? strtolower($fileOptions['media']) : 'all';
if (!isset($filesToIncludeByType[$type])) {
$filesToIncludeByType[$type] = [];
}
if (!empty($fileOptions['forceOnTop'])) {
array_unshift($filesToIncludeByType[$type], $filenameFromMainDir);
} else {
$filesToIncludeByType[$type][] = $filenameFromMainDir;
}
// remove the file from the incoming file array
unset($cssFiles[$key]);
$type = isset($fileOptions['media']) ? strtolower($fileOptions['media']) : 'all';
if (!isset($filesToIncludeByType[$type])) {
$filesToIncludeByType[$type] = [];
}
if (!empty($fileOptions['forceOnTop'])) {
array_unshift($filesToIncludeByType[$type], $filenameFromMainDir);
} else {
$filesToIncludeByType[$type][] = $filenameFromMainDir;
}
// remove the file from the incoming file array
unset($cssFiles[$key]);
}
if (!empty($filesToIncludeByType)) {
foreach ($filesToIncludeByType as $mediaOption => $filesToInclude) {
if (empty($filesToInclude)) {
continue;
}
$targetFile = $this->createMergedCssFile($filesToInclude);
$concatenatedOptions = [
'file' => $targetFile,
'rel' => 'stylesheet',
'media' => $mediaOption,
'compress' => true,
'excludeFromConcatenation' => true,
'forceOnTop' => false,
'allWrap' => ''
];
// place the merged stylesheet on top of the stylesheets
$cssFiles = array_merge($cssFiles, [$targetFile => $concatenatedOptions]);
foreach ($filesToIncludeByType as $mediaOption => $filesToInclude) {
if (empty($filesToInclude)) {
continue;
}
$targetFile = $this->createMergedCssFile($filesToInclude);
$concatenatedOptions = [
'file' => $targetFile,
'rel' => 'stylesheet',
'media' => $mediaOption,
'compress' => true,
'excludeFromConcatenation' => true,
'forceOnTop' => false,
'allWrap' => ''
];
// place the merged stylesheet on top of the stylesheets
$cssFiles = array_merge($cssFiles, [$targetFile => $concatenatedOptions]);
}
return $cssFiles;
}
Expand Down Expand Up @@ -447,6 +432,10 @@ protected function getFilenameFromMainDir($filename)

// if the file is an absolute reference within the docRoot
$absolutePath = $docRoot . '/' . $fileNameWithoutSlash;
// if it is already an absolute path to the file
if (PathUtility::isAbsolutePath($filename)) {
$absolutePath = $filename;
}
// Calling is_file without @ for a path starting with '../' causes a PHP Warning when using open_basedir restriction
if (@is_file($absolutePath)) {
if (strpos($absolutePath, $this->rootPath) === 0) {
Expand Down
@@ -0,0 +1,45 @@
.. include:: ../../Includes.txt

=====================================================================================
Breaking: #88758 - Selective Concatenation of CSS files in ResourceCompressor removed
=====================================================================================

See :issue:`88758`

Description
===========

ResourceCompressor, used to merge and compress CSS and JS files, has had an option to only
merge CSS files from selected folders. This was used to limit CSS files of skins for TYPO3
Backend files.

The functionality has been removed, as all added CSS files are now merged into one file.

As TYPO3 Frontend and TypoScript has a much more flexible system for adding CSS files,
which should be concatenated, this change does not affect TYPO3 API of Frontend Requests.


Impact
======

Calling ResourceCompressor->concatenateCssFiles() with a second argument has no effect anymore.

Adding CSS files manually in TYPO3 Backend via custom extensions will now automatically be merged
with the loaded CSS styles of :php:`$TBE_STYLES` skin.


Affected Installations
======================

TYPO3 installations with extensions adding third-party CSS files in the TYPO3 Backend,
or extensions using ResourceCompressor directly.


Migration
=========

None, as it is considered to be useful to have one larger CSS file for TYPO3 Backend.

If necessary, add a CSS file manually via PageRenderer API which should be excluded from Concatenation.

.. index:: PHP-API, FullyScanned
Expand Up @@ -241,4 +241,10 @@
'Breaking-88741-CHashCalculationInIndexedSearchRemoved.rst',
],
],
'TYPO3\CMS\Core\Resource\ResourceCompressor->concatenateCssFiles' => [
'maximumNumberOfArguments' => 1,
'restFiles' => [
'Breaking-88758-SelectiveConcatenationOfCSSFilesInResourceCompressorRemoved.rst',
],
],
];

0 comments on commit 51041ae

Please sign in to comment.