Cleanup #16002: Refactoring of t3lib_spritemanager_SimpleHandler (Thanks to Steffen...
authorErnesto Baschny <ernst@cron-it.de>
Sat, 16 Oct 2010 22:09:22 +0000 (22:09 +0000)
committerErnesto Baschny <ernst@cron-it.de>
Sat, 16 Oct 2010 22:09:22 +0000 (22:09 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@9112 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/core_autoload.php
t3lib/spritemanager/class.t3lib_spritemanager_abstracthandler.php [new file with mode: 0644]
t3lib/spritemanager/class.t3lib_spritemanager_simplehandler.php

index bce002d..4916587 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -25,6 +25,7 @@
        * Cleanup #16009: Refactor sprite icon API, to have more elementary functions instead of big ones (Thanks to Steffen Ritter)
        * Added feature #15966: Adds mode to debug the backend ($TYPO3_CONF_VARS['BE']['debug']) (Thanks to Steffen Kamper)
        * Fixed bug #16016: Scheduler: Since new CSH handling (#15990), no icon in scheduler works anymore
+       * Cleanup #16002: Refactoring of t3lib_spritemanager_SimpleHandler (Thanks to Steffen Ritter)
 
 2010-10-16  Steffen Kamper  <steffen@typo3.org>
 
index 2ae936e..3c6ba1b 100644 (file)
@@ -140,6 +140,7 @@ $t3libClasses = array(
        't3lib_spritemanager_spritegenerator' => PATH_t3lib . 'spritemanager/class.t3lib_spritemanager_spritegenerator.php',
        't3lib_spritemanager_spriteicongenerator' => PATH_t3lib . 'interfaces/interface.t3lib_spritemanager_spriteicongenerator.php',
        't3lib_spritemanager_simplehandler' => PATH_t3lib . 'spritemanager/class.t3lib_spritemanager_simplehandler.php',
+       't3lib_spritemanager_abstracthandler' => PATH_t3lib . 'spritemanager/class.t3lib_spritemanager_abstracthandler.php',
        't3lib_extjs_extdirectdebug' => PATH_t3lib . 'extjs/class.t3lib_extjs_extdirectdebug.php',
 );
 
diff --git a/t3lib/spritemanager/class.t3lib_spritemanager_abstracthandler.php b/t3lib/spritemanager/class.t3lib_spritemanager_abstracthandler.php
new file mode 100644 (file)
index 0000000..d50053c
--- /dev/null
@@ -0,0 +1,185 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2010 Steffen Ritter <info@steffen-ritter.net>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*  A copy is found in the textfile GPL.txt and important notices to the license
+*  from the author is found in LICENSE.txt distributed with these scripts.
+*
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+
+/**
+ * An abstract class implementing t3lib_spritemanager_SpriteIconGenerator.
+ * Provides base functionality for all handlers.
+ *
+ * @author     Steffen Ritter <info@steffen-ritter.net>
+ * @package TYPO3
+ * @subpackage t3lib
+ */
+abstract class t3lib_spritemanager_AbstractHandler implements t3lib_spritemanager_SpriteIconGenerator {
+       /**
+        * all "registered" icons available through sprite API will cmmulated here
+        * @var array
+        */
+       protected $iconNames = array();
+
+       /**
+        * contains the content of the CSS file to write
+        * @var String
+        */
+       protected $styleSheetData = '';
+
+       /**
+        * path to CSS file for generated styles
+        * @var String
+        */
+       protected $cssTcaFile = "";
+
+       /**
+        * constructor just init's the temp-file-name
+        * @return void
+        */
+       function __construct() {
+                       // the file name is prefixed with "z" since the concatenator orders files per name
+               $this->cssTcaFile = PATH_site . t3lib_SpriteManager::$tempPath . 'zextensions.css';
+               $this->styleSheetData = '/* Auto-Generated via ' . get_class($this) . ' */' . LF;
+       }
+
+       /**
+        * Loads all stylesheet files registered through
+        * t3lib_SpriteManager::::addIconSprite
+        *
+        * In fact the stylesheet-files are copied to t3lib_SpriteManager::tempPath
+        * where they automatically will be included from via template.php and
+        * t3lib_compressor.
+        *
+        * @return void
+        */
+       protected function loadRegisteredSprites() {
+                       // saves which CSS Files are currently "allowed to be in place"
+               $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
+                       // process every registeres file
+               foreach ((array) $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
+                       $fileName = basename($file);
+                               // file should be present
+                       $allowedCssFilesinTempDir[] = $fileName;
+                               // get-Cache Filename
+                       $unique = md5($fileName . filemtime(PATH_site . $file) . filesize(PATH_site . $file));
+                       $cacheFile = PATH_site . t3lib_SpriteManager::$tempPath . $fileName . $unique . '.css';
+                       if (!file_exists($cacheFile)) {
+                               copy(PATH_site . $file, $cacheFile);
+                       }
+               }
+                       // get all .css files in dir
+               $cssFilesPresentInTempDir = t3lib_div::getFilesInDir(PATH_site . t3lib_SpriteManager::$tempPath , '.css', 0);
+                       // and delete old ones which are not needed anymore
+               $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
+               foreach ($filesToDelete as $file) {
+                       unlink(PATH_site . t3lib_SpriteManager::$tempPath . $file);
+               }
+       }
+
+       /**
+        * Interface function. This will be called from the sprite manager to
+        * refresh all caches.
+        *
+        * @return void
+        */
+       public function generate() {
+                       // include registered Sprites
+               $this->loadRegisteredSprites();
+
+                       // cache results in the CSS file
+               t3lib_div::writeFile($this->cssTcaFile, $this->styleSheetData);
+       }
+
+       /**
+        * Returns the detected icon-names which may be used through t3lib_iconWorks::getSpriteIcon.
+        *
+        * @return array all generated and registred sprite-icon-names, will be empty if there are none
+        */
+       public function getAvailableIconNames() {
+               return $this->iconNames;
+       }
+
+       /**
+        * this method creates sprite icon names for all tables in TCA (including their possible type-icons)
+        * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
+        *
+        * @return array array as $iconName => $fileName
+        */
+       protected function collectTcaSpriteIcons() {
+               $tcaTables = array_keys($GLOBALS['TCA']);
+
+               $resultArray = array();
+
+                       // path (relative from typo3 dir) for skin-Images
+               if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
+                       $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
+               } else {
+                       $skinPath = '';
+               }
+
+                       // check every table in the TCA, if an icon is needed
+               foreach ($tcaTables as $tableName) {
+
+                               // this method is only needed for TCA tables where
+                               // typeicon_classes are not configured
+                       if (!is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
+                               $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
+
+                                       // adding the default Icon (without types)
+                               if (isset($tcaCtrl['iconfile'])) {
+                                               // in CSS wie need a path relative to the css file
+                                               // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
+                                       if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
+                                               $icon = $tcaCtrl['iconfile'];
+                                       } else {
+                                               $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
+                                       }
+
+                                       $icon = t3lib_div::resolveBackPath($icon);
+                                       $resultArray['tcarecords-' . $tableName . '-default'] = $icon;
+
+                               }
+
+                                       // if records types are available, register them
+                               if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
+                                       foreach ($tcaCtrl['typeicons'] as $type => $icon) {
+
+                                                       // in CSS wie need a path relative to the css file
+                                                       // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
+                                               if (strpos($icon, '/') === FALSE) {
+                                                       $icon = $skinPath . 'gfx/i/' . $icon;
+                                               }
+
+                                               $icon = t3lib_div::resolveBackPath($icon);
+
+                                               $resultArray['tcarecords-' . $tableName . '-' . $type] = $icon;
+                                       }
+                               }
+                       }
+               }
+               return $resultArray;
+       }
+}
+
+?>
index d542602..3a39f85 100644 (file)
  * @package TYPO3
  * @subpackage t3lib
  */
