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

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

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

57.5
/Classes/ViewHelpers/Once/AbstractOnceViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Once;
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 FluidTYPO3\Vhs\Utility\ContextUtility;
12
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
13
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
15

16
/**
17
 * Base class for "Render Once"-style ViewHelpers: session, cookie,
18
 * request, template variable set, ViewHelper variable set etc.
19
 */
20
abstract class AbstractOnceViewHelper extends AbstractConditionViewHelper
21
{
22
    /**
23
     * Standard storage - static variable meaning uniqueness of $identifier
24
     * across each Request, i.e. unique to each individual plugin/content.
25
     *
26
     * @var array
27
     */
28
    protected static $identifiers = [];
29

30
    /**
31
     * Always-current but statically assigned instance of rendering context
32
     * which applied at the exact time that the ViewHelper was asked to
33
     * evaluate whether or not to render content.
34
     *
35
     * @var RenderingContextInterface&RenderingContext
36
     */
37
    protected static $currentRenderingContext;
38

39
    public function initializeArguments(): void
40
    {
41
        parent::initializeArguments();
28✔
42
        $this->registerArgument(
28✔
43
            'identifier',
28✔
44
            'string',
28✔
45
            'Identity of this condition - if used in other places, the condition applies to the same identity in the ' .
28✔
46
            'storage (i.e. cookie name or session key)'
28✔
47
        );
28✔
48
        $this->registerArgument(
28✔
49
            'lockToDomain',
28✔
50
            'boolean',
28✔
51
            'If TRUE, locks this condition to a specific domain, i.e. the storage of $identity is associated with ' .
28✔
52
            'a domain. If same identity is also used without domain lock, it matches any domain locked condition',
28✔
53
            false,
28✔
54
            false
28✔
55
        );
28✔
56
        $this->registerArgument(
28✔
57
            'ttl',
28✔
58
            'integer',
28✔
59
            'Time-to-live for skip registration, number of seconds. After this expires the registration is unset',
28✔
60
            false,
28✔
61
            86400
28✔
62
        );
28✔
63
    }
64

65
    /**
66
     * @return mixed
67
     */
68
    public static function renderStatic(
69
        array $arguments,
70
        \Closure $renderChildrenClosure,
71
        RenderingContextInterface $renderingContext
72
    ) {
73
        /** @var RenderingContext $renderingContext */
74
        static::$currentRenderingContext = $renderingContext;
×
75
        return parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
×
76
    }
77

78
    public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool
79
    {
80
        static::removeIfExpired($arguments);
×
81
        $shouldSkip = static::assertShouldSkip($arguments) === false;
×
82
        static::storeIdentifier($arguments);
×
83
        return $shouldSkip;
×
84
    }
85

86
    protected static function getIdentifier(array $arguments): string
87
    {
88
        return $arguments['identifier'] ?? static::class;
21✔
89
    }
90

91
    protected static function storeIdentifier(array $arguments): void
92
    {
93
        $identifier = static::getIdentifier($arguments);
×
94
        if (!isset(static::$identifiers[$identifier])) {
×
95
            static::$identifiers[$identifier] = time();
×
96
        }
97
    }
98

99
    protected static function removeIfExpired(array $arguments): void
100
    {
101
        $id = static::getIdentifier($arguments);
×
102
        if (isset(static::$identifiers[$id]) && static::$identifiers[$id] <= time() - $arguments['ttl']) {
×
103
            unset(static::$identifiers[$id]);
×
104
        }
105
    }
106

107
    protected static function assertShouldSkip(array $arguments): bool
108
    {
109
        $identifier = static::getIdentifier($arguments);
×
110
        return isset(static::$identifiers[$identifier]);
×
111
    }
112

113
    /**
114
     * Override: forcibly disables page caching - a TRUE condition
115
     * in this ViewHelper means page content would be depending on
116
     * the current visitor's session/cookie/auth etc.
117
     *
118
     * Returns value of "then" attribute.
119
     * If then attribute is not set, iterates through child nodes and renders ThenViewHelper.
120
     * If then attribute is not set and no ThenViewHelper and no ElseViewHelper is found, all child nodes are rendered
121
     *
122
     * @return mixed rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found
123
     */
124
    protected function renderThenChild()
125
    {
126
        if (ContextUtility::isFrontend()) {
×
127
            $GLOBALS['TSFE']->no_cache = 1;
×
128
        }
129
        return parent::renderThenChild();
×
130
    }
131
}
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