[TASK] Separate ext:statictemplates code from core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ExtensionManagementUtility.php
index 3e146ff..13262df 100644 (file)
@@ -4,7 +4,7 @@ namespace TYPO3\CMS\Core\Utility;
 /***************************************************************
  *  Copyright notice
  *
- *  (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
+ *  (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
  *  All rights reserved
  *
  *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -1288,7 +1288,6 @@ tt_content.' . $key . $prefix . ' {
 
        /**
         * Call this method to add an entry in the static template list found in sys_templates
-        * "static template files" are the modern equivalent (provided from extensions) to the traditional records in "static_templates"
         * FOR USE IN ext_localconf.php FILES
         *
         * @param string $extKey Is of course the extension key
@@ -1570,20 +1569,22 @@ tt_content.' . $key . $prefix . ' {
                $phpCodeToCache[] = '';
                // Iterate through loaded extensions and add ext_localconf content
                foreach ($extensionInformation as $extensionKey => $extensionDetails) {
-                       // Include a header per extension to make the cache file more readable
-                       $phpCodeToCache[] = '/**';
-                       $phpCodeToCache[] = ' * Extension: ' . $extensionKey;
-                       $phpCodeToCache[] = ' * File: ' . $extensionDetails['ext_localconf.php'];
-                       $phpCodeToCache[] = ' */';
-                       $phpCodeToCache[] = '';
-                       // Set $_EXTKEY and $_EXTCONF for this extension
-                       $phpCodeToCache[] = '$_EXTKEY = \'' . $extensionKey . '\';';
-                       $phpCodeToCache[] = '$_EXTCONF = $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXT\'][\'extConf\'][$_EXTKEY];';
-                       $phpCodeToCache[] = '';
-                       // Add ext_localconf.php content of extension
-                       $phpCodeToCache[] = trim(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extensionDetails['ext_localconf.php']));
-                       $phpCodeToCache[] = '';
-                       $phpCodeToCache[] = '';
+                       if (isset($extensionDetails['ext_localconf.php']) && $extensionDetails['ext_localconf.php']) {
+                               // Include a header per extension to make the cache file more readable
+                               $phpCodeToCache[] = '/**';
+                               $phpCodeToCache[] = ' * Extension: ' . $extensionKey;
+                               $phpCodeToCache[] = ' * File: ' . $extensionDetails['ext_localconf.php'];
+                               $phpCodeToCache[] = ' */';
+                               $phpCodeToCache[] = '';
+                               // Set $_EXTKEY and $_EXTCONF for this extension
+                               $phpCodeToCache[] = '$_EXTKEY = \'' . $extensionKey . '\';';
+                               $phpCodeToCache[] = '$_EXTCONF = $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXT\'][\'extConf\'][$_EXTKEY];';
+                               $phpCodeToCache[] = '';
+                               // Add ext_localconf.php content of extension
+                               $phpCodeToCache[] = trim(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extensionDetails['ext_localconf.php']));
+                               $phpCodeToCache[] = '';
+                               $phpCodeToCache[] = '';
+                       }
                }
                $phpCodeToCache = implode(LF, $phpCodeToCache);
                // Remove all start and ending php tags from content
