Project

General

Profile

Bug #23616 » 15814.diff

Steffen Kamper, 2011-03-30 10:03

View differences:

t3lib/class.t3lib_concatenate.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Steffen Kamper (steffen@typo3.org)
* 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!
***************************************************************/
/**
* Concatenator
* This merges JavaScript/CSS files or inline blocks into one file
*
* @author Steffen Kamper <steffen@typo3.org>
* @package TYPO3
* @subpackage t3lib
* $Id$
*/
class t3lib_Concatenate {
const JAVASCRIPT = '.js';
const CSS = '.css';
const UNKNOWN = '.bin';
protected $targetDirectory = 'typo3temp/concatenate/';
protected $scripts = array();
protected $filename;
protected $mimeType;
/**
* Constructor
*/
public function __construct($mimeType = self::JAVASCRIPT) {
// we check for existance of our targetDirectory
if (!is_dir(PATH_site . $this->targetDirectory)) {
t3lib_div::mkdir(PATH_site . $this->targetDirectory);
}
$this->mimeType = $mimeType;
$this->filename = uniqid('concat-') . '-all' . $mimeType;
}
/**
* Sets the mime type
*
* @param $mimeType
* @return void
*/
public function setMimeType($mimeType) {
if (in_array($mimeType, array(self::JAVASCRIPT, self::CSS, self::UNKNOWN))) {
$this->mimeType = $mimeType;
}
}
/**
* Gets the mime type
*
* @return string
*/
public function getMimeType() {
return $this->mimeType;
}
/**
* Sets the filename. Fileextension is dependent from mime type
*
* @param $filename
* @return void
*/
public function setFilename($filename) {
$this->filename = $filename . $this->mimeType;
}
/**
* Gets the filename
*
* @return string
*/
public function getFilename() {
return $this->filename;
}
/**
* Register a file for concatenation
*
* @param $file
* @return void
*/
public function registerFile($file) {
if (!$this->exists($file)) {
$this->scripts[] = array(
'key' => $file,
'type' => 'file',
'value' => $file
);
}
}
/**
* Register an inline block for concatenation
*
* @param string $name unique identifier
* @param string $inlineScript
* @return void
*/
public function registerInlineScript($name, $inlineScript) {
if (!$this->exists($name)) {
$this->scripts[] = array(
'key' => $name,
'type' => 'inline',
'value' => $inlineScript
);
}
}
/**
* Do the concatenation if file doesn't exists.
* Returns the filename of the concatenated file relative to site path
*
* @return string $filename
*/
public function concatenate() {
$filename = $this->targetDirectory . $this->filename;
if (!file_exists(PATH_site . $filename)) {
$fileHandler = fopen(PATH_site . $filename, 'w');
foreach ($this->scripts as $script) {
if ($script['type'] === 'inline') {
fwrite($fileHandler, $script['value']);
} else {
fwrite($fileHandler, file_get_contents(PATH_site . $script['value']));
}
}
fclose($fileHandler);
}
return $filename;
}
/**
* Checks the scripts array for existing key
*
* @param string $key
* @return bool
*/
protected function exists($key) {
foreach ($this->scripts as $script) {
if ($script['key'] === $key) {
return TRUE;
}
return FALSE;
}
}
}
?>
t3lib/core_autoload.php (working copy)
't3lib_cli' => PATH_t3lib . 'class.t3lib_cli.php',
't3lib_clipboard' => PATH_t3lib . 'class.t3lib_clipboard.php',
't3lib_compressor' => PATH_t3lib . 'class.t3lib_compressor.php',
't3lib_concatenate' => PATH_t3lib . 'class.t3lib_concatenate.php',
't3lib_cs' => PATH_t3lib . 'class.t3lib_cs.php',
't3lib_db' => PATH_t3lib . 'class.t3lib_db.php',
't3lib_db_preparedstatement' => PATH_t3lib . 'db/class.t3lib_db_preparedstatement.php',
(1-1/2)