[CLEANUP] Add phpDoc to properties in ext:backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Sprite / AbstractSpriteHandler.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 * An abstract class implementing SpriteIconGeneratorInterface.
21 * Provides base functionality for all handlers.
22 *
23 * @author Steffen Ritter <info@steffen-ritter.net>
24 */
25 abstract class AbstractSpriteHandler implements SpriteIconGeneratorInterface {
26
27 /**
28 * all "registered" icons available through sprite API will cumulated here
29 *
30 * @var array
31 */
32 protected $iconNames = array();
33
34 /**
35 * contains the content of the CSS file to write
36 *
37 * @var string
38 */
39 protected $styleSheetData = '';
40
41 /**
42 * path to CSS file for generated styles
43 *
44 * @var string
45 */
46 protected $cssTcaFile = '';
47
48 /**
49 * constructor just init's the temp-file-name
50 *
51 * @return void
52 */
53 public function __construct() {
54 // The file name is prefixed with "z" since the concatenator orders files per name
55 $this->cssTcaFile = PATH_site . SpriteManager::$tempPath . 'zextensions.css';
56 $this->styleSheetData = '/* Auto-Generated via ' . get_class($this) . ' */' . LF;
57 }
58
59 /**
60 * Loads all stylesheet files registered through
61 * \TYPO3\CMS\Backend\Sprite\SpriteManager::addIconSprite
62 *
63 * In fact the stylesheet-files are copied to \TYPO3\CMS\Backend\Sprite\SpriteManager::tempPath
64 * where they automatically will be included from via
65 * \TYPO3\CMS\Backend\Template\DocumentTemplate and
66 * \TYPO3\CMS\Core\Resource\ResourceCompressor
67 *
68 * @return void
69 */
70 protected function loadRegisteredSprites() {
71 // Saves which CSS Files are currently "allowed to be in place"
72 $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
73 // Process every registeres file
74 foreach ((array)$GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
75 $fileName = basename($file);
76 // File should be present
77 $allowedCssFilesinTempDir[] = $fileName;
78 // get-Cache Filename
79 $fileStatus = stat(PATH_site . $file);
80 $unique = md5($fileName . $fileStatus['mtime'] . $fileStatus['size']);
81 $cacheFile = PATH_site . SpriteManager::$tempPath . $fileName . $unique . '.css';
82 if (!file_exists($cacheFile)) {
83 copy(PATH_site . $file, $cacheFile);
84 }
85 }
86 // Get all .css files in dir
87 $cssFilesPresentInTempDir = GeneralUtility::getFilesInDir(PATH_site . SpriteManager::$tempPath, '.css', 0);
88 // and delete old ones which are not needed anymore
89 $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
90 foreach ($filesToDelete as $file) {
91 unlink(PATH_site . SpriteManager::$tempPath . $file);
92 }
93 }
94
95 /**
96 * Interface function. This will be called from the sprite manager to
97 * refresh all caches.
98 *
99 * @return void
100 */
101 public function generate() {
102 // Include registered Sprites
103 $this->loadRegisteredSprites();
104 // Cache results in the CSS file
105 GeneralUtility::writeFile($this->cssTcaFile, $this->styleSheetData);
106 }
107
108 /**
109 * Returns the detected icon-names which may be used through
110 * \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon.
111 *
112 * @return array all generated and registered sprite-icon-names, will be empty if there are none
113 */
114 public function getAvailableIconNames() {
115 return $this->iconNames;
116 }
117
118 /**
119 * this method creates sprite icon names for all tables in TCA (including their possible type-icons)
120 * where there is no "typeicon_classes" of this TCA table ctrl section
121 * (moved form \TYPO3\CMS\Backend\Utility\IconUtility)
122 *
123 * @return array Array as $iconName => $fileName
124 */
125 protected function collectTcaSpriteIcons() {
126 $tcaTables = array_keys($GLOBALS['TCA']);
127 $resultArray = array();
128 // Path (relative from typo3 dir) for skin-Images
129 if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
130 $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
131 } else {
132 $skinPath = '';
133 }
134 // check every table in the TCA, if an icon is needed
135 foreach ($tcaTables as $tableName) {
136 // This method is only needed for TCA tables where
137 // typeicon_classes are not configured
138 if (is_array($GLOBALS['TCA'][$tableName]) && !is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
139 $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
140 // Adding the default Icon (without types)
141 if (isset($tcaCtrl['iconfile'])) {
142 // In CSS we need a path relative to the css file
143 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
144 if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
145 $icon = $tcaCtrl['iconfile'];
146 } else {
147 $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
148 }
149 $icon = GeneralUtility::resolveBackPath($icon);
150 $resultArray['tcarecords-' . $tableName . '-default'] = $icon;
151 }
152 // If records types are available, register them
153 if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
154 foreach ($tcaCtrl['typeicons'] as $type => $icon) {
155 // In CSS we need a path relative to the css file
156 // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
157 if (strpos($icon, '/') === FALSE) {
158 $icon = $skinPath . 'gfx/i/' . $icon;
159 }
160 $icon = GeneralUtility::resolveBackPath($icon);
161 $resultArray['tcarecords-' . $tableName . '-' . $type] = $icon;
162 }
163 }
164 }
165 }
166 return $resultArray;
167 }
168
169 }