[TASK] Re-work/simplify copyright header in PHP files - Part 2
[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::rmdir(PATH_site . 'typo3temp/Cache', TRUE);
49
50 $bootstrap = \TYPO3\CMS\Core\Core\Bootstrap::getInstance();
51 $bootstrap->unregisterClassLoader();
52
53 \TYPO3\CMS\Core\Cache\Cache::flagCachingFrameworkForReinitialization();
54
55 $bootstrap
56 ->initializeClassLoader()
57 ->initializeCachingFramework()
58 ->initializeClassLoaderCaches()
59 ->initializePackageManagement('TYPO3\\CMS\\Core\\Package\\PackageManager');
60
61 // Get all table names starting with 'cf_' and truncate them
62 $database = $this->getDatabaseConnection();
63 $tables = $database->admin_get_tables();
64 foreach ($tables as $table) {
65 $tableName = $table['Name'];
66 if (substr($tableName, 0, 3) === 'cf_') {
67 $database->exec_TRUNCATEquery($tableName);
68 }
69 }
70
71 // From this point on, the code may fatal, if some broken extension is loaded.
72
73 // Use bootstrap to load all ext_localconf and ext_tables
74 $bootstrap
75 ->loadTypo3LoadedExtAndExtLocalconf(FALSE)
76 ->applyAdditionalConfigurationSettings()
77 ->initializeTypo3DbGlobal()
78 ->loadExtensionTables(FALSE);
79
80 // The cache manager is already instantiated in the install tool
81 // with some hacked settings to disable caching of extbase and fluid.
82 // We want a "fresh" object here to operate on a different cache setup.
83 // cacheManager implements SingletonInterface, so the only way to get a "fresh"
84 // instance is by circumventing makeInstance and/or the objectManager and
85 // using new directly!
86 $cacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
87 $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
88 // Cache manager needs cache factory. cache factory injects itself to manager in __construct()
89 new \TYPO3\CMS\Core\Cache\CacheFactory('production', $cacheManager);
90
91 $cacheManager->flushCaches();
92 }
93
94 /**
95 * Get a database instance.
96 *
97 * @TODO: This method is a copy from AbstractAction. Review them and extract to service
98 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
99 */
100 protected function getDatabaseConnection() {
101 static $database;
102 if (!is_object($database)) {
103 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
104 $database = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
105 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
106 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
107 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
108 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
109 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
110 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
111 $database->connectDB();
112 }
113 return $database;
114 }
115 }