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