CacheApiService   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 42.31%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 15
c 7
b 0
f 3
lcom 1
cbo 0
dl 0
loc 152
ccs 22
cts 52
cp 0.4231
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A injectDataHandler() 0 3 1
A injectObjectManager() 0 3 1
A injectInstallToolClearCacheService() 0 3 1
A clearAllCaches() 0 3 2
A clearPageCache() 0 3 1
A clearConfigurationCache() 0 3 1
A clearSystemCache() 0 3 1
A clearAllActiveOpcodeCache() 0 3 1
A initializeObject() 0 10 1
A clearAllExceptPageCache() 0 19 3
A clearAllActiveOpcodeCacheWrapper() 0 7 2
1
<?php
2
namespace Etobi\CoreAPI\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012 Georg Ringer <[email protected]>
8
 *  (c) 2014 Stefano Kowalke <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
28
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Cache API service
33
 *
34
 * @author Georg Ringer <[email protected]>
35
 * @author Stefano Kowalke <[email protected]>
36
 * @package Etobi\CoreAPI\Service\SiteApiService
37
 */
38
class CacheApiService {
39
40
	/**
41
	 * @var \TYPO3\CMS\Core\DataHandling\DataHandler
42
	 */
43
	protected $dataHandler;
44
45
	/**
46
	 * @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
47
	 */
48
	protected $objectManager;
49
50
	/**
51
	 * @var \TYPO3\CMS\Install\Service\ClearCacheService
52
	 */
53
	protected $installToolClearCacheService;
54
55
	/**
56
	 * @param \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler
57
	 *
58
	 * @return void
59
	 */
60
	public function injectDataHandler(\TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler) {
61
		$this->dataHandler = $dataHandler;
62
	}
63
64
	/**
65
	 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
66
	 *
67
	 * @return void
68
	 */
69
	public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
70
		$this->objectManager = $objectManager;
71
	}
72
73
	/**
74
	 * @param \TYPO3\CMS\Install\Service\ClearCacheService $installToolClearCacheService
75
	 *
76
	 * @return void
77
	 */
78
	public function injectInstallToolClearCacheService(\TYPO3\CMS\Install\Service\ClearCacheService $installToolClearCacheService) {
79
		$this->installToolClearCacheService = $installToolClearCacheService;
80
	}
81
82
	/**
83
	 * Initialize the object.
84
	 *
85
	 * @return void
86
	 */
87
	public function initializeObject() {
88
		// Create a fake admin user
89
		$adminUser = $this->objectManager->get('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
90
		$adminUser->user['uid'] = $GLOBALS['BE_USER']->user['uid'];
91
		$adminUser->user['username'] = '_CLI_lowlevel';
92
		$adminUser->user['admin'] = 1;
93
		$adminUser->workspace = 0;
94
95
		$this->dataHandler->start(Array(), Array(), $adminUser);
96
	}
97
98
	/**
99
	 * Clear all caches.
100
	 *
101
	 * @param bool $hard
102
	 * @return void
103
	 */
104 1
	public function clearAllCaches($hard = FALSE) {
105 1
		!$hard ? $this->dataHandler->clear_cacheCmd('all') : $this->installToolClearCacheService->clearAll();
106 1
	}
107
108
	/**
109
	 * Clear the page cache.
110
	 *
111
	 * @return void
112
	 */
113 1
	public function clearPageCache() {
114 1
		$this->dataHandler->clear_cacheCmd('pages');
115 1
	}
116
117
	/**
118
	 * Clears the configuration cache.
119
	 *
120
	 * @return void
121
	 */
122 1
	public function clearConfigurationCache() {
123 1
		$this->dataHandler->clear_cacheCmd('temp_cached');
124 1
	}
125
126
	/**
127
	 * Clear the system cache
128
	 *
129
	 * @return void
130
	 */
131 1
	public function clearSystemCache() {
132 1
		$this->dataHandler->clear_cacheCmd('system');
133 1
	}
134
135
	/**
136
	 * Clears the opcode cache.
137
	 *
138
	 * @param string|NULL $fileAbsPath The file as absolute path to be cleared
139
	 *                                 or NULL to clear completely.
140
	 *
141
	 * @return void
142
	 */
143
	public function clearAllActiveOpcodeCache($fileAbsPath = NULL) {
144
		$this->clearAllActiveOpcodeCacheWrapper($fileAbsPath);
145
	}
146
147
	/**
148
	 * Clear all caches except the page cache.
149
	 * This is especially useful on big sites when you can't
150
	 * just drop the page cache.
151
	 *
152
	 * @return array with list of cleared caches
153
	 */
154 1
	public function clearAllExceptPageCache() {
155 1
		$out = array();
156 1
		$cacheKeys = array_keys($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
157 1
		$ignoredCaches = array('cache_pages', 'cache_pagesection');
158
159 1
		$toBeFlushed = array_diff($cacheKeys, $ignoredCaches);
160
161
		/** @var \TYPO3\CMS\Core\Cache\CacheManager $cacheManager */
162 1
		$cacheManager = $GLOBALS['typo3CacheManager'];
163 1
		foreach ($cacheKeys as $cacheKey) {
164 1
			if ($cacheManager->hasCache($cacheKey)) {
165
				$out[] = $cacheKey;
166
				$singleCache = $cacheManager->getCache($cacheKey);
167
				$singleCache->flush();
168
			}
169 1
		}
170
171 1
		return $toBeFlushed;
172
	}
173
174
	/**
175
	 * Clears the opcode cache. This just wraps the static call for testing purposes.
176
	 *
177
	 * @param string|NULL $fileAbsPath The file as absolute path to be cleared
178
	 *                                 or NULL to clear completely.
179
	 *
180
	 * @return void
181
	 */
182
	protected function clearAllActiveOpcodeCacheWrapper($fileAbsPath) {
183
		if (version_compare(TYPO3_version, '7.4.0', '<')) {
184
			\TYPO3\CMS\Core\Utility\OpcodeCacheUtility::clearAllActive($fileAbsPath);
185
		} else {
186
			\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Service\OpcodeCacheService')->clearAllActive($fileAbsPath);
187
		}
188
	}
189
}
190