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