77d201d81dd002d320aa542cd2094545f6f3b65d
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Sprite / SpriteManager.php
1 <?php
2 namespace TYPO3\CMS\Backend\Sprite;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * TYPO3 sprite manager, used in BE and in FE if a BE user is logged in.
21 *
22 * This class builds CSS definitions of registered icons, writes TCA definitions
23 * and registers sprite icons in a cache file.
24 *
25 * A configurable handler class does the business task.
26 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
27 */
28 class SpriteManager
29 {
30 /**
31 * @var string Directory for cached sprite informations
32 */
33 public static $tempPath = 'typo3temp/sprites/';
34
35 /**
36 * Is sprite manager initialized
37 */
38 protected static $isInitialized = false;
39
40 /**
41 * Initialize sprite manager.
42 * Loads registered sprite configuration from cache, or
43 * rebuilds new cache before registration.
44 *
45 * @return void
46 */
47 public static function initialize()
48 {
49 if (!static::isInitialized()) {
50 $cacheIdentifier = static::getCacheIdentifier();
51 /** @var $codeCache \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend */
52 $codeCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core');
53 if ($codeCache->has($cacheIdentifier)) {
54 $codeCache->requireOnce($cacheIdentifier);
55 } else {
56 static::buildSpriteDataAndCreateCacheEntry();
57 }
58 self::$isInitialized = true;
59 }
60 }
61
62 /**
63 * Whether the sprite manager is initialized.
64 *
65 * @return bool TRUE if sprite manager is initialized
66 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
67 */
68 public static function isInitialized()
69 {
70 GeneralUtility::deprecationLog(self::class . ' is deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8');
71 return self::$isInitialized;
72 }
73
74 /**
75 * Set up sprite icon data and create cache entry calling the registered generator.
76 *
77 * Stuff the compiled $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
78 * global into php code cache.
79 *
80 * @throws \RuntimeException
81 * @return void
82 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
83 */
84 protected static function buildSpriteDataAndCreateCacheEntry()
85 {
86 GeneralUtility::deprecationLog(self::class . ' is deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8');
87 $handlerClass = $GLOBALS['TYPO3_CONF_VARS']['BE']['spriteIconGenerator_handler'];
88 /** @var $handler \TYPO3\CMS\Backend\Sprite\SpriteIconGeneratorInterface */
89 $handler = GeneralUtility::makeInstance($handlerClass);
90 // Throw exception if handler class does not implement required interface
91 if (!$handler instanceof \TYPO3\CMS\Backend\Sprite\SpriteIconGeneratorInterface) {
92 throw new \RuntimeException('Class ' . $handlerClass . ' in $TYPO3_CONF_VARS[BE][spriteIconGenerator_handler] ' . ' does not implement ' . \TYPO3\CMS\Backend\Sprite\SpriteIconGeneratorInterface::class, 1294586333);
93 }
94 // Create temp directory if missing
95 if (!is_dir((PATH_site . self::$tempPath))) {
96 GeneralUtility::mkdir(PATH_site . self::$tempPath);
97 }
98 // Generate CSS and TCA files, build icon set register
99 $handler->generate();
100 // Get all icons registered from skins, merge with core icon list
101 $availableSkinIcons = (array)$GLOBALS['TBE_STYLES']['spriteIconApi']['coreSpriteImageNames'];
102 if (isset($GLOBALS['TBE_STYLES']['skins']) && is_array($GLOBALS['TBE_STYLES']['skins'])) {
103 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinData) {
104 $availableSkinIcons = array_merge($availableSkinIcons, (array)$skinData['availableSpriteIcons']);
105 }
106 }
107 // Merge icon names provided by the skin, with
108 // registered "complete sprites" and the handler class
109 $iconNames = array_merge($availableSkinIcons, (array)$GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'], $handler->getAvailableIconNames());
110 $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable'] = $iconNames;
111
112 $cacheFileContent = '$GLOBALS[\'TBE_STYLES\'][\'spriteIconApi\'][\'iconsAvailable\'] = ';
113 $cacheFileContent .= var_export($iconNames, true) . ';';
114 /** @var $codeCache \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend */
115 GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core')->set(static::getCacheIdentifier(), $cacheFileContent);
116 }
117
118 /**
119 * Get cache identifier for $GLOBALS['TBE_STYLES']['spriteIconApi']['iconsAvailable']
120 *
121 * @return string
122 */
123 protected static function getCacheIdentifier()
124 {
125 return 'sprites_' . sha1((TYPO3_version . PATH_site . 'spriteManagement'));
126 }
127
128 /**
129 * API for extensions to register own sprites.
130 *
131 * Get an array of icon names and the styleSheetFile with defined sprite icons.
132 * The stylesheet filename should contain the extension name to be unique.
133 *
134 * Naming conventions:
135 * - IconName: extensions-$extKey-$iconName
136 * - CSS class for loading the sprite: t3-icon-extensions-$extKey
137 * - CSS class for single icons: t3-icon-$extKey-$iconName
138 *
139 * @param array $icons Icon names
140 * @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
141 * @return void
142 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
143 */
144 public static function addIconSprite(array $icons, $styleSheetFile = '')
145 {
146 GeneralUtility::deprecationLog(self::class . ' is deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8');
147 $GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'] = array_merge((array)$GLOBALS['TBE_STYLES']['spritemanager']['spriteIconsAvailable'], $icons);
148 if ($styleSheetFile !== '') {
149 $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'][] = $styleSheetFile;
150 }
151 }
152
153 /**
154 * API for extensions to register new sprite images which can be used with
155 * \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('extensions-$extKey-iconName');
156 *
157 * @param array $icons Icons to be registered, $iconname => $iconFile, $iconFile must be relative to PATH_site
158 * @param string $extKey Extension key
159 * @return void
160 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
161 */
162 public static function addSingleIcons(array $icons, $extKey = '')
163 {
164 GeneralUtility::deprecationLog(self::class . ' is deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8');
165 foreach ($icons as $iconName => $iconFile) {
166 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['extensions-' . $extKey . '-' . $iconName] = $iconFile;
167 }
168 }
169
170 /**
171 * API to register new type icons for tables which use "typeicon_classes"
172 * Can be used to provide icons for "modules" in pages table
173 *
174 * @param string $table Table name to which the type icon should be added
175 * @param string $type Type column name of the table
176 * @param string $iconFile Icon filename, relative to PATH_typo3
177 * @return void
178 * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
179 */
180 public static function addTcaTypeIcon($table, $type, $iconFile)
181 {
182 GeneralUtility::deprecationLog(self::class . ' is deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8');
183 $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons']['tcarecords-' . $table . '-' . $type] = $iconFile;
184 if (is_array($GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'])) {
185 $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes'][$type] = 'tcarecords-' . $table . '-' . $type;
186 }
187 }
188 }