ResourceCompressor
This class merges and compresses CSS and JavaScript files of the TYPO3 Frontend.
It should never be used for TYPO3 Backend.
Table of Contents
Properties
- $createGzipped : bool
- gzipped versions are only created if $TYPO3_CONF_VARS['BE' or 'FE']['compressionLevel'] is set
- $gzipCompressionLevel : int
- $gzipFileExtension : string
- $htaccessTemplate : string
- $initialized : bool
- $rootPath : string
- $targetDirectory : string
Methods
- compressCssFile() : string
- Compresses a CSS file
- compressCssFiles() : array<string|int, mixed>
- Compress multiple css files
- compressJavaScriptSource() : string
- compressJsFile() : string
- Compresses a javascript file
- compressJsFiles() : array<string|int, mixed>
- Compress multiple javascript files
- concatenateCssFiles() : array<string|int, mixed>
- Concatenates the Stylesheet files
- concatenateJsFiles() : array<string|int, mixed>
- Concatenates the JavaScript files
- checkBaseDirectory() : bool
- Decides whether a file comes from one of the baseDirectories
- compressCssString() : string
- Compress a CSS string by removing comments and whitespace characters
- createMergedCssFile() : mixed
- Creates a merged CSS file
- createMergedFile() : mixed
- Creates a merged file with given file type
- createMergedJsFile() : mixed
- Creates a merged JS file
- cssFixRelativeUrlPaths() : string
- cssFixStatements() : string
- Moves @charset, @import and @namespace statements to the top of the content, because they must occur before all other CSS rules
- getFilenameFromMainDir() : string
- Finds the relative path to a file, relative to the root path.
- getJavaScriptFileType() : string
- Determines the JavaScript mime type
- getPathFixer() : RelativeCssPathFixer
- initialize() : void
- retrieveExternalFile() : string
- Retrieves an external file and stores it locally.
- returnFileReference() : string
- Decides whether a client can deal with gzipped content or not and returns the according file name, based on HTTP_ACCEPT_ENCODING
- writeFileAndCompressed() : mixed
- Writes $contents into file $filename together with a gzipped version into $filename.gz (gzipFileExtension)
Properties
$createGzipped
gzipped versions are only created if $TYPO3_CONF_VARS['BE' or 'FE']['compressionLevel'] is set
protected
bool
$createGzipped
= false
$gzipCompressionLevel
protected
int
$gzipCompressionLevel
= -1
$gzipFileExtension
protected
string
$gzipFileExtension
= '.gz'
$htaccessTemplate
protected
string
$htaccessTemplate
= '<FilesMatch "\.(js|css)(\.gz)?$">
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 7 days"
</IfModule>
FileETag MTime Size
</FilesMatch>'
$initialized
protected
bool
$initialized
= false
$rootPath
protected
string
$rootPath
= ''
$targetDirectory
protected
string
$targetDirectory
= 'typo3temp/assets/compressed/'
Methods
compressCssFile()
Compresses a CSS file
public
compressCssFile(string $filename) : string
Options: baseDirectories If set, only include files below one of the base directories
removes comments and whitespaces Adopted from https://github.com/drupal/drupal/blob/8.0.x/core/lib/Drupal/Core/Asset/CssOptimizer.php
Parameters
- $filename : string
-
Source filename, relative to requested page
Return values
string —Compressed filename, relative to requested page
compressCssFiles()
Compress multiple css files
public
compressCssFiles(array<string|int, mixed> $cssFiles) : array<string|int, mixed>
Parameters
- $cssFiles : array<string|int, mixed>
-
The files to compress (array key = filename), relative to requested page
Return values
array<string|int, mixed> —The CSS files after compression (array key = new filename), relative to requested page
compressJavaScriptSource()
public
compressJavaScriptSource(string $javaScriptSourceCode) : string
Parameters
- $javaScriptSourceCode : string
Return values
stringcompressJsFile()
Compresses a javascript file
public
compressJsFile(string $filename) : string
Parameters
- $filename : string
-
Source filename, relative to requested page
Return values
string —Filename of the compressed file, relative to requested page
compressJsFiles()
Compress multiple javascript files
public
compressJsFiles(array<string|int, mixed> $jsFiles) : array<string|int, mixed>
Parameters
- $jsFiles : array<string|int, mixed>
-
The files to compress (array key = filename), relative to requested page
Return values
array<string|int, mixed> —The js files after compression (array key = new filename), relative to requested page
concatenateCssFiles()
Concatenates the Stylesheet files
public
concatenateCssFiles(array<string|int, mixed> $cssFiles) : array<string|int, mixed>
Parameters
- $cssFiles : array<string|int, mixed>
-
CSS files to process
Return values
array<string|int, mixed> —CSS files
concatenateJsFiles()
Concatenates the JavaScript files
public
concatenateJsFiles(array<string|int, mixed> $jsFiles) : array<string|int, mixed>
Parameters
- $jsFiles : array<string|int, mixed>
-
JavaScript files to process
Return values
array<string|int, mixed> —JS files
checkBaseDirectory()
Decides whether a file comes from one of the baseDirectories
protected
checkBaseDirectory(string $filename, array<string|int, mixed> $baseDirectories) : bool
Parameters
- $filename : string
-
Filename
- $baseDirectories : array<string|int, mixed>
-
Base directories
Return values
bool —File belongs to a base directory or not
compressCssString()
Compress a CSS string by removing comments and whitespace characters
protected
compressCssString(string $contents) : string
Parameters
- $contents : string
Return values
stringcreateMergedCssFile()
Creates a merged CSS file
protected
createMergedCssFile(array<string|int, mixed> $filesToInclude) : mixed
Parameters
- $filesToInclude : array<string|int, mixed>
-
Files which should be merged, paths relative to root path
Return values
mixed —Filename of the merged file
createMergedFile()
Creates a merged file with given file type
protected
createMergedFile(array<string|int, mixed> $filesToInclude[, string $type = 'css' ]) : mixed
Parameters
- $filesToInclude : array<string|int, mixed>
-
Files which should be merged, paths relative to root path
- $type : string = 'css'
-
File type
Tags
Return values
mixed —Filename of the merged file
createMergedJsFile()
Creates a merged JS file
protected
createMergedJsFile(array<string|int, mixed> $filesToInclude) : mixed
Parameters
- $filesToInclude : array<string|int, mixed>
-
Files which should be merged, paths relative to root path
Return values
mixed —Filename of the merged file
cssFixRelativeUrlPaths()
protected
cssFixRelativeUrlPaths(string $contents, string $filename) : string
Parameters
- $contents : string
- $filename : string
Return values
stringcssFixStatements()
Moves @charset, @import and @namespace statements to the top of the content, because they must occur before all other CSS rules
protected
cssFixStatements(string $contents) : string
Parameters
- $contents : string
-
Data to process
Return values
string —Processed data
getFilenameFromMainDir()
Finds the relative path to a file, relative to the root path.
protected
getFilenameFromMainDir(string $filename) : string
Parameters
- $filename : string
-
the name of the file
Return values
string —the path to the file relative to the root path ($this->rootPath)
getJavaScriptFileType()
Determines the JavaScript mime type
protected
getJavaScriptFileType() : string
The <script> tag only needs the type if the page is not rendered as HTML5. For TYPO3 Frontend the configured config.doctype is evaluated.
Return values
stringgetPathFixer()
protected
getPathFixer() : RelativeCssPathFixer
Return values
RelativeCssPathFixerinitialize()
protected
initialize() : void
retrieveExternalFile()
Retrieves an external file and stores it locally.
protected
retrieveExternalFile(string $url) : string
Parameters
- $url : string
Return values
string —Temporary local filename for the externally-retrieved file
returnFileReference()
Decides whether a client can deal with gzipped content or not and returns the according file name, based on HTTP_ACCEPT_ENCODING
protected
returnFileReference(string $filename) : string
Parameters
- $filename : string
-
File name
Return values
string —$filename suffixed with '.gz' or not - dependent on HTTP_ACCEPT_ENCODING
writeFileAndCompressed()
Writes $contents into file $filename together with a gzipped version into $filename.gz (gzipFileExtension)
protected
writeFileAndCompressed(string $filename, string $contents) : mixed
Parameters
- $filename : string
-
Target filename
- $contents : string
-
File contents