Skip to content

Commit

Permalink
allow prebuilt responses and handle default adjustments when rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vo committed Mar 28, 2022
1 parent 11a0ea6 commit f520dc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
16 changes: 12 additions & 4 deletions core-bundle/src/Controller/AbstractFragmentController.php
Expand Up @@ -104,22 +104,22 @@ protected function createTemplate(Model $model, string|null $fallbackTemplateNam
// Allow calling render() without a view
$this->view = !$isLegacyTemplate ? "@Contao/$templateName.html.twig" : null;

$onGetResponse = function (FragmentTemplate $template) use ($templateName, $isLegacyTemplate): Response {
$onGetResponse = function (FragmentTemplate $template, Response|null $preBuiltResponse) use ($templateName, $isLegacyTemplate): Response {
if ($isLegacyTemplate) {
// Render using the legacy framework
$legacyTemplate = $this->container->get('contao.framework')->createInstance(FrontendTemplate::class, [$templateName]);
$legacyTemplate->setData($template->getData());

$response = $legacyTemplate->getResponse();
$this->markResponseForInternalCaching($response);
$this->prepareResponse($response);

return $response;
}

// Directly render with Twig
$context = $this->container->get('contao.twig.interop.context_factory')->fromContaoTemplate($template);

return $this->render(parameters: $context);
return $this->render(parameters: $context, response: $preBuiltResponse);
};

$template = new FragmentTemplate($templateName, $onGetResponse);
Expand Down Expand Up @@ -222,12 +222,20 @@ protected function render(string|null $view = null, array $parameters = [], Resp
if (null === $response) {
$response = new Response();

$this->markResponseForInternalCaching($response);
$this->prepareResponse($response);
}

return parent::render($view, $parameters, $response);
}

/**
* Called whenever a new response gets created.
*/
protected function prepareResponse(Response $response): void
{
$this->markResponseForInternalCaching($response);
}

/**
* Marks the response to affect the caching of the current page but removes any default cache header.
*/
Expand Down
Expand Up @@ -23,8 +23,12 @@

abstract class AbstractContentElementController extends AbstractFragmentController
{
private ContentModel|null $model = null;

public function __invoke(Request $request, ContentModel $model, string $section, array $classes = null): Response
{
$this->model = $model;

$template = $this->createTemplate($model, 'ce_'.$this->getType());
$properties = $request->attributes->get('templateProperties', []);
$this->addDefaultDataToTemplate($template, $model, $section, $classes ?? [], $properties);
Expand All @@ -34,6 +38,15 @@ public function __invoke(Request $request, ContentModel $model, string $section,
return $this->getResponse($template, $model, $request);
}

protected function prepareResponse(Response $response): void
{
parent::prepareResponse($response);

if (null !== $this->model) {
$this->addSharedMaxAgeToResponse($response, $this->model);
}
}

protected function addSharedMaxAgeToResponse(Response $response, ContentModel $model): void
{
$time = time();
Expand Down
6 changes: 3 additions & 3 deletions core-bundle/src/Twig/Interop/FragmentTemplate.php
Expand Up @@ -30,7 +30,7 @@ final class FragmentTemplate /* TODO: remove base class in Contao 6 */ extends T
private array $context = [];

/**
* @param \Closure(self):Response $onGetResponse
* @param \Closure(self, Response|null):Response $onGetResponse
*
* @internal
* @noinspection MagicMethodsValidityInspection
Expand Down Expand Up @@ -122,9 +122,9 @@ public function getName(): string
return $this->templateName;
}

public function getResponse(): Response
public function getResponse(Response|null $preBuiltResponse = null): Response
{
return ($this->onGetResponse)($this);
return ($this->onGetResponse)($this, $preBuiltResponse);
}

// We need to extend from the legacy Template class to keep existing type
Expand Down

0 comments on commit f520dc8

Please sign in to comment.