From 194363cdd3271a33c1213745fc47371717d83201 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 8 Aug 2024 17:51:26 +0200 Subject: [PATCH] Add own implementation of Controller --- .../Controller/Website/ContentController.php | 82 ++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/Content/UserInterface/Controller/Website/ContentController.php b/Content/UserInterface/Controller/Website/ContentController.php index f98c9000..b9872d1f 100644 --- a/Content/UserInterface/Controller/Website/ContentController.php +++ b/Content/UserInterface/Controller/Website/ContentController.php @@ -13,12 +13,90 @@ namespace Sulu\Bundle\ContentBundle\Content\UserInterface\Controller\Website; -use Sulu\Bundle\WebsiteBundle\Controller\DefaultController; +use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; +use Sulu\Bundle\PreviewBundle\Preview\Preview; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; /** * TODO this controller will later replace the DefaultController of the WebsiteBundle * and should be base for all content controllers. + * + * @template T of DimensionContentInterface */ -class ContentController extends DefaultController +class ContentController extends AbstractController { + /** + * @param T $object + */ + public function indexAction( + Request $request, + DimensionContentInterface $object, + string $view, // TODO maybe inject metadata where we also get the cachelifetime from + bool $preview, + bool $partial, + ): Response { + $requestFormat = $request->getRequestFormat() ?? 'html'; + + $parameters = $this->resolveSuluParameters($object, $requestFormat === 'json'); + + if ($requestFormat === 'json') { + $response = new JsonResponse($parameters); + } else { + $response = new Response($this->renderSuluView($view, $requestFormat, $parameters, $preview, $partial)); + } + + $response->setPublic(); + + // TODO resolve cachelifetime + // TODO implement cacheLifetimeRequestStore + + return $response; + } + + /** + * @param T $object + * + * @return array + */ + protected function resolveSuluParameters(DimensionContentInterface $object, bool $normalize): array + { + return [ + 'resource' => $object->getResource(), // TODO normalize JSON response + + // TODO resolve content + ]; + } + + /** + * @param array $parameters + * + * @throws NotAcceptableHttpException + */ + protected function renderSuluView( + string $view, + string $requestFormat, // TODO maybe we should avoid this and resolve it before + array $parameters, + bool $preview, + bool $partial, + ): string { + $viewTemplate = $view . '.' . $requestFormat . '.twig'; + + if (!$this->container->get('twig')->getLoader()->exists($viewTemplate)) { + throw new NotAcceptableHttpException(\sprintf('Page does not exist in "%s" format.', $requestFormat)); + } + + if ($partial) { + return $this->renderBlockView($viewTemplate, 'content', $parameters); + } elseif ($preview) { + $parameters['previewParentTemplate'] = $viewTemplate; + $parameters['previewContentReplacer'] = Preview::CONTENT_REPLACER; + $viewTemplate = '@SuluWebsite/Preview/preview.html.twig'; + } + + return $this->renderView($viewTemplate, $parameters); + } }