Skip to content

Commit

Permalink
[TASK] Use render() instead of renderStatic() in all ViewHelpers
Browse files Browse the repository at this point in the history
Performance tests have shown that the benefits of `renderStatic()`
are negligent with current PHP versions. Thus, in preparation of
the deprecation of the `CompileWithRenderStatic` trait, we
migrate all ViewHelpers to use `render()`.
  • Loading branch information
s2b committed Aug 20, 2024
1 parent 919b0b8 commit 9643576
Show file tree
Hide file tree
Showing 33 changed files with 130 additions and 318 deletions.
19 changes: 6 additions & 13 deletions src/ViewHelpers/AliasViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* Declares new variables which are aliases of other variables.
Expand Down Expand Up @@ -58,8 +56,6 @@
*/
class AliasViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* @var bool
*/
Expand All @@ -74,17 +70,14 @@ public function initializeArguments()
/**
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
public function render()
{
$globalVariableProvider = $renderingContext->getVariableProvider();
$localVariableProvider = new StandardVariableProvider($arguments['map']);
$globalVariableProvider = $this->renderingContext->getVariableProvider();
$localVariableProvider = new StandardVariableProvider($this->arguments['map']);
$scopedVariableProvider = new ScopedVariableProvider($globalVariableProvider, $localVariableProvider);
$renderingContext->setVariableProvider($scopedVariableProvider);

$output = $renderChildrenClosure();

$renderingContext->setVariableProvider($globalVariableProvider);

$this->renderingContext->setVariableProvider($scopedVariableProvider);
$output = $this->renderChildren();
$this->renderingContext->setVariableProvider($globalVariableProvider);
return $output;
}
}
8 changes: 2 additions & 6 deletions src/ViewHelpers/ConstantViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* Wrapper for PHPs :php:`constant` function.
Expand Down Expand Up @@ -46,17 +44,15 @@
*/
class ConstantViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('name', 'string', 'String representation of a PHP constant or enum');
}

public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): mixed
public function render(): mixed
{
$name = $arguments['name'] ?? $renderChildrenClosure();
$name = $this->arguments['name'] ?? $this->renderChildren();
return constant($name);
}
}
8 changes: 2 additions & 6 deletions src/ViewHelpers/CountViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* This ViewHelper counts elements of the specified array or countable object.
Expand Down Expand Up @@ -44,8 +42,6 @@
*/
class CountViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* @var bool
*/
Expand All @@ -65,9 +61,9 @@ public function initializeArguments()
/**
* @return int
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
public function render()
{
$countable = $renderChildrenClosure();
$countable = $this->renderChildren();
if ($countable === null) {
return 0;
}
Expand Down
30 changes: 10 additions & 20 deletions src/ViewHelpers/CycleViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* This ViewHelper cycles through the specified values.
Expand Down Expand Up @@ -68,8 +66,6 @@
*/
class CycleViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* @var bool
*/
Expand All @@ -85,34 +81,28 @@ public function initializeArguments()
/**
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
public function render()
{
$values = $arguments['values'];
$as = $arguments['as'];
$values = $this->arguments['values'];
$as = $this->arguments['as'];
if ($values === null) {
return $renderChildrenClosure();
return $this->renderChildren();
}
$values = static::initializeValues($values);
$index = static::initializeIndex($as, $renderingContext->getViewHelperVariableContainer());

$index = static::initializeIndex($as, $this->renderingContext->getViewHelperVariableContainer());
$currentValue = isset($values[$index]) ? $values[$index] : null;

$scopedVariableProvider = new ScopedVariableProvider(
$renderingContext->getVariableProvider(),
$this->renderingContext->getVariableProvider(),
new StandardVariableProvider([$as => $currentValue]),
);
$renderingContext->setVariableProvider($scopedVariableProvider);

$output = $renderChildrenClosure();

$renderingContext->setVariableProvider($scopedVariableProvider->getGlobalVariableProvider());

$this->renderingContext->setVariableProvider($scopedVariableProvider);
$output = $this->renderChildren();
$this->renderingContext->setVariableProvider($scopedVariableProvider->getGlobalVariableProvider());
$index++;
if (!isset($values[$index])) {
$index = 0;
}
$renderingContext->getViewHelperVariableContainer()->addOrUpdate(static::class, $as, $index);

$this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(static::class, $as, $index);
return $output;
}

Expand Down
15 changes: 5 additions & 10 deletions src/ViewHelpers/DebugViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* This ViewHelper is only meant to be used during development.
Expand Down Expand Up @@ -44,8 +42,6 @@
*/
class DebugViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* @var bool
*/
Expand All @@ -67,16 +63,15 @@ public function initializeArguments()
/**
* @return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
public function render()
{
$typeOnly = $arguments['typeOnly'];
$expressionToExamine = $renderChildrenClosure();
$typeOnly = $this->arguments['typeOnly'];
$expressionToExamine = $this->renderChildren();
if ($typeOnly === true) {
return is_object($expressionToExamine) ? get_class($expressionToExamine) : gettype($expressionToExamine);
}

$html = $arguments['html'];
$levels = $arguments['levels'];
$html = $this->arguments['html'];
$levels = $this->arguments['levels'];
return static::dumpVariable($expressionToExamine, $html, 1, $levels);
}

Expand Down
11 changes: 2 additions & 9 deletions src/ViewHelpers/FirstViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* The FirstViewHelper returns the first item of an array.
Expand All @@ -29,17 +27,14 @@
*/
final class FirstViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

