[TASK] Use simple file backend for core php cache
[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 * Initialize sprite manager.
49 * Loads registered sprite configuration from cache, or
50 * rebuilds new cache before registration.
51 *
52 * @return void
53 */
54 public static function initialize() {
55 $cacheIdentifier = static::getCacheIdentifier();
56 /** @var $codeCache t3lib_cache_frontend_PhpFrontend */
57 $codeCache = $GLOBALS['typo3CacheManager']->getCache('cache_core');
58 if ($codeCache->has($cacheIdentifier)) {
59 $codeCache->requireOnce($cacheIdentifier);
60 } else {
61 static::createSpriteCache();
62 $codeCache->requireOnce($cacheIdentifier);
63 }
64 }
65
66 /**
67 * Compile sprite icon cache by calling the registered generator.
68 *
69 * Stuff the compiled $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
70 * global into php code cache
71 *
72 * @throws RuntimeException
73 * @return void
74 */
75 protected static function createSpriteCache() {
76 $handlerClass = $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'];
77 /** @var $handler t3lib_spritemanager_SpriteIconGenerator */
78 $handler = t3lib_div::makeInstance($handlerClass);
79
80 // Throw exception if handler class does not implement required interface
81 if (!$handler instanceof t3lib_spritemanager_SpriteIconGenerator) {
82 throw new RuntimeException(
83 'Class ' . $handlerClass . ' in $TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] ' .
84 ' does not implement t3lib_spritemanager_SpriteIconGenerator',
85 1294586333
86 );
87 }
88
89 // Create temp directory if missing
90 if (!is_dir(PATH_site . self::$tempPath)) {
91 t3lib_div::mkdir(PATH_site . self::$tempPath);
92 }
93
94 // Generate CSS and TCA files, build icon set register
95 $handler->generate();
96
97 // Get all icons registered from skins, merge with core icon list
98 $availableSkinIcons = (array) $GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
99 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinData) {
100 $availableSkinIcons = array_merge($availableSkinIcons, (array) $skinData['availableSpriteIcons']);
101 }
102
103 // Merge icon names provided by the skin, with
104 // registered "complete sprites" and the handler class
105 $iconNames = array_merge(
106 $availableSkinIcons,
107 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
108 $handler->getAvailableIconNames()
109 );
110
111 $cacheString = addslashes(serialize($iconNames));
112 $cacheFileContent = '$GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\'));';
113
114 /** @var $codeCache t3lib_cache_frontend_PhpFrontend */
115 $GLOBALS['typo3CacheManager']->getCache('cache_core')->set(
116 static::getCacheIdentifier(),
117 $cacheFileContent
118 );
119 }
120
121 /**
122 * Get cache identifier for $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
123 *
124 * @return string
125 */
126 protected static function getCacheIdentifier() {
127 return 'sprites_' . sha1(TYPO3_version . PATH_site . 'spriteManagement');
128 }
129
130 /**
131 * API for extensions to register own sprites.
132 *
133 * Get an array of icon names and the styleSheetFile with defined sprite icons.
134 * The stylesheet filename should contain the extension name to be unique.
135 *
136 * Naming conventions:
137 * - IconName: extensions-$extKey-$iconName
138 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
139 * - CSS class for single icons: t3-icon-$extKey-$iconName
140 *
141 * @param array $icons Icon names
142 * @param string $styleSheetFile Stylesheet filename relative to PATH_typo3. Skins do not need to supply the $styleSheetFile, if the CSS file is within the registered stylesheet folders
143 * @return void
144 */
145 public static function addIconSprite(array $icons, $styleSheetFile = '') {
146 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge(
147 (array) $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'],
148 $icons
149 );
150 if ($styleSheetFile !== '') {
151 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
152 }
153 }
154
155 /**
156 * API for extensions to register new sprite images which can be used with
157 * t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
158 *
159 * @param array $icons Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
160 * @param string $extKey Extension key
161 * @return void
162 */
163 public static function addSingleIcons(array $icons, $extKey = '') {
164 foreach ($icons as $iconName => $iconFile) {
165 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
166 }
167 }
168
169 /**
170 * API to register new type icons for tables which use "typeicon_classes"
171 * Can be used to provide icons for "modules" in pages table
172 *
173 * @param string $table Table name to which the type icon should be added
174 * @param string $type Type column name of the table
175 * @param string $iconFile Icon filename, relative to PATH_typo3
176 * @return void
177 */
178 public static function addTcaTypeIcon($table, $type, $iconFile) {
179 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
180 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
181 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
182 }
183 }
184 }
185 ?>