TYPO3  7.6
ClearCacheService.php
Go to the documentation of this file.
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 
18 
24 {
28  protected $objectManager = null;
29 
33  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
34  {
35  $this->objectManager = $objectManager;
36  }
37 
53  public function clearAll()
54  {
55  // Delete typo3temp/Cache
56  GeneralUtility::flushDirectory(PATH_site . 'typo3temp/Cache', true, true);
57 
58  $bootstrap = \TYPO3\CMS\Core\Core\Bootstrap::getInstance();
60  ->initializeCachingFramework()
61  ->initializePackageManagement(\TYPO3\CMS\Core\Package\PackageManager::class);
62 
63  // Get all table names starting with 'cf_' and truncate them
64  $database = $this->getDatabaseConnection();
65  $tables = $database->admin_get_tables();
66  foreach ($tables as $table) {
67  $tableName = $table['Name'];
68  if (substr($tableName, 0, 3) === 'cf_') {
69  $database->exec_TRUNCATEquery($tableName);
70  } elseif ($tableName === 'cache_treelist') {
71  // cache_treelist is not implemented in the caching framework.
72  // clear this table manually
73  $database->exec_TRUNCATEquery('cache_treelist');
74  }
75  }
76 
77  // From this point on, the code may fatal, if some broken extension is loaded.
78 
79  // Use bootstrap to load all ext_localconf and ext_tables
81  ->loadTypo3LoadedExtAndExtLocalconf(false)
82  ->defineLoggingAndExceptionConstants()
83  ->unsetReservedGlobalVariables()
84  ->initializeTypo3DbGlobal()
85  ->loadExtensionTables(false);
86 
87  // The cache manager is already instantiated in the install tool
88  // with some hacked settings to disable caching of extbase and fluid.
89  // We want a "fresh" object here to operate on a different cache setup.
90  // cacheManager implements SingletonInterface, so the only way to get a "fresh"
91  // instance is by circumventing makeInstance and/or the objectManager and
92  // using new directly!
93  $cacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
94  $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
95  // Cache manager needs cache factory. cache factory injects itself to manager in __construct()
96  new \TYPO3\CMS\Core\Cache\CacheFactory('production', $cacheManager);
97 
98  $cacheManager->flushCaches();
99  }
100 
107  protected function getDatabaseConnection()
108  {
109  static $database;
110  if (!is_object($database)) {
112  $database = $this->objectManager->get(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
113  $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
114  $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
115  $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
116  $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
117  $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
118  $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
119  $database->initialize();
120  $database->connectDB();
121  }
122  return $database;
123  }
124 }