[TASK] Deprecate usage of $GLOBALS['typo3CacheManager']
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / ClearCacheService.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 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 text file 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 * Basic service to clear caches within the install tool.
34 * This is NOT an API class, it is for internal use in the install tool only.
35 */
36 class ClearCacheService {
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
40 * @inject
41 */
42 protected $objectManager = NULL;
43
44 /**
45 * This clear cache implementation follows a pretty brutal approach.
46 * Goal is to reliably get rid of cache entries, even if some broken
47 * extension is loaded that would kill the backend 'clear cache' action.
48 *
49 * Therefor this method "knows" implementation details of the cache
50 * framework and uses them to clear all file based cache (typo3temp/Cache)
51 * and database caches (tables prefixed with cf_) manually.
52 *
53 * After that ext_tables and ext_localconf of extensions are loaded, those
54 * may register additional caches in the caching framework with different
55 * backend, and will then clear them with the usual flush() method.
56 *
57 * @return void
58 */
59 public function clearAll() {
60 // Delete typo3temp/Cache
61 GeneralUtility::rmdir(PATH_site . 'typo3temp/Cache', TRUE);
62
63 $bootstrap = \TYPO3\CMS\Core\Core\Bootstrap::getInstance();
64 $bootstrap->reinitializeClassLoaderAndCachesAndPackageManagement();
65
66 // Get all table names starting with 'cf_' and truncate them
67 $database = $this->getDatabaseInstance();
68 $tables = $database->admin_get_tables();
69 foreach ($tables as $table) {
70 $tableName = $table['Name'];
71 if (substr($tableName, 0, 3) === 'cf_') {
72 $database->exec_TRUNCATEquery($tableName);
73 }
74 }
75
76 // From this point on, the code may fatal, if some broken extension is loaded.
77
78 // Use bootstrap to load all ext_localconf and ext_tables
79 $bootstrap
80 ->loadTypo3LoadedExtAndExtLocalconf(FALSE)
81 ->applyAdditionalConfigurationSettings()
82 ->initializeTypo3DbGlobal()
83 ->loadExtensionTables(FALSE);
84
85 // The cache manager is already instantiated in the install tool
86 // with some hacked settings to disable caching of extbase and fluid.
87 // We want a "fresh" object here to operate on a different cache setup.
88 // cacheManager implements SingletonInterface, so the only way to get a "fresh"
89 // instance is by circumventing makeInstance and/or the objectManager and
90 // using new directly!
91 $cacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
92 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
93 // Cache manager needs cache factory. cache factory injects itself to manager in __construct()
94 new \TYPO3\CMS\Core\Cache\CacheFactory('production', $cacheManager);
95
96 $cacheManager->flushCaches();
97 }
98
99 /**
100 * Get a database instance.
101 *
102 * @TODO: This method is a copy from AbstractAction. Review them and extract to service
103 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
104 */
105 protected function getDatabaseInstance() {
106 static $database;
107 if (!is_object($database)) {
108 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
109 $database = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
110 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
111 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
112 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
113 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
114 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
115 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
116 $database->connectDB();
117 }
118 return $database;
119 }
120 }