TYPO3  7.6
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ResourceCompressor Class Reference

Public Member Functions

 __construct ()
 
 setInitialPaths ()
 
 setRelativePath ($relativePath)
 
 setRootPath ($rootPath)
 
 setBackPath ($backPath)
 
 concatenateCssFiles (array $cssFiles, array $options=array())
 
 concatenateJsFiles (array $jsFiles)
 
 compressCssFiles (array $cssFiles)
 
 compressCssFile ($filename)
 
 compressJsFiles (array $jsFiles)
 
 compressJsFile ($filename)
 

Static Public Member Functions

static compressCssPregCallback ($matches)
 

Protected Member Functions

 setInitialBackPath ()
 
 setInitialRootPath ()
 
 setInitialRelativePath ()
 
 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/compressor/'
 
 $relativePath = ''
 
 $rootPath = ''
 
 $backPath = ''
 
 $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

__construct ( )

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 602 of file ResourceCompressor.php.

References $filename, and 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 418 of file ResourceCompressor.php.

References $filename, ResourceCompressor\compressCssString(), ResourceCompressor\cssFixRelativeUrlPaths(), PathUtility\dirname(), ResourceCompressor\getFilenameFromMainDir(), GeneralUtility\getUrl(), PathUtility\pathinfo(), GeneralUtility\resolveBackPath(), ResourceCompressor\returnFileReference(), 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 389 of file ResourceCompressor.php.

References $filename, and ResourceCompressor\compressCssFile().

static compressCssPregCallback (   $matches)
static

Callback function for preg_replace

See Also
compressCssFile
Parameters
array$matches
Returns
string the compressed string
Deprecated:
since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, not in use anymore

Definition at line 450 of file ResourceCompressor.php.

References elseif, and GeneralUtility\logDeprecatedFunction().

compressCssString (   $contents)
protected

Compress a CSS string by removing comments and whitespace characters

Parameters
string$contents
Returns
string

Definition at line 753 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 535 of file ResourceCompressor.php.

References $filename, ResourceCompressor\getFilenameFromMainDir(), GeneralUtility\getUrl(), 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 512 of file ResourceCompressor.php.

References ResourceCompressor\compressJsFile().

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

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 191 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 250 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 297 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 322 of file ResourceCompressor.php.

References $filename, ResourceCompressor\cssFixRelativeUrlPaths(), ResourceCompressor\cssFixStatements(), PathUtility\dirname(), GeneralUtility\getIndpEnv(), GeneralUtility\getUrl(), GeneralUtility\isFirstPartOfStr(), GeneralUtility\isOnCurrentHost(), GeneralUtility\isValidUrl(), GeneralUtility\resolveBackPath(), ResourceCompressor\retrieveExternalFile(), 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 308 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 620 of file ResourceCompressor.php.

References ResourceCompressor\findAndReplaceUrlPathsByRegex().

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

cssFixStatements (   $contents)
protected

Definition at line 675 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 646 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

Definition at line 561 of file ResourceCompressor.php.

References ResourceCompressor\$backPath, $filename, 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 734 of file ResourceCompressor.php.

References $filename, $url, GeneralUtility\getUrl(), and 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 718 of file ResourceCompressor.php.

References $filename, and GeneralUtility\getIndpEnv().

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

setBackPath (   $backPath)

Sets relative back path

Parameters
string$backPathBack path
Returns
void

Definition at line 174 of file ResourceCompressor.php.

References ResourceCompressor\$backPath.

Referenced by ResourceCompressor\setInitialBackPath().

setInitialBackPath ( )
protected

Sets relative back path

Returns
void

Definition at line 114 of file ResourceCompressor.php.

References ResourceCompressor\$backPath, $GLOBALS, and ResourceCompressor\setBackPath().

Referenced by ResourceCompressor\setInitialPaths().

setInitialPaths ( )
setInitialRelativePath ( )
protected

Sets relative path to PATH_site

Returns
void

Definition at line 136 of file ResourceCompressor.php.

References $GLOBALS, ResourceCompressor\$relativePath, and ResourceCompressor\setRelativePath().

Referenced by ResourceCompressor\setInitialPaths().

setInitialRootPath ( )
protected

Sets absolute path to working directory

Returns
void

Definition at line 125 of file ResourceCompressor.php.

References ResourceCompressor\$rootPath, and ResourceCompressor\setRootPath().

Referenced by ResourceCompressor\setInitialPaths().

setRelativePath (   $relativePath)

Sets relative path to PATH_site

Parameters
string$relativePathRelative path to site root
Returns
void

Definition at line 148 of file ResourceCompressor.php.

References ResourceCompressor\$relativePath.

Referenced by ResourceCompressor\setInitialRelativePath().

setRootPath (   $rootPath)

Sets absolute path to working directory

Parameters
string$rootPathAbsolute path
Returns
void

Definition at line 161 of file ResourceCompressor.php.

References ResourceCompressor\$rootPath.

Referenced by ResourceCompressor\setInitialRootPath().

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 701 of file ResourceCompressor.php.

References $filename, and GeneralUtility\writeFile().

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

Member Data Documentation

$backPath = ''
protected
$createGzipped = false
protected

Definition at line 52 of file ResourceCompressor.php.

$gzipCompressionLevel = -1
protected

Definition at line 57 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 59 of file ResourceCompressor.php.

$relativePath = ''
protected
$rootPath = ''
protected
$targetDirectory = 'typo3temp/compressor/'
protected

Definition at line 30 of file ResourceCompressor.php.