@@ -1601,6 +1602,93 @@ tt_content.' . $key . $prefix . ' {
        }
 
        /**
+        * Wrapper for buildBaseTcaFromSingleFiles handling caching.
+        *
+        * This builds 'base' TCA that is later overloaded by ext_tables.php.
+        *
+        * Use a cache file if exists and caching is allowed.
+        *
+        * This is an internal method. It is only used during bootstrap and
+        * extensions should not use it!
+        *
+        * @param boolean $allowCaching Whether or not to load / create concatenated cache file
+        * @return void
+        * @access private
+        */
+       static public function loadBaseTca($allowCaching = TRUE) {
+               if ($allowCaching) {
+                       $cacheIdentifier = static::getBaseTcaCacheIdentifier();
+                       /** @var $codeCache \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend */
+                       $codeCache = $GLOBALS['typo3CacheManager']->getCache('cache_core');
+                       if ($codeCache->has($cacheIdentifier)) {
+                               $codeCache->requireOnce($cacheIdentifier);
+                       } else {
+                               self::buildBaseTcaFromSingleFiles();
+                               self::createBaseTcaCacheFile();
+                       }
+               } else {
+                       self::buildBaseTcaFromSingleFiles();
+               }
+       }
+
+       /**
+        * Find all Configuration/TCA/* files of extensions and create base TCA from it.
+        * The filename must be the table name in $GLOBALS['TCA'], and the content of
+        * the file should return an array with content of a specific table.
+        *
+        * @return void
+        * @see Extension core, cms, extensionmanager and others for examples.
+        */
+       static protected function buildBaseTcaFromSingleFiles() {
+               $GLOBALS['TCA'] = array();
+               foreach (self::getLoadedExtensionListArray() as $extensionName) {
+                       $tcaConfigurationDirectory = self::extPath($extensionName) . 'Configuration/TCA';
+                       if (is_dir($tcaConfigurationDirectory)) {
+                               $files = scandir($tcaConfigurationDirectory);
+                               foreach ($files as $file) {
+                                       if (
+                                               is_file($tcaConfigurationDirectory . '/' . $file)
+                                               && ($file !== '.')
+                                               && ($file !== '..')
+                                               && (substr($file, -4, 4) === '.php')
+                                       ) {
+                                               $tcaOfTable = require($tcaConfigurationDirectory . '/' . $file);
+                                               if (is_array($tcaOfTable)) {
+                                                       // TCA table name is filename without .php suffix, eg 'sys_notes', not 'sys_notes.php'
+                                                       $tcaTableName = substr($file, 0, -4);
+                                                       $GLOBALS['TCA'][$tcaTableName] = $tcaOfTable;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Cache base $GLOBALS['TCA'] to cache file to require the whole thing in one
+        * file for next access instead of cycling through all extensions again.
+        *
+        * @return void
+        */
+       static protected function createBaseTcaCacheFile() {
+               $phpCodeToCache = '$GLOBALS[\'TCA\'] = ';
+               $phpCodeToCache .= ArrayUtility::arrayExport($GLOBALS['TCA']);
+               $phpCodeToCache .= ';';
+               /** @var $codeCache \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend */
+               $codeCache = $GLOBALS['typo3CacheManager']->getCache('cache_core');
+               $codeCache->set(static::getBaseTcaCacheIdentifier(), $phpCodeToCache);
+       }
+
+       /**
+        * Cache identifier of base TCA cache entry.
+        *
+        * @return string
+        */
+       static protected function getBaseTcaCacheIdentifier() {
+               return 'tca_base_' . sha1((TYPO3_version . PATH_site . 'tca'));
+       }
+
+       /**
         * Execute all ext_tables.php files of loaded extensions.
         * The method implements an optionally used caching mechanism that concatenates all
         * ext_tables.php files in one file.
@@ -1673,21 +1761,23 @@ tt_content.' . $key . $prefix . ' {
                $phpCodeToCache[] = '';
                // Iterate through loaded extensions and add ext_tables content
                foreach ($extensionInformation as $extensionKey => $extensionDetails) {
-                       // Include a header per extension to make the cache file more readable
-                       $phpCodeToCache[] = '/**';
-                       $phpCodeToCache[] = ' * Extension: ' . $extensionKey;
-                       $phpCodeToCache[] = ' * File: ' . $extensionDetails['ext_tables.php'];
-                       $phpCodeToCache[] = ' */';
-                       $phpCodeToCache[] = '';
-                       // Set $_EXTKEY and $_EXTCONF for this extension
-                       $phpCodeToCache[] = '$_EXTKEY = \'' . $extensionKey . '\';';
-                       $phpCodeToCache[] = '$_EXTCONF = $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXT\'][\'extConf\'][$_EXTKEY];';
-                       $phpCodeToCache[] = '';
-                       // Add ext_tables.php content of extension
-                       $phpCodeToCache[] = trim(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extensionDetails['ext_tables.php']));
-                       $phpCodeToCache[] = '';
-                       $phpCodeToCache[] = '\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();';
-                       $phpCodeToCache[] = '';
+                       if (isset($extensionDetails['ext_tables.php']) && $extensionDetails['ext_tables.php']) {
+                               // Include a header per extension to make the cache file more readable
+                               $phpCodeToCache[] = '/**';
+                               $phpCodeToCache[] = ' * Extension: ' . $extensionKey;
+                               $phpCodeToCache[] = ' * File: ' . $extensionDetails['ext_tables.php'];
+                               $phpCodeToCache[] = ' */';
+                               $phpCodeToCache[] = '';
+                               // Set $_EXTKEY and $_EXTCONF for this extension
+                               $phpCodeToCache[] = '$_EXTKEY = \'' . $extensionKey . '\';';
+                               $phpCodeToCache[] = '$_EXTCONF = $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXT\'][\'extConf\'][$_EXTKEY];';
+                               $phpCodeToCache[] = '';
+                               // Add ext_tables.php content of extension
+                               $phpCodeToCache[] = trim(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extensionDetails['ext_tables.php']));
+                               $phpCodeToCache[] = '';
+                               $phpCodeToCache[] = '\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();';
+                               $phpCodeToCache[] = '';
+                       }
                }
                $phpCodeToCache = implode(LF, $phpCodeToCache);
                // Remove all start and ending php tags from content
@@ -1704,8 +1794,12 @@ tt_content.' . $key . $prefix . ' {
         * $TCA[$tableName]['ctrl']['dynamicConfigFile'] must be the
         * absolute path to a file.
         *
+        * Be aware that 'dynamicConfigFile' is obsolete, and all TCA
+        * table definitions should be moved to Configuration/TCA/tablename.php
+        * to be fully loaded automatically.
+        *
         * Example:
-        * dynamicConfigFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'Configuration/TCA/SysNote.php',
+        * dynamicConfigFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'SysNote.php',
         *
         * @return void
         * @throws \RuntimeException
@@ -1714,7 +1808,7 @@ tt_content.' . $key . $prefix . ' {
        static public function loadNewTcaColumnsConfigFiles() {
                global $TCA;
 
-               foreach ($TCA as $tableName => $configuration) {
+               foreach (array_keys($TCA) as $tableName) {
                        if (!isset($TCA[$tableName]['columns'])) {
                                $columnsConfigFile = $TCA[$tableName]['ctrl']['dynamicConfigFile'];
                                if ($columnsConfigFile) {
@@ -2099,4 +2193,4 @@ tt_content.' . $key . $prefix . ' {
        }
 
 }
-?>
\ No newline at end of file
+?>