Passed
Push — master ( c57432...a219dd )
by Benjamin
01:42
created

SetPageCacheHook::isPageCacheContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
namespace Qbus\SkipPageIsBeingGenerated\Hooks;
3
4
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
5
6
/**
7
 * SetPageCacheHook
8
 *
9
 * @author Benjamin Franzke <[email protected]>
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
11
 */
12
class SetPageCacheHook
13
{
14
    /**
15
     * @param array             $params
16
     * @param FrontendInterface $frontend
17
     * @return void
18
     */
19
    public function set(&$params, $frontend)
20
    {
21
        if ($this->isPageCacheContent($params, $frontend) === false) {
22
            return;
23
        }
24
25
        if (isset($params['variable']['temp_content']) && $params['variable']['temp_content']) {
26
            /* We can't prevent temp_content ('Page is being generated') from going into cache.
27
             * But lifetime '-1' will immediately invalidate the temporary cache entry,
28
             * which is enough, so that it is never used. */
29
            $params['lifetime'] = -1;
30
31
            if ($frontend->getBackend() instanceof \TYPO3\CMS\Core\Cache\Backend\RedisBackend) {
32
                /* The redis backend does not allow lifetime of -1, use 1 as a workaround.
33
                 * That means the temporary record will be stored to cache, but as we set the
34
                 * 'variable' to false, it is interpreted as unset in TSFE:
35
                 * https://github.com/TYPO3/TYPO3.CMS/blob/8.5.1/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php#L2352 */
36
                $params['lifetime'] = 1;
37
                /* We may move `'variable' = false` out this if in a non-bugfix release,
38
                 * but for now we leave it here to make sure we do not break things. */
39
                $params['variable'] = false;
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param array             $params
46
     * @param FrontendInterface $frontend
47
     * @return bool
48
     */
49
    protected function isPageCacheContent($params, $frontend)
50
    {
51
        if ($frontend->getIdentifier() !== 'cache_pages') {
52
            return false;
53
        }
54
55
        // TYPO3 v9 added none-page content to cache_pages, ignore those.
56
        $ignoredIdentifiers = [
57
            'redirects',
58
            '-titleTag-',
59
            '-metatag-',
60
        ];
61
        foreach ($ignoredIdentifiers as $ignored) {
62
            if (strpos($params['entryIdentifier'], $ignored) !== false) {
63
                return false;
64
            }
65
        }
66
67
        return true;
68
    }
69
}
70