-class t3lib_spritemanager_SimpleHandler implements t3lib_spritemanager_SpriteIconGenerator {
-       /**
-        * all "registered" Icons available through sprite-api will cumuluated within
-        * @var array
-        */
-       protected $iconNames = array();
-
-       /**
-        * contains the content of the CSS file to write
-        * @var String
-        */
-       protected $styleSheetData = "/* Auto-Generated via t3lib_spritemanager_SimpleHandler */\n";
-
-       /**
-        * css-template for each sprite-icon of an tca-record-symbol
-        * @var String
-        */
-       protected $styleSheetTemplateTca = '
-.t3-icon-###TABLE###-###TYPE### {
-       background-position: 0px 0px !important;
-       background-image: url(\'###IMAGE###\') !important;
-}
-       ';
+class t3lib_spritemanager_SimpleHandler extends t3lib_spritemanager_AbstractHandler {
 
        /**
         * css template for single Icons registered by extension authors
@@ -73,18 +51,11 @@ class t3lib_spritemanager_SimpleHandler implements t3lib_spritemanager_SpriteIco
        ';
 
        /**
-        * path to css file for generated styles
-        * @var String
-        */
-       protected $cssTcaFile = "";
-
-       /**
         * constructor just init's the temp-file-name
         * @return void
         */
        function __construct() {
-                       // the file name is prefixed with "z" since the concatenator orders files per name
-               $this->cssTcaFile = PATH_site . t3lib_SpriteManager::$tempPath . 'zextensions.css';
+               parent::__construct();
        }
 
        /**
@@ -94,33 +65,32 @@ class t3lib_spritemanager_SimpleHandler implements t3lib_spritemanager_SpriteIco
         * @return void
         */
        public function generate() {
-                       // generate Icons for all TCA tables
-               $this->buildTcaSpriteIcons();
 
                        // generate IconData for single Icons registered
-               $this->buildExtensionSpriteIcons();
+               $this->buildCssAndRegisterIcons();
 
-                       // include registered Sprites
-               $this->loadRegisteredSprites();
-
-                       // cache results in the CSS file
-               t3lib_div::writeFile($this->cssTcaFile, $this->styleSheetData);
+               parent::generate();
        }
 
 
        /**
         * This function builds an css class for every single icon registered via
         * t3lib_SpriteManager::addSingleIcons to use them via t3lib_iconWorks::getSpriteIcon
+        * and TCA-Icons for "classic" record Icons to be uses via t3lib_iconWorks::getSpriteIconForRecord
         * In the simpleHandler the icon just will be added as css-background-image.
         *
         * @return void
         */
-       protected function buildExtensionSpriteIcons() {
-                       // backpath from the stylesheet file ($cssTcaFile) to typo3 dir
+       protected function buildCssAndRegisterIcons() {
+                       // backpath from the stylesheet file ($cssTcaFile) to PATH_site dir
                        // in order to set the background-image URL paths correct
                $iconPath = '../../' . TYPO3_mainDir;
 
-               foreach((array) $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons'] as $iconName => $iconFile) {
+               $iconsToProcess = array_merge(
+                       (array) $GLOBALS['TBE_STYLES']['spritemanager']['singleIcons'],
+                       $this->collectTcaSpriteIcons()
+               );
+               foreach ($iconsToProcess as $iconName => $iconFile) {
                        $css = str_replace('###NAME###', str_replace(
                                array('extensions-', 'tcarecords-'), array('', ''), $iconName
                        ), $this->styleSheetTemplateExtIcons);
@@ -130,124 +100,6 @@ class t3lib_spritemanager_SimpleHandler implements t3lib_spritemanager_SpriteIco
                        $this->styleSheetData .= $css;
                }
        }
-
-       /**
-        * Loads all StyleSheets Files registered through
-        * t3lib_SpriteManager::::addIconSprite
-        *
-        * In fact the stylesheet-files are copied to t3lib_SpriteManager::tempPath
-        * where they automatically will be included from via template.php and
-        * t3lib_compressor.
-        *
-        * @return void
-        */
-       protected function loadRegisteredSprites() {
-                       // saves which CSS Files are currently "allowed to be in place"
-               $allowedCssFilesinTempDir = array(basename($this->cssTcaFile));
-                       // process every registeres file
-               foreach((array) $GLOBALS['TBE_STYLES']['spritemanager']['cssFiles'] as $file) {
-                       $fileName = basename($file);
-                               // file should be present
-                       $allowedCssFilesinTempDir[] = $fileName;
-                               // get-Cache Filename
-                       $unique = md5($fileName . filemtime(PATH_site . $file) . filesize(PATH_site . $file));
-                       $cacheFile = PATH_site . t3lib_SpriteManager::$tempPath . $fileName . $unique . '.css';
-                       if(!file_exists($cacheFile)) {
-                               copy(PATH_site . $file, $cacheFile);
-                       }
-               }
-                       // get all .css files in dir
-               $cssFilesPresentInTempDir = t3lib_div::getFilesInDir(PATH_site . t3lib_SpriteManager::$tempPath , '.css', 0);
-                       // and delete old ones which are not needed anymore
-               $filesToDelete = array_diff($cssFilesPresentInTempDir, $allowedCssFilesinTempDir);
-               foreach ($filesToDelete as $file) {
-                       unlink(PATH_site . t3lib_SpriteManager::$tempPath . $file);
-               }
-       }
-
-       /**
-        * public-interface function: getter for iconNames
-        * will return the detected icon-names which may be used throug t3lib_iconWorks::getSpriteIcon
-        *
-        * @return array all generated and registred sprite-icon-names
-        */
-       public function getAvailableIconNames() {
-               return $this->iconNames;
-       }
-
-       /**
-        * this method creates SpriteIcon names for all tables in TCA (including their possible type-icons)
-        * where there is no "typeicon_classes" of this TCA table ctrl section (moved form t3lib_iconWorks)
-        *
-        * @return void
-        */
-       protected function buildTcaSpriteIcons() {
-               $tcaTables = array_keys($GLOBALS['TCA']);
-
-                       // delete old tempFiles
-               @unlink($this->cssTcaFile);
-
-                       // backpath from the stylesheet file ($cssTcaFile) to typo3 dir
-                       // in order to set the background-image URL paths correct
-               $iconPath = '../../' . TYPO3_mainDir;
-
-                       // path (relative from typo3 dir) for skin-Images
-               if (isset($GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'])) {
-                       $skinPath = $GLOBALS['TBE_STYLES']['skinImgAutoCfg']['relDir'];
-               } else {
-                       $skinPath = '';
-               }
-
-                       // check every table in the TCA, if an icon is needed
-               foreach ($tcaTables as $tableName) {
-
-                               // this method is only needed for TCA tables where
-                               // typeicon_classes are not configured
-                       if (!is_array($GLOBALS['TCA'][$tableName]['ctrl']['typeicon_classes'])) {
-                               $tcaCtrl = $GLOBALS['TCA'][$tableName]['ctrl'];
-
-                               $template = str_replace('###TABLE###', $tableName, $this->styleSheetTemplateTca);
-
-                                       // adding the default Icon (without types)
-                               if (isset($tcaCtrl['iconfile'])) {
-                                               // in CSS wie need a path relative to the css file
-                                               // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
-                                       if (strpos($tcaCtrl['iconfile'], '/') !== FALSE) {
-                                               $icon = $tcaCtrl['iconfile'];
-                                       } else {
-                                               $icon = $skinPath . 'gfx/i/' . $tcaCtrl['iconfile'];
-                                       }
-
-                                       $icon = t3lib_div::resolveBackPath($iconPath . $icon);
-
-                                               // saving default icon
-                                       $stylesString = str_replace('###TYPE###', 'default', $template);
-                                       $stylesString = str_replace('###IMAGE###', $icon, $stylesString);
-                                       $this->styleSheetData .= $stylesString;
-                                       $this->iconNames[] = 'tcarecords-' . $tableName . '-default';
-                               }
-
-                                       // if records types are available, register them
-                               if (isset($tcaCtrl['typeicon_column']) && is_array($tcaCtrl['typeicons'])) {
-                                       foreach ($tcaCtrl['typeicons'] as $type => $icon) {
-
-                                                       // in CSS wie need a path relative to the css file
-                                                       // [TCA][ctrl][iconfile] defines icons without path info to reside in gfx/i/
-                                               if (strpos($icon, '/') === FALSE) {
-                                                       $icon = $skinPath . 'gfx/i/' . $icon;
-                                               }
-
-                                               $icon = t3lib_div::resolveBackPath($iconPath . $icon);
-                                               $stylesString = str_replace('###TYPE###', $type, $template);
-                                               $stylesString = str_replace('###IMAGE###', $icon, $stylesString);
-                                                       // saving type icon
-                                               $this->styleSheetData .= $stylesString;
-                                               $this->iconNames[] = 'tcarecords-' . $tableName . '-' . $type;
-                                       }
-                               }
-                       }
-               }
-       }
 }
 
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/spritemanager/class.t3lib_spritemanager_simplehandler.php'])        {