Skip to content

Commit

Permalink
[FrameworkBundle] Add AbstractController::renderBlock() and `$block…
Browse files Browse the repository at this point in the history
…` argument to `renderView()`
  • Loading branch information
nicolas-grekas committed Aug 9, 2023
1 parent f0959b4 commit 29a7ae9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
1 change: 1 addition & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ FrameworkBundle
---------------

* [BC break] Add native return type to `Translator` and to `Application::reset()`
* Add `$block` argument to `AbstractController::renderView()`
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable
the integration by setting `framework.annotations` to `false`

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.4
---

* Add `AbstractController::renderBlock()` and `$block` argument to `renderView()`
* Add native return type to `Translator` and to `Application::reset()`
* Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable the integration by setting `framework.annotations` to `false`
* Enable `json_decode_detailed_errors` context for Serializer by default if `kernel.debug` is true and the `seld/jsonlint` package is installed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,13 @@ protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = nu
* Returns a rendered view.
*
* Forms found in parameters are auto-cast to form views.
*
* @param string|null $block
*/
protected function renderView(string $view, array $parameters = []): string
protected function renderView(string $view, array $parameters = []/* , string $block = null */): string
{
$block = 2 < \func_num_args() ? func_get_arg(2) : null;

if (!$this->container->has('twig')) {
throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
}
Expand All @@ -239,6 +243,10 @@ protected function renderView(string $view, array $parameters = []): string
}
}

if (null !== $block) {
return $this->container->get('twig')->load($view)->renderBlock($block, $parameters);
}

return $this->container->get('twig')->render($view, $parameters);
}

Expand All @@ -250,21 +258,18 @@ protected function renderView(string $view, array $parameters = []): string
*/
protected function render(string $view, array $parameters = [], Response $response = null): Response
{
$content = $this->renderView($view, $parameters);
$response ??= new Response();

if (200 === $response->getStatusCode()) {
foreach ($parameters as $v) {
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
break;
}
}
}

$response->setContent($content);
return $this->doRender($view, null, $parameters, $response);
}

return $response;
/**
* Renders a block in a view.
*
* If an invalid form is found in the list of parameters, a 422 status code is returned.
* Forms found in parameters are auto-cast to form views.
*/
protected function renderBlock(string $view, string $block, array $parameters = [], Response $response = null): Response

Check failure on line 270 in src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyUnusedMethod

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php:270:24: PossiblyUnusedMethod: Cannot find explicit calls to method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::renderBlock (but did find some potential callers) (see https://psalm.dev/087)

Check failure on line 270 in src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyUnusedMethod

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php:270:24: PossiblyUnusedMethod: Cannot find explicit calls to method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::renderBlock (but did find some potential callers) (see https://psalm.dev/087)
{
return $this->doRender($view, $block, $parameters, $response);
}

/**
Expand Down Expand Up @@ -431,4 +436,23 @@ protected function sendEarlyHints(iterable $links = [], Response $response = nul

return $response;
}

private function doRender(string $view, ?string $block, array $parameters, ?Response $response): Response
{
$content = $this->renderView($view, $parameters, $block);
$response ??= new Response();

if (200 === $response->getStatusCode()) {
foreach ($parameters as $v) {
if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
$response->setStatusCode(422);
break;
}
}
}

$response->setContent($content);

return $response;
}
}

0 comments on commit 29a7ae9

Please sign in to comment.