Skip to content

Commit

Permalink
[TASK] Use render() as primary ViewHelper method
Browse files Browse the repository at this point in the history
  • Loading branch information
s2b committed Aug 20, 2024
1 parent 919b0b8 commit 504e77d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
45 changes: 27 additions & 18 deletions src/Core/ViewHelper/AbstractViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function initializeArgumentsAndRender()
$this->validateArguments();
$this->initialize();

return $this->callRenderMethod();
return $this->render();
}

/**
Expand Down Expand Up @@ -491,6 +491,31 @@ public function validateAdditionalArguments(array $arguments)
}
}

public function render()
{
return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws ViewHelper\Exception
* @deprecated
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)

Check failure on line 507 in src/Core/ViewHelper/AbstractViewHelper.php

View workflow job for this annotation

GitHub Actions / build (8.2)

PHPDoc tag @throws with type TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelper\Exception is not subtype of Throwable

Check failure on line 507 in src/Core/ViewHelper/AbstractViewHelper.php

View workflow job for this annotation

GitHub Actions / build (8.3)

PHPDoc tag @throws with type TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelper\Exception is not subtype of Throwable

Check failure on line 507 in src/Core/ViewHelper/AbstractViewHelper.php

View workflow job for this annotation

GitHub Actions / build (8.2)

PHPDoc tag @throws with type TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelper\Exception is not subtype of Throwable

Check failure on line 507 in src/Core/ViewHelper/AbstractViewHelper.php

View workflow job for this annotation

GitHub Actions / build (8.3)

PHPDoc tag @throws with type TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelper\Exception is not subtype of Throwable
{
throw new Exception(
sprintf(
'ViewHelper class "%s" does not declare a "render()" method and inherits the default "renderStatic". ' .
'Executing this ViewHelper would cause infinite recursion - please either implement "render()" or ' .
'"renderStatic()" on your ViewHelper class',
static::class,
),
);
}

/**
* You only should override this method *when you absolutely know what you
* are doing*, and really want to influence the generated PHP code during
Expand Down Expand Up @@ -528,7 +553,7 @@ public function validateAdditionalArguments(array $arguments)
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
{
$execution = sprintf(
'%s::renderStatic(%s, %s, $renderingContext)',
'$renderingContext->getViewHelperInvoker()->invoke(%s::class, %s, $renderingContext, %s)',
static::class,
$argumentsName,
$closureName,
Expand All @@ -551,22 +576,6 @@ public function compile($argumentsName, $closureName, &$initializationPhpCode, V
return $execution;
}

/**
* Default implementation of static rendering; useful API method if your ViewHelper
* when compiled is able to render itself statically to increase performance. This
* default implementation will simply delegate to the ViewHelperInvoker.
*
* @param array<string, mixed> $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$viewHelperClassName = get_called_class();
return $renderingContext->getViewHelperInvoker()->invoke($viewHelperClassName, $arguments, $renderingContext, $renderChildrenClosure);
}

/**
* Save the associated ViewHelper node in a static public class variable.
* called directly after the ViewHelper was built.
Expand Down
4 changes: 3 additions & 1 deletion src/Core/ViewHelper/Traits/CompileWithRenderStatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* any ViewHelper that conforms to the `renderStatic`
* method pattern.
*
* @todo add missing types with Fluid v5
* @deprecated Will be removed in v5. The non-static render() method
* should be used instead
*/
trait CompileWithRenderStatic
{
Expand All @@ -27,6 +28,7 @@ trait CompileWithRenderStatic
*/
public function render()
{
trigger_error('CompileWithRenderStatic has been deprecated and will be removed in Fluid v5.', E_USER_DEPRECATED);
return static::renderStatic(
$this->arguments,
$this->buildRenderChildrenClosure(),
Expand Down
21 changes: 0 additions & 21 deletions src/Core/ViewHelper/ViewHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,6 @@ public function handleAdditionalArguments(array $arguments);
*/
public function validateAdditionalArguments(array $arguments);

/**
* Here follows a more detailed description of the arguments of this function:
*
* $arguments contains a plain array of all arguments this ViewHelper has received,
* including the default argument values if an argument has not been specified
* in the ViewHelper invocation.
*
* $renderChildrenClosure is a closure you can execute instead of $this->renderChildren().
* It returns the rendered child nodes, so you can simply do $renderChildrenClosure() to execute
* it. It does not take any parameters.
*
* $renderingContext contains references to the VariableProvider and the
* ViewHelperVariableContainer.
*
* @param array<string, mixed> $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed the resulting value from the ViewHelper
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext);

/**
* Called when being inside a cached template.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Core/ViewHelper/AbstractViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ public function testCompileReturnsAndAssignsExpectedPhpCode(): void
$subject = $this->getMockBuilder(AbstractViewHelper::class)->onlyMethods([])->getMock();
$result = $subject->compile('foobar', 'baz', $init, $node, new TemplateCompiler());
self::assertEmpty($init);
self::assertEquals(get_class($subject) . '::renderStatic(foobar, baz, $renderingContext)', $result);
self::assertEquals('$renderingContext->getViewHelperInvoker()->invoke(' . get_class($subject) . '::class, foobar, $renderingContext, baz)', $result);
}

#[Test]
public function testCallRenderMethodCanRenderViewHelperWithoutRenderMethodAndCallsRenderStatic(): void
{
$subject = new RenderMethodFreeViewHelper();
$method = new \ReflectionMethod($subject, 'callRenderMethod');
$method = new \ReflectionMethod($subject, 'render');
$subject->setRenderingContext(new RenderingContext());
$result = $method->invoke($subject);
self::assertSame('I was rendered', $result);
Expand All @@ -162,7 +162,7 @@ public function testCallRenderMethodOnViewHelperWithoutRenderMethodWithDefaultRe
{
$this->expectException(Exception::class);
$subject = new RenderMethodFreeDefaultRenderStaticViewHelper();
$method = new \ReflectionMethod($subject, 'callRenderMethod');
$method = new \ReflectionMethod($subject, 'render');
$subject->setRenderingContext(new RenderingContext());
$method->invoke($subject);
}
Expand Down

0 comments on commit 504e77d

Please sign in to comment.