Completed
Push — master ( dfd5cb...5cac00 )
by Tim
09:24
created

Classes/Hooks/ClearCache.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
0 ignored issues
show
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...
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...
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;
0 ignored issues
show
$composerClassLoading is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Core\Core\Boot...sComposerClassLoading() has been deprecated with message: will be removed in TYPO3 v10.0. Use the Environment API 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...
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