public function initializeArguments(): void
{
$this->registerArgument('value', 'array', '');
}

public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): mixed
public function render(): mixed
{
$value = $arguments['value'] ?? $renderChildrenClosure();

$value = $this->arguments['value'] ?? $this->renderChildren();
if ($value === null || !is_iterable($value)) {
$givenType = get_debug_type($value);
throw new \InvalidArgumentException(
Expand All @@ -48,9 +43,7 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
1712220569,
);
}

$value = iterator_to_array($value);

return array_shift($value);
}
}
43 changes: 17 additions & 26 deletions src/ViewHelpers/ForViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* Loop ViewHelper which can be used to iterate over arrays.
Expand Down Expand Up @@ -78,8 +76,6 @@
*/
class ForViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* @var bool
*/
Expand All @@ -99,50 +95,45 @@ public function initializeArguments()
* @return string
* @throws ViewHelper\Exception
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
public function render()
{
if (!isset($arguments['each'])) {
if (!isset($this->arguments['each'])) {
return '';
}
if (is_object($arguments['each']) && !$arguments['each'] instanceof \Traversable) {
if (is_object($this->arguments['each']) && !$this->arguments['each'] instanceof \Traversable) {
throw new ViewHelper\Exception('ForViewHelper only supports arrays and objects implementing \Traversable interface', 1248728393);
}

if ($arguments['reverse'] === true) {
$arguments['each'] = array_reverse(iterator_to_array($arguments['each']), true);
if ($this->arguments['reverse'] === true) {
$this->arguments['each'] = array_reverse(iterator_to_array($this->arguments['each']), true);
}
if (isset($arguments['iteration'])) {
if (isset($this->arguments['iteration'])) {
$iterationData = [
'index' => 0,
'cycle' => 1,
'total' => count($arguments['each']),
'total' => count($this->arguments['each']),
];
}

$globalVariableProvider = $renderingContext->getVariableProvider();
$globalVariableProvider = $this->renderingContext->getVariableProvider();
$localVariableProvider = new StandardVariableProvider();
$renderingContext->setVariableProvider(new ScopedVariableProvider($globalVariableProvider, $localVariableProvider));

$this->renderingContext->setVariableProvider(new ScopedVariableProvider($globalVariableProvider, $localVariableProvider));
$output = '';
foreach ($arguments['each'] as $keyValue => $singleElement) {
$localVariableProvider->add($arguments['as'], $singleElement);
if (isset($arguments['key'])) {
$localVariableProvider->add($arguments['key'], $keyValue);
foreach ($this->arguments['each'] as $keyValue => $singleElement) {
$localVariableProvider->add($this->arguments['as'], $singleElement);
if (isset($this->arguments['key'])) {
$localVariableProvider->add($this->arguments['key'], $keyValue);
}
if (isset($arguments['iteration'])) {
if (isset($this->arguments['iteration'])) {
$iterationData['isFirst'] = $iterationData['cycle'] === 1;
$iterationData['isLast'] = $iterationData['cycle'] === $iterationData['total'];
$iterationData['isEven'] = $iterationData['cycle'] % 2 === 0;
$iterationData['isOdd'] = !$iterationData['isEven'];
$localVariableProvider->add($arguments['iteration'], $iterationData);
$localVariableProvider->add($this->arguments['iteration'], $iterationData);
$iterationData['index']++;
$iterationData['cycle']++;
}
$output .= $renderChildrenClosure();
$output .= $this->renderChildren();
}

$renderingContext->setVariableProvider($globalVariableProvider);

$this->renderingContext->setVariableProvider($globalVariableProvider);
return $output;
}
}
15 changes: 4 additions & 11 deletions src/ViewHelpers/Format/CaseViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

namespace TYPO3Fluid\Fluid\ViewHelpers\Format;

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* Modifies the case of an input string to upper- or lowercase or capitalization.
Expand Down Expand Up @@ -67,8 +65,6 @@
*/
final class CaseViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

/**
* Directs the input string being converted to "lowercase"
*/
Expand Down Expand Up @@ -111,15 +107,13 @@ public function initializeArguments(): void
* Changes the case of the input string
* @throws Exception
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
public function render(): string
{
$value = $arguments['value'];
$mode = $arguments['mode'];

$value = $this->arguments['value'];
$mode = $this->arguments['mode'];
if ($value === null) {
$value = (string)$renderChildrenClosure();
$value = (string)$this->renderChildren();
}

switch ($mode) {
case self::CASE_LOWER:
$output = mb_strtolower($value, 'utf-8');
Expand All @@ -145,7 +139,6 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
default:
throw new Exception('The case mode "' . $mode . '" supplied to Fluid\'s format.case ViewHelper is not supported.', 1358349150);
}

return $output;
}
}
Loading

0 comments on commit 9643576

Please sign in to comment.