• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 28315908377

26 Jun 2026 09:49AM UTC coverage: 69.872% (-0.04%) from 69.907%
28315908377

push

github

NamelessCoder
[TASK] Add visibility and type on all class constants

6 of 17 new or added lines in 8 files covered. (35.29%)

4803 of 6874 relevant lines covered (69.87%)

4.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.11
/Classes/ViewHelpers/Render/CacheViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Render;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use TYPO3\CMS\Core\Cache\CacheManager;
12
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
15
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
16

17
/**
18
 * ### Cache Rendering ViewHelper
19
 *
20
 * Caches the child content (any type supported as long as it
21
 * can be serialized). Because of the added overhead you should
22
 * only use this if what you are caching is complex enough that
23
 * it performs many DB request (for example when displaying an
24
 * object with many lazy properties which don't load until the
25
 * template asks for the property value). In short, applies to
26
 * just about the same use cases as any other cache - but remember
27
 * that Fluid is already a very efficient rendering engine so don't
28
 * just assume that using the ViewHelper will increase performance
29
 * or decrease memory usage.
30
 *
31
 * Works forcibly, i.e. can only re-render its content if the
32
 * cache is cleared. A CTRL+Refresh in the browser does nothing,
33
 * even if a BE user is logged in. Only use this ViewHelper around
34
 * content which you are absolutely sure it makes sense to cache
35
 * along with an identity - for example, if rendering an uncached
36
 * plugin which contains a Partial template that is in all aspects
37
 * just a solid-state HTML representation of something like a list
38
 * of current news.
39
 *
40
 * The cache behind this ViewHelper is the Extbase object cache,
41
 * which is cleared when you clear the page content cache.
42
 *
43
 * Do not use on form elements, it will invalidate the checksum.
44
 *
45
 * Do not use around ViewHelpers which add header data or which
46
 * interact with the PageRenderer or other "live" objects; this
47
 * includes many of the VHS ViewHelpers!
48
 */
49
class CacheViewHelper extends AbstractRenderViewHelper
50
{
51
    private const string ID_PREFIX = 'vhs-render-cache-viewhelper';
52
    private const string ID_SEPARATOR = '-';
53

54
    /**
55
     * @var boolean
56
     */
57
    protected $escapeChildren = false;
58

59
    public function initializeArguments(): void
60
    {
61
        $this->registerArgument('content', 'string', 'Content to be cached');
2✔
62
        $this->registerArgument('identity', 'string', 'Identity for cached entry', true);
2✔
63
        parent::initializeArguments();
2✔
64
    }
65

66
    /**
67
     * @return mixed
68
     */
69
    public static function renderStatic(
70
        array $arguments,
71
        \Closure $renderChildrenClosure,
72
        RenderingContextInterface $renderingContext
73
    ) {
74
        /** @var string $identity */
75
        $identity = $arguments['identity'];
×
76
        if (!ctype_alnum(preg_replace('/[\-_]/i', '', $identity))) {
×
77
            if ($identity instanceof DomainObjectInterface) {
×
NEW
78
                $identity = get_class($identity) . self::ID_SEPARATOR . $identity->getUid();
×
79
            } elseif (method_exists($identity, '__toString')) {
×
80
                $identity = (string) $identity;
×
81
            } else {
82
                throw new \RuntimeException(
×
83
                    'Parameter $identity for Render/CacheViewHelper was not a string or a string-convertible object',
×
84
                    1352581782
×
85
                );
×
86
            }
87
        }
88

89
        // Hash the cache-key to circumvent disallowed chars
90
        $identity = sha1($identity);
×
91

92
        if (static::has($identity)) {
×
93
            return static::retrieve($identity);
×
94
        }
95
        /** @var mixed $content */
96
        $content = $arguments['content'] ?? $renderChildrenClosure();
×
97
        static::store($content, $identity);
×
98
        return $content;
×
99
    }
100

101
    protected static function has(string $id): bool
102
    {
NEW
103
        return static::getCache()->has(self::ID_PREFIX . self::ID_SEPARATOR . $id);
×
104
    }
105

106
    /**
107
     * @param mixed $value
108
     */
109
    protected static function store($value, string $id): void
110
    {
NEW
111
        static::getCache()->set(self::ID_PREFIX . self::ID_SEPARATOR . $id, $value);
×
112
    }
113

114
    /**
115
     * @return mixed
116
     */
117
    protected static function retrieve(string $id)
118
    {
119
        $cache = static::getCache();
×
NEW
120
        if ($cache->has(self::ID_PREFIX . self::ID_SEPARATOR . $id)) {
×
NEW
121
            return $cache->get(self::ID_PREFIX . self::ID_SEPARATOR . $id);
×
122
        }
123
        return null;
×
124
    }
125

126
    protected static function getCache(): FrontendInterface
127
    {
128
        /** @var CacheManager $cacheManager */
129
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
×
130
        return $cacheManager->getCache('vhs_main');
×
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc