[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / SqlExpectedSchemaService.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Expected schema service
34 *
35 * @internal use in install tool only!
36 */
37 class SqlExpectedSchemaService {
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
41 * @inject
42 */
43 protected $objectManager = NULL;
44
45 /**
46 * Get expected schema array
47 *
48 * @return array Expected schema
49 */
50 public function getExpectedDatabaseSchema() {
51 /** @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService */
52 $schemaMigrationService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
53 // Raw concatenated ext_tables.sql and friends string
54 $expectedSchemaString = $this->getTablesDefinitionString();
55 // Remove comments
56 $cleanedExpectedSchemaString = implode(LF, $schemaMigrationService->getStatementArray($expectedSchemaString, TRUE, '^CREATE TABLE '));
57 $expectedSchema = $schemaMigrationService->getFieldDefinitions_fileContent($cleanedExpectedSchemaString);
58 return $expectedSchema;
59 }
60
61 /**
62 * Cycle through all loaded extensions and get full table definitions as concatenated string
63 *
64 * @param boolean $withStatic TRUE if sql from ext_tables_static+adt.sql should be loaded, too.
65 * @return string Concatenated SQL of loaded extensions ext_tables.sql
66 */
67 public function getTablesDefinitionString($withStatic = FALSE) {
68 $sqlString = array();
69
70 // Find all ext_tables.sql of loaded extensions
71 $loadedExtensionInformation = $GLOBALS['TYPO3_LOADED_EXT'];
72 foreach ($loadedExtensionInformation as $extensionConfiguration) {
73 if ((is_array($extensionConfiguration) || $extensionConfiguration instanceof \ArrayAccess) && $extensionConfiguration['ext_tables.sql']) {
74 $sqlString[] = GeneralUtility::getUrl($extensionConfiguration['ext_tables.sql']);
75 }
76 if ($withStatic
77 && (is_array($extensionConfiguration) || $extensionConfiguration instanceof \ArrayAccess)
78 && $extensionConfiguration['ext_tables_static+adt.sql']
79 ) {
80 $sqlString[] = GeneralUtility::getUrl($extensionConfiguration['ext_tables_static+adt.sql']);
81 }
82 }
83
84 // Add caching framework sql definition
85 $sqlString[] = $this->getCachingFrameworkRequiredDatabaseSchema();
86
87 // Add category registry sql definition
88 $sqlString[] = \TYPO3\CMS\Core\Category\CategoryRegistry::getInstance()->getDatabaseTableDefinitions();
89
90 return implode(LF . LF . LF . LF, $sqlString);
91 }
92
93 /**
94 * Get schema SQL of required cache framework tables.
95 *
96 * This method needs ext_localconf and ext_tables loaded!
97 *
98 * This is a hack, but there was no smarter solution with current cache configuration setup:
99 * ToolController sets the extbase caches to NullBackend to ensure the install tool does not
100 * cache anything. The CacheManager gets the required SQL from database backends only, so we need to
101 * temporarily 'fake' the standard db backends for extbase caches so they are respected.
102 *
103 * Additionally, the extbase_object cache is already in use and instantiated, and the CacheManager singleton
104 * does not allow overriding this definition. The only option at the moment is to 'fake' another cache with
105 * a different name, and then substitute this name in the sql content with the real one.
106 *
107 * @TODO: This construct needs to be improved. It does not recognise if some custom ext overwrote the extbase cache config
108 * @TODO: Solve this as soon as cache configuration is separated from ext_localconf / ext_tables
109 * @TODO: It might be possible to reduce this ugly construct by circumventing the 'singleton' of CacheManager by using 'new'
110 *
111 * @return string Cache framework SQL
112 */
113 public function getCachingFrameworkRequiredDatabaseSchema() {
114 $cacheConfigurationBackup = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'];
115 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['extbase_datamapfactory_datamap'] = array();
116 $extbaseObjectFakeName = uniqid('extbase_object');
117 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$extbaseObjectFakeName] = array();
118 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['extbase_reflection'] = array();
119 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['extbase_typo3dbbackend_tablecolumns'] = array();
120 /** @var \TYPO3\CMS\Core\Cache\CacheManager $cacheManager */
121 $cacheManager = $GLOBALS['typo3CacheManager'];
122 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
123 $cacheSqlString = \TYPO3\CMS\Core\Cache\Cache::getDatabaseTableDefinitions();
124 $sqlString = str_replace($extbaseObjectFakeName, 'extbase_object', $cacheSqlString);
125 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] = $cacheConfigurationBackup;
126 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
127
128 return $sqlString;
129 }
130 }