Added Feature #14324: Add spriteGeneratorAPI to support the new sprite-icon-API ...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_spritemanager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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, it is initiated from BE and FE if BE-User ist active
31 * Its task will be to build css-definitions for registered Icons of Extensions,
32 * TCA-Tables and so on, so that they will be usuable through sprite-icon-api.
33 * An special configurable handler-class will process the "real" task so that the user
34 * may differ between details of generation and their caching.
35 *
36 * @author Steffen Ritter <info@steffen-ritter.net>
37 * @package TYPO3
38 * @subpackage t3lib
39 */
40 class t3lib_spriteManager {
41 /**
42 * @var string
43 */
44 public static $tempPath = 'typo3temp/sprites/';
45
46 /**
47 *@var t3lib_spriteManager_spriteIconGenerator
48 */
49 protected $handler = NULL;
50
51 /**
52 * @var array
53 */
54 protected $iconNames = array();
55
56 /**
57 * @var string the file name the current cache file
58 */
59 protected $tempFileName = '';
60
61 /**
62 * class constructor checks if cache has to be rebuild and initiates the rebuild
63 * instantiates the handler class
64 *
65 * @param boolean $regenerate with set to false, cache won't be regenerated if needed (useful for feediting)
66 * @return void
67 */
68 function __construct($regenerate = TRUE) {
69 // we check for existance of our targetDirectory
70 if (!is_dir(PATH_site . t3lib_spriteManager::$tempPath)) {
71 t3lib_div::mkdir(PATH_site . t3lib_spriteManager::$tempPath);
72 }
73 // create a fileName, the hash includes all icons and css-styles registered and the extlist
74 $this->tempFileName = PATH_site . t3lib_spriteManager::$tempPath .
75 md5(serialize($GLOBALS['TBE_STYLES']['spriteManager']) .
76 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']) . '.inc';
77 // if no cache-file for the current config ist present, regenerate it
78 if(!@file_exists($this->tempFileName)) {
79 // regenerate if allowed
80 if($regenerate) {
81 $handlerClass = (
82 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] ?
83 $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'] :
84 't3lib_spriteManager_simpleHandler'
85 );
86 $this->handler = t3lib_div::makeInstance($handlerClass);
87 // check if the handler could be loaded and implements the needed interface
88 if (!$this->handler || !($this->handler instanceof t3lib_spriteManager_spriteIconGenerator)) {
89 throw new Exception(
90 "class in TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] does not exist,
91 or does not implement t3lib_spriteManager_spriteIconGenerator"
92 );
93 }
94 // all went good? to go for rebuild
95 $this->rebuildCache();
96 } else {
97 // use old file if present
98 list($this->tempFileName) = t3lib_div::getFilesInDir(PATH_site . t3lib_spriteManager::$tempPath, 'inc', 1);
99 }
100 }
101 }
102
103 /**
104 * this method calls the main methods from the handler classes
105 * merges the results with the data from the skin, and cache it
106 *
107 * @return void
108 */
109 protected function rebuildCache() {
110 // ask the handlerClass to kindly rebuild our data
111 $this->handler->generate();
112
113 // get active skin Name
114 list($activeSkin) = $GLOBALS['TBE_STYLES']['skins'];
115 $activeSkin = $activeSkin['name'];
116
117 // merge icon names whith them provided by the skin,
118 // registered from "complete sprites" and the ones detected
119 // by the handlerclass
120 $this->iconNames = array_merge(
121 (array) $GLOBALS['TBE_STYLES']['skins'][$activeSkin]['availableSpriteIcons'],
122 (array) $GLOBALS['TBE_STYLES']['spriteManager']['spriteIconsAvailable'],
123 $this->handler->getAvailableIconNames()
124 );
125
126 // serialize found icons, and cache them to file
127 $cacheString = addslashes(serialize($this->iconNames));
128 $fileContent = '<?php $GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = unserialize(stripslashes(\'' . $cacheString . '\')); ?>';
129
130 // delete old cache files
131 $oldFiles = t3lib_div::getFilesInDir(PATH_site . t3lib_spriteManager::$tempPath, 'inc', 1);
132 foreach ($oldFiles aS $file) {
133 @unlink($file);
134 }
135 // and write the new one
136 t3lib_div::writeFile($this->tempFileName, $fileContent);
137 }
138
139
140 /**
141 * includes the generated cacheFile, if present
142 *
143 * @return void
144 */
145 public function loadCacheFile() {
146 if (@file_exists($this->tempFileName)) {
147 include_once($this->tempFileName);
148 }
149 }
150
151 /**
152 * if an extension has an pregenerated sprite, it might register it here.
153 * Giving the "available" iconNames and the styleSheetFile where the sprite icons are defined (make shure the css filename contains the extname to be unique).
154 * the iconnames and the stylesheet must follow the conventions as follows:
155 * IconName: extensions-$extKey-$iconName.
156 * Class for loading the sprite: t3-icon-extensions-$extKey
157 * Class for single icons: t3-icon-$extKey-$iconName
158 * NOTE: do not use this for skins, stylesheets of skins will be included automatically.
159 * Available icons of skins should be located manually (extTables) to $GLOBALS[TBE_STYLES][skins][skinName][availableIcons]
160 *
161 * @param array icons the names of the introduced icons
162 * @parram string $styleSheetFile the name of the styleshet file relative to PATH_site
163 */
164 public static function addIconSprite(array $icons, $styleSheetFile) {
165 $GLOBALS['TBE_STYLES']['spriteManager']['spriteIconsAvailable'] = array_merge(
166 $GLOBALS['TBE_STYLES']['spriteManager']['spriteIconsAvailable'],
167 $icons
168 );
169
170 $GLOBALS['TBE_STYLES']['spriteManager']['cssFiles'][] = $styleSheetFile;
171 }
172
173 /**
174 * will allow Ext-Developers to register their icons to get included in sprites,
175 * they may use them afterwards with t3lib_iconWorks::getSpriteIcon('extensions-$extKey-iconName');
176 * @param array $icons array which contains the adding icons array ( $iconname => $iconFile) $iconFile relative to PATH_typo3
177 * @param string $extKey string of the extension which adds the icons
178 * @return void
179 */
180 public static function addSingleIcons(array $icons, $extKey = '') {
181 foreach ($icons as $iconName => $iconFile) {
182 $GLOBALS['TBE_STYLES']['spriteManager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
183 }
184 }
185 }
186
187 ?>