Completed
Push — master ( d3c055...25d723 )
by Tim
13:13
created

ClearCache::clear()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 8
cp 0
rs 9.3554
c 0
b 0
f 0
cc 5
nc 3
nop 2
crap 30
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\ClassLoadingInformation;
16
use TYPO3\CMS\Core\Core\Environment;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Clear Cache hook for the Backend.
21
 *
22
 * @hook TYPO3_CONF_VARS|SC_OPTIONS|additionalBackendItems|cacheActions
23
 */
24
class ClearCache implements ClearCacheActionsHookInterface
25
{
26
    /**
27
     * Modifies CacheMenuItems array.
28
     *
29
     * @param array $cacheActions
30
     * @param array $optionValues
31
     */
32
    public function manipulateCacheActions(&$cacheActions, &$optionValues)
33
    {
34
        if (!$this->isAdmin() || (!$this->isAlwaysActivated() && $this->isProduction())) {
35
            return;
36
        }
37
38
        $action = [
39
            'id' => 'autoloader',
40
            'title' => 'LLL:EXT:autoloader/Resources/Private/Language/locallang.xlf:cache.title',
41
            'description' => 'LLL:EXT:autoloader/Resources/Private/Language/locallang.xlf:cache.description',
42
            'href' => $this->getAjaxUri(),
43
            'iconIdentifier' => 'extension-autoloader-clearcache',
44
        ];
45
46
        $cacheActions[] = $action;
47
    }
48
49
    /**
50
     * clear Cache ajax handler.
51
     *
52
     * @param array $ajaxParams
53
     * @param $ajaxObj
54
     */
55
    public function clear($ajaxParams, $ajaxObj)
0 ignored issues
show
Unused Code introduced by
The parameter $ajaxParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $ajaxObj is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        if (!$this->isAdmin() || (!$this->isAlwaysActivated() && $this->isProduction())) {
58
            return;
59
        }
60
61
        /** @var \TYPO3\CMS\Core\Cache\CacheManager $cacheManager */
62
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
63
        $cacheManager->getCache('autoloader')
64
            ->flush();
65
66
        $composerClassLoading = Environment::isComposerMode();
67
68
        // Dump new class loading information
69
        if (!$composerClassLoading) {
70
            ClassLoadingInformation::dumpClassLoadingInformation();
71
        }
72
    }
73
74
    /**
75
     * Get Ajax URI.
76
     *
77
     * @return string
78
     */
79
    protected function getAjaxUri(): string
80
    {
81
        /** @var UriBuilder $uriBuilder */
82
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
83
        try {
84
            $routeIdentifier = 'ajax_autoloader::clearCache';
85
            $uri = $uriBuilder->buildUriFromRoute($routeIdentifier);
86
        } catch (RouteNotFoundException $e) {
87
            return '';
88
        }
89
90
        return (string) $uri;
91
    }
92
93
    /**
94
     * Return if the clear cache element is als visible in production.
95
     *
96
     * @return bool
97
     */
98
    protected function isAlwaysActivated(): bool
99
    {
100
        $configuration = \unserialize((string) $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['autoloader']);
101
102
        return isset($configuration['enableAutoloaderClearCacheInProduction']) ? (bool) $configuration['enableAutoloaderClearCacheInProduction'] : false;
103
    }
104
105
    /**
106
     * Return TRUE if the current instance is in production mode.
107
     *
108
     * @return bool
109
     */
110
    protected function isProduction(): bool
111
    {
112
        return GeneralUtility::getApplicationContext()
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Core\Utility\G...getApplicationContext() has been deprecated with message: since TYPO3 v10.2, will be removed in TYPO3 v11, use Environment::getContext() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
113
            ->isProduction();
114
    }
115
116
    /**
117
     * Check if the user is a admin.
118
     *
119
     * @return bool
120
     */
121
    protected function isAdmin(): bool
122
    {
123
        return \is_object($this->getBackendUserAuthentication()) && $this->getBackendUserAuthentication()
124
                ->isAdmin();
125
    }
126
127
    /**
128
     * Return the Backend user authentication.
129
     *
130
     * @return BackendUserAuthentication
131
     */
132
    protected function getBackendUserAuthentication()
133
    {
134
        return $GLOBALS['BE_USER'];
135
    }
136
}
137