TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ResourceCompressor Class Reference

Public Member Functions

 __construct ()
 
 setRootPath ($rootPath)
 
 concatenateCssFiles (array $cssFiles, array $options=[])
 
 concatenateJsFiles (array $jsFiles)
 
 compressCssFiles (array $cssFiles)
 
 compressCssFile ($filename)
 
 compressJsFiles (array $jsFiles)
 
 compressJsFile ($filename)
 

Protected Member Functions

 createMergedCssFile (array $filesToInclude)
 
 createMergedJsFile (array $filesToInclude)
 
 createMergedFile (array $filesToInclude, $type= 'css')
 
 getFilenameFromMainDir ($filename)
 
 checkBaseDirectory ($filename, array $baseDirectories)
 
 cssFixRelativeUrlPaths ($contents, $oldDir)
 
 findAndReplaceUrlPathsByRegex ($contents, $regex, $newDir, $wrap= '|')
 
 cssFixStatements ($contents)
 
 writeFileAndCompressed ($filename, $contents)
 
 returnFileReference ($filename)
 
 retrieveExternalFile ($url)
 
 compressCssString ($contents)
 

Protected Attributes

 $targetDirectory = 'typo3temp/assets/compressed/'
 
 $rootPath = ''
 
 $createGzipped = false
 
 $gzipCompressionLevel = -1
 
 $htaccessTemplate
 

Detailed Description

Compressor This merges and compresses CSS and JavaScript files of the TYPO3 Backend.

Definition at line 25 of file ResourceCompressor.php.

Constructor & Destructor Documentation

Member Function Documentation

checkBaseDirectory (   $filename,
array  $baseDirectories 
)
protected

Decides whether a file comes from one of the baseDirectories

Parameters
string$filenameFilename
array$baseDirectoriesBase directories
Returns
bool File belongs to a base directory or not

Definition at line 480 of file ResourceCompressor.php.

References GeneralUtility\isFirstPartOfStr().

Referenced by ResourceCompressor\concatenateCssFiles().

compressCssFile (   $filename)

Compresses a CSS file

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
string$filenameSource filename, relative to requested page
Returns
string Compressed filename, relative to requested page

Definition at line 339 of file ResourceCompressor.php.

References ResourceCompressor\compressCssString(), ResourceCompressor\cssFixRelativeUrlPaths(), PathUtility\dirname(), ResourceCompressor\getFilenameFromMainDir(), PathUtility\pathinfo(), GeneralUtility\resolveBackPath(), ResourceCompressor\returnFileReference(), PathUtility\stripPathSitePrefix(), and ResourceCompressor\writeFileAndCompressed().

Referenced by ResourceCompressor\compressCssFiles().

compressCssFiles ( array  $cssFiles)

Compress multiple css files

Parameters
array$cssFilesThe files to compress (array key = filename), relative to requested page
Returns
array The CSS files after compression (array key = new filename), relative to requested page

Definition at line 310 of file ResourceCompressor.php.

References ResourceCompressor\compressCssFile().

compressCssString (   $contents)
protected

Compress a CSS string by removing comments and whitespace characters

Parameters
string$contents
Returns
string

Definition at line 656 of file ResourceCompressor.php.

Referenced by ResourceCompressor\compressCssFile().

compressJsFile (   $filename)

Compresses a javascript file

Parameters
string$filenameSource filename, relative to requested page
Returns
string Filename of the compressed file, relative to requested page

Definition at line 394 of file ResourceCompressor.php.

References ResourceCompressor\getFilenameFromMainDir(), PathUtility\pathinfo(), GeneralUtility\resolveBackPath(), ResourceCompressor\returnFileReference(), and ResourceCompressor\writeFileAndCompressed().

Referenced by ResourceCompressor\compressJsFiles().

compressJsFiles ( array  $jsFiles)

Compress multiple javascript files

Parameters
array$jsFilesThe files to compress (array key = filename), relative to requested page
Returns
array The js files after compression (array key = new filename), relative to requested page

Definition at line 371 of file ResourceCompressor.php.

References ResourceCompressor\compressJsFile().

concatenateCssFiles ( array  $cssFiles,
array  $options = [] 
)

Concatenates the Stylesheet files

Options: baseDirectories If set, only include files below one of the base directories

Parameters
array$cssFilesCSS files to process
array$optionsAdditional options
Returns
array CSS files

Definition at line 110 of file ResourceCompressor.php.

References ResourceCompressor\checkBaseDirectory(), ResourceCompressor\createMergedCssFile(), and ResourceCompressor\getFilenameFromMainDir().

concatenateJsFiles ( array  $jsFiles)

Concatenates the JavaScript files

Parameters
array$jsFilesJavaScript files to process
Returns
array JS files

Definition at line 167 of file ResourceCompressor.php.

References ResourceCompressor\createMergedJsFile(), and ResourceCompressor\getFilenameFromMainDir().

createMergedCssFile ( array  $filesToInclude)
protected

Creates a merged CSS file

Parameters
array$filesToIncludeFiles which should be merged, paths relative to root path
Returns
mixed Filename of the merged file

Definition at line 212 of file ResourceCompressor.php.

References ResourceCompressor\createMergedFile().

Referenced by ResourceCompressor\concatenateCssFiles().

