1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Clear Cache hook for the Backend. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace HDNET\Autoloader\Hooks; |
9
|
|
|
|
10
|
|
|
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException; |
11
|
|
|
use TYPO3\CMS\Backend\Routing\UriBuilder; |
12
|
|
|
use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface; |
13
|
|
|
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; |
14
|
|
|
use TYPO3\CMS\Core\Cache\CacheManager; |
15
|
|
|
use TYPO3\CMS\Core\Core\Bootstrap; |
16
|
|
|
use TYPO3\CMS\Core\Core\ClassLoadingInformation; |
17
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
19
|
|
|
use TYPO3\CMS\Core\Utility\VersionNumberUtility; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Clear Cache hook for the Backend. |
23
|
|
|
* |
24
|
|
|
* @hook TYPO3_CONF_VARS|SC_OPTIONS|additionalBackendItems|cacheActions |
25
|
|
|
*/ |
26
|
|
|
class ClearCache implements ClearCacheActionsHookInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Modifies CacheMenuItems array. |
30
|
|
|
* |
31
|
|
|
* @param array $cacheActions |
32
|
|
|
* @param array $optionValues |
33
|
|
|
*/ |
34
|
|
|
public function manipulateCacheActions(&$cacheActions, &$optionValues) |
35
|
|
|
{ |
36
|
|
|
if (!$this->isAdmin() || (!$this->isAlwaysActivated() && $this->isProduction())) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$action = [ |
41
|
|
|
'id' => 'autoloader', |
42
|
|
|
'title' => 'LLL:EXT:autoloader/Resources/Private/Language/locallang.xlf:cache.title', |
43
|
|
|
'description' => 'LLL:EXT:autoloader/Resources/Private/Language/locallang.xlf:cache.description', |
44
|
|
|
'href' => $this->getAjaxUri(), |
45
|
|
|
'iconIdentifier' => 'extension-autoloader', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$cacheActions[] = $action; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* clear Cache ajax handler. |
53
|
|
|
* |
54
|
|
|
* @param array $ajaxParams |
55
|
|
|
* @param $ajaxObj |
56
|
|
|
*/ |
57
|
|
|
public function clear($ajaxParams, $ajaxObj) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if (!$this->isAdmin() || (!$this->isAlwaysActivated() && $this->isProduction())) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @var \TYPO3\CMS\Core\Cache\CacheManager $cacheManager */ |
64
|
|
|
$cacheManager = GeneralUtility::makeInstance(CacheManager::class); |
65
|
|
|
$cacheManager->getCache('autoloader') |
66
|
|
|
->flush(); |
67
|
|
|
|
68
|
|
|
$composerClassLoading = true; |
|
|
|
|
69
|
|
|
if (VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getNumericTypo3Version()) >= 9000000) { |
70
|
|
|
// TYPO3 >= 9.0.0 |
71
|
|
|
$composerClassLoading = Environment::isComposerMode(); |
72
|
|
|
} else { |
73
|
|
|
// TYPO3 < 9.0.0 |
74
|
|
|
$composerClassLoading = Bootstrap::usesComposerClassLoading(); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Dump new class loading information |
78
|
|
|
if (!$composerClassLoading) { |
79
|
|
|
ClassLoadingInformation::dumpClassLoadingInformation(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Ajax URI. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function getAjaxUri(): string |
89
|
|
|
{ |
90
|
|
|
/** @var UriBuilder $uriBuilder */ |
91
|
|
|
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
92
|
|
|
try { |
93
|
|
|
$routeIdentifier = 'ajax_autoloader::clearCache'; |
94
|
|
|
$uri = $uriBuilder->buildUriFromRoute($routeIdentifier); |
95
|
|
|
} catch (RouteNotFoundException $e) { |
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return (string) $uri; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return if the clear cache element is als visible in production. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function isAlwaysActivated(): bool |
108
|
|
|
{ |
109
|
|
|
$configuration = \unserialize((string) $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['autoloader']); |
110
|
|
|
|
111
|
|
|
return isset($configuration['enableAutoloaderClearCacheInProduction']) ? (bool) $configuration['enableAutoloaderClearCacheInProduction'] : false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return TRUE if the current instance is in production mode. |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
protected function isProduction(): bool |
120
|
|
|
{ |
121
|
|
|
return GeneralUtility::getApplicationContext() |
122
|
|
|
->isProduction(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if the user is a admin. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
protected function isAdmin(): bool |
131
|
|
|
{ |
132
|
|
|
return \is_object($this->getBackendUserAuthentication()) && $this->getBackendUserAuthentication() |
133
|
|
|
->isAdmin(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the Backend user authentication. |
138
|
|
|
* |
139
|
|
|
* @return BackendUserAuthentication |
140
|
|
|
*/ |
141
|
|
|
protected function getBackendUserAuthentication() |
142
|
|
|
{ |
143
|
|
|
return $GLOBALS['BE_USER']; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.