[FEATURE] Add wincache as a cache backend
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_spritemanager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Steffen Ritter <info@steffen-ritter.net>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * TYPO3 sprite manager, used in BE and in FE if a BE user is logged in.
31 *
32 * This class builds CSS definitions of registered icons, writes TCA definitions
33 * and registers sprite icons in a cache file.
34 *
35 * A configurable handler class does the business task.
36 *
37 * @author Steffen Ritter <info@steffen-ritter.net>
38 * @package TYPO3
39 * @subpackage t3lib
40 */
41 class t3lib_SpriteManager {
42 /**
43 * @var string Directory for cached sprite informations
44 */
45 public static $tempPath = 'typo3temp/sprites/';
46
47 /**
48 * @var t3lib_spritemanager_SpriteIconGenerator Handler class instance
49 */
50 protected $handler = NULL;
51
52 /**
53 * @var array Register of valid icons
54 */
55 protected $iconNames = array();
56
57 /**
58 * @var string Name of current cache file
59 */
60 protected $tempFileName = '';
61
62 /**
63 * Check if the icon cache has to be rebuild, instantiate and call the handler class if so.
64 *
65 * @param boolean $allowRegeneration Suppress regeneration if FALSE (useful for feediting)
66 */
67 function __construct($allowRegeneration = TRUE) {
68 // Create temp directory if missing
69 if (!is_dir(PATH_site . self::$tempPath)) {
70 t3lib_div::mkdir(PATH_site . self::$tempPath);
71 }
72
73 // Create cache filename, the hash includes all icons, registered CSS styles registered and the extension list
74 $this->tempFileName = PATH_site . self::$tempPath .
75 md5(serialize($GLOBALS['TBE_STYLES']['spritemanager']) .
76 md5(serialize($GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'])) .
77 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']) . '.inc';
78
79 // Regenerate cache file if not already existing
80 if (!@file_exists($this->tempFileName)) {
81 if ($allowRegeneration) {
82 $handlerClass = (
83 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
84 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
85 't3lib_spritemanager_SimpleHandler'
86 );
87 $this->handler = t3lib_div::makeInstance($handlerClass);
88
89 // Throw exception if handler class does not implement required interface
90 if (!$this->handler || !($this->handler instanceof t3lib_spritemanager_SpriteIconGenerator)) {
91 throw new RuntimeException(
92 'Class in $TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] (' .
93 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] .
94 ') does not exist or does not implement t3lib_spritemanager_SpriteIconGenerator.',
95 1294586333
96 );
97 }
98
99 $this->rebuildCache();
100 } else {
101 // Set tempFileName to existing file if regeneration is not allowed
102 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
103 }
104 }
105 }
106
107 /**
108 * Call handler class, merge results with skin data and cache it.
109 *
110 * @return void
111 */
112 protected function rebuildCache() {
113 // Generate CSS and TCA files, build icon set register
114 $this->handler->generate();
115
116 // Get all icons registered from skins, merge with core icon list
117 $availableSkinIcons = (array) $GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
118 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinName => $skinData) {
119 $availableSkinIcons = array_merge($availableSkinIcons, (array) $skinData['availableSpriteIcons']);
120 }
121
122 // Merge icon names provided by the skin, with
123 // registered "complete sprites" and the handler class
124 $this->iconNames = array_merge(
125 $availableSkinIcons,
126 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
127 $this->handler->getAvailableIconNames()
128 );
129
130 // Create serialized cache data
131 $cacheString = addslashes(serialize($this->iconNames));
132 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
133
134 // Clean up cache directory
135 $oldFiles = t3lib_div::getFilesInDir(PATH_site . self::$tempPath, 'inc', TRUE);
136 foreach ($oldFiles as $file) {
137 @unlink($file);
138 }
139
140 // Write new cache file
141 t3lib_div::writeFile($this->tempFileName, $fileContent);
142 }
143
144 /**
145 * Include cache file if exists
146 *
147 * @return void
148 */
149 public function loadCacheFile() {
150 if (@file_exists($this->tempFileName)) {
151 include_once($this->tempFileName);
152 }
153 }
154
155 /**
156 * API for extensions to register own sprites.
157 *
158 * Get an array of icon names and the styleSheetFile with defined sprite icons.
159 * The stylesheet filename should contain the extension name to be unique.
160 *
161 * Naming conventions:
162 * - IconName: extensions-$extKey-$iconName
163 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
164 * - CSS class for single icons: t3-icon-$extKey-$iconName
165 *
166 * @param array Icon names
167 * @param string Stylesheet filename relative to PATH_typo3. Skins do not need to supply the $styleSheetFile, if the CSS file is within the registered stylesheet folders
168 * @return void
169 */
170 public static function addIconSprite(array $icons, $styleSheetFile = '') {
171 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
172 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
173 $icons
174 );
175 if ($styleSheetFile !== '') {
176 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
177 }
178 }
179
180 /**
181 * API for extensions to register new sprite images which can be used with
182 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
183 *
184 * @param array Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
185 * @param string Extension key
186 * @return void
187 */
188 public static function addSingleIcons(array $icons, $extKey = '') {
189 foreach ($icons as $iconName => $iconFile) {
190 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
191 }
192 }
193
194 /**
195 * API to register new type icons for tables which use "typeicon_classes"
196 * Can be used to provide icons for "modules" in pages table
197 *
198 * @param string Table name to which the type icon should be added
199 * @param string Type column name of the table
200 * @param string Icon filename, relative to PATH_typo3
201 * @return void
202 */
203 public static function addTcaTypeIcon($table, $type, $iconFile) {
204 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
205 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
206 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
207 }
208 }
209 }
210
211 ?>