Project

General

Profile

Bug #22546 ยป 14247.diff

Administrator Admin, 2010-04-29 00:10

View differences:

t3lib/class.t3lib_compressor.php (Revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Steffen Gebert (steffen@steffen-gebert.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Compressor
* This class can currently merge CSS files of the TYPO3 Backend.
*
* @author Steffen Gebert <steffen@steffen-gebert.de>
* @package TYPO3
* @subpackage t3lib
* $Id$
*/
class t3lib_compressor {
protected $skinStylesheetDirectories = array();
protected $targetDirectory = 'typo3temp/compressor/';
/**
* Constructor
*/
public function __construct() {
// we check for existance of our targetDirectory
if (!is_dir(PATH_site . $this->targetDirectory)) {
t3lib_div::mkdir(PATH_site . $this->targetDirectory);
}
if (TYPO3_MODE === 'BE') {
// we later have to decide, which stylesheets are included in all modules (so can be merged)
// or which only belong to the current opened module/page (and so can't be merged)
// add core stylesheets
$this->skinStylesheetDirectories[] = 'stylesheets/structure/';
$this->skinStylesheetDirectories[] = 'stylesheets/visual/';
// every extension registered in $TBE_STYLES['skins']
if (is_array($GLOBALS['TBE_STYLES']['skins'])) {
foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinKey => $skinOptions) {
$extRelPath = t3lib_extMgm::extRelPath($skinKey) ;
$this->skinStylesheetDirectories[] = $extRelPath . 'stylesheets/structure/';
$this->skinStylesheetDirectories[] = $extRelPath. 'stylesheets/visual/';
if (is_array($skinOptions['stylesheetDirectories'])) {
foreach($skinOptions['stylesheetDirectories'] as $additionalStylesheetDir) {
if (strlen($additionalStylesheetDir)) {
$this->skinStylesheetDirectories[] = $extRelPath . $additionalStylesheetDir;
}
}
}
}
}
}
}
/**
* Gets all files to process in $params and processes them. This function is called by PageRenderer
*
* @param array Files added to the PageRenderer
* @param t3lib_PageRenderer instance of t3lib_PageRenderer
* @return void
*/
public function concatenate(array &$params, t3lib_PageRenderer &$ref) {
// process the CSS files
$this->concatenateCss($params['cssFiles'], $ref);
}
/**
* Concatenates the cssFiles
*
* @param array $cssFiles CSS files added to the PageRenderer
* @param t3lib_PageRenderer $ref instance of t3lib_PageRenderer
* @return void
*/
protected function concatenateCss(array &$cssFiles, t3lib_PageRenderer &$ref) {
$filesToInclude = array();
// we add up the filenames, filemtimes and filsizes to later build a checksum over
// it and include it in the temporary file name
$unique = '';
foreach ($cssFiles as $filename => $options) {
// we remove BACK_PATH from $filename, so make it relative to TYPO3_mainDir
$filenameFromMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
if ($this->isSkinCssFile($filenameFromMainDir)) {
$filesToInclude[] = $filenameFromMainDir;
$unique .= $filenameFromMainDir . filemtime($filename) . filesize($filename);
// remove the file from the PageRenderer
unset($cssFiles[$filename]);
}
}
// name of the concatenated file, relative to PATH_site
$mergedFile = $this->targetDirectory . TYPO3_MODE . '-'. md5($unique) . '.css';
$this->createMergedFile(PATH_site . $mergedFile, $filesToInclude);
$concatenatedOptions = array(
'rel' => 'stylesheet',
'media' => 'all',
'title' => 'Merged TYPO3 Backend Stylesheets',
'compress' => TRUE,
);
$mergedFileRelative = $GLOBALS['BACK_PATH'] . '../' . $mergedFile;
// place the merged stylesheet on top of the stylesheets
$cssFiles = array_merge(array($mergedFileRelative => $concatenatedOptions), $cssFiles);
}
/**
* Creates a merged CSS file
*
* @param string $targetFile Filename of the merged file
* @param array $filesToInclude Files which should be merged, paths relative to TYPO3_mainDir
* @return void
*/
protected function createMergedFile($targetFile, array $filesToInclude) {
// if the file doesn't already exist, we create it
if (!file_exists($targetFile)) {
$concatenated = '';
// concatenate all the files together
foreach ($filesToInclude as $filename) {
$concatenated .= $this->processCssFile($filename);
//$concatenated .= $filename .chr(10);
}
if (strlen($concatenated)) {
t3lib_div::writeFile($targetFile, $concatenated);
}
}
}
/**
* Decides whether a CSS file comes from a skin, or not.
*
* @param string $filename Filename
* @return boolean File belongs to a skin or not
*/
protected function isSkinCssFile($filename) {
foreach ($this->skinStylesheetDirectories as $skinStyleetDirectory) {
// check, if $filename starts with $skinStylesheetDirectory (it's position 0, not FALSE!)
if (strpos($filename, $skinStyleetDirectory) === 0) {
return TRUE;
}
}
return FALSE;
}
/**
* Processes a single CSS file
* Reads it, removes comments, adjusts url() references etc., gives the contents back
*
* @param string $filename Filename
* @return string Processed file contents
*/
protected function processCssFile($filename) {
$contents = file_get_contents($GLOBALS['BACK_PATH'] . $filename);
$contents = $this->cssFixRelativeUrlPaths($contents, dirname($filename) . '/', $this->targetDirectory);
// remove comments
$contents = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $contents);
// remove tabs, spaces, newlines, etc
$contents = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $contents);
return $contents;
}
/**
* Fixes the relative paths inside of url() references in CSS files
*
* @param string $contents Data to process
* @param string $oldDir Directory of the originial file, relative to TYPO3_mainDir
* @param string $newDir Directory of the resulting file
* @return string Processed data
*/
protected function cssFixRelativeUrlPaths($contents, $oldDir, $newDir) {
$matches = array();
preg_match_all('/url[\s]*\([\'\"]?(.*)[\'\"]?\)/iU', $contents, $matches);
foreach ($matches[1] as $match) {
// remove '," or white-spaces around
$match = preg_replace('/[\"\'\s]/', '', $match);
$newPath = t3lib_div::resolveBackPath('../../' . TYPO3_mainDir . $oldDir . $match);
$contents = str_replace($match, $newPath, $contents);
}
return $contents;
}
}
?>
t3lib/class.t3lib_pagerenderer.php (Arbeitskopie)
$jsFooterFiles = '';
$noJS = FALSE;
// preRenderHook for possible manuipulation
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess'])) {
$params = array (
......
}
$jsLibs = $this->renderJsLibraries();
if ($this->compressCss || $this->compressJavascript) {
// do the file compression
$this->doCompress();
......
// then remove concatenated files from array and add the concatenated file
// extern concatination
if ($this->concatenateFiles && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['concatenateHandler']) {
// use extern concatenate routine
if ($this->concatenateFiles) {
$params = array (
'jsLibs' => &$this->jsLibs,
'jsFiles' => &$this->jsFiles,
......
'headerData' => &$this->headerData,
'footerData' => &$this->footerData,
);
t3lib_div::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['concatenateHandler'], $params, $this);
} else {
// own method, nothing implemented atm
if ($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['concatenateHandler']) {
// use extern concatenate routine
t3lib_div::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['concatenateHandler'], $params, $this);
} elseif (TYPO3_MODE === 'BE') {
$compressor = t3lib_div::makeInstance('t3lib_compressor');
$compressor->concatenate($params, $this);
}
}
}
typo3/template.php (Arbeitskopie)
TYPO3_mainDir . 'templates/template_page_backend.html'
);
$this->pageRenderer->setLanguage($GLOBALS['LANG']->lang);
$this->pageRenderer->enableConcatenateFiles();
}
return $this->pageRenderer;
}
    (1-1/1)