createMergedFile ( array  $filesToInclude,
  $type = 'css' 
)
protected

Creates a merged file with given file type

Parameters
array$filesToIncludeFiles which should be merged, paths relative to root path
string$typeFile type
Exceptions
InvalidArgumentException
Returns
mixed Filename of the merged file

Definition at line 237 of file ResourceCompressor.php.

References ResourceCompressor\cssFixRelativeUrlPaths(), ResourceCompressor\cssFixStatements(), PathUtility\dirname(), GeneralUtility\getIndpEnv(), GeneralUtility\isFirstPartOfStr(), GeneralUtility\isOnCurrentHost(), GeneralUtility\isValidUrl(), GeneralUtility\resolveBackPath(), ResourceCompressor\retrieveExternalFile(), PathUtility\stripPathSitePrefix(), and GeneralUtility\writeFile().

Referenced by ResourceCompressor\createMergedCssFile(), and ResourceCompressor\createMergedJsFile().

createMergedJsFile ( array  $filesToInclude)
protected

Creates a merged JS file

Parameters
array$filesToIncludeFiles which should be merged, paths relative to root path
Returns
mixed Filename of the merged file

Definition at line 223 of file ResourceCompressor.php.

References ResourceCompressor\createMergedFile().

Referenced by ResourceCompressor\concatenateJsFiles().

cssFixRelativeUrlPaths (   $contents,
  $oldDir 
)
protected

Fixes the relative paths inside of url() references in CSS files

Parameters
string$contentsData to process
string$oldDirDirectory of the original file, relative to TYPO3_mainDir
Returns
string Processed data

Definition at line 498 of file ResourceCompressor.php.

References ResourceCompressor\findAndReplaceUrlPathsByRegex().

Referenced by ResourceCompressor\compressCssFile(), and ResourceCompressor\createMergedFile().

cssFixStatements (   $contents)
protected

Definition at line 552 of file ResourceCompressor.php.

Referenced by ResourceCompressor\createMergedFile().

findAndReplaceUrlPathsByRegex (   $contents,
  $regex,
  $newDir,
  $wrap = '|' 
)
protected

Finds and replaces all URLs by using a given regex

Parameters
string$contentsData to process
string$regexRegex used to find URLs in content
string$newDirPath to prepend to the original file
string$wrapWrap around replaced values
Returns
string Processed data

Definition at line 523 of file ResourceCompressor.php.

Referenced by ResourceCompressor\cssFixRelativeUrlPaths().

getFilenameFromMainDir (   $filename)
protected

Finds the relative path to a file, relative to the root path.

Parameters
string$filenamethe name of the file
Returns
string the path to the file relative to the root path ($this->rootPath)

Definition at line 420 of file ResourceCompressor.php.

References GeneralUtility\getFileAbsFileName(), GeneralUtility\getIndpEnv(), and GeneralUtility\resolveBackPath().

Referenced by ResourceCompressor\compressCssFile(), ResourceCompressor\compressJsFile(), ResourceCompressor\concatenateCssFiles(), and ResourceCompressor\concatenateJsFiles().

retrieveExternalFile (   $url)
protected

Retrieves an external file and stores it locally.

Parameters
string$url
Returns
string Temporary local filename for the externally-retrieved file

Definition at line 637 of file ResourceCompressor.php.

References GeneralUtility\writeFile().

Referenced by ResourceCompressor\createMergedFile().

returnFileReference (   $filename)
protected

Decides whether a client can deal with gzipped content or not and returns the according file name, based on HTTP_ACCEPT_ENCODING

Parameters
string$filenameFile name
Returns
string $filename suffixed with '.gzip' or not - dependent on HTTP_ACCEPT_ENCODING

Definition at line 622 of file ResourceCompressor.php.

References GeneralUtility\getIndpEnv(), and PathUtility\getRelativePath().

Referenced by ResourceCompressor\compressCssFile(), and ResourceCompressor\compressJsFile().

setRootPath (   $rootPath)

Sets absolute path to working directory

Parameters
string$rootPathAbsolute path
Returns
void

Definition at line 93 of file ResourceCompressor.php.

References ResourceCompressor\$rootPath.

Referenced by ResourceCompressor\__construct().

writeFileAndCompressed (   $filename,
  $contents 
)
protected

Writes $contents into file $filename together with a gzipped version into $filename.gz

Parameters
string$filenameTarget filename
string$contentsFile contents
Returns
void

Definition at line 605 of file ResourceCompressor.php.

References GeneralUtility\writeFile().

Referenced by ResourceCompressor\compressCssFile(), and ResourceCompressor\compressJsFile().

Member Data Documentation

$createGzipped = false
protected

Definition at line 42 of file ResourceCompressor.php.

$gzipCompressionLevel = -1
protected

Definition at line 47 of file ResourceCompressor.php.

$htaccessTemplate
protected
Initial value:
= '<FilesMatch "\\.(js|css)(\\.gzip)?$">
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 7 days"
</IfModule>
FileETag MTime Size
</FilesMatch>'

Definition at line 49 of file ResourceCompressor.php.

$rootPath = ''
protected

Definition at line 35 of file ResourceCompressor.php.

Referenced by ResourceCompressor\setRootPath().

$targetDirectory = 'typo3temp/assets/compressed/'
protected

Definition at line 30 of file ResourceCompressor.php.