vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Controller/WebsiteController.php line 81

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\WebsiteBundle\Controller;
  11. use Sulu\Bundle\HttpCacheBundle\CacheLifetime\CacheLifetimeEnhancerInterface;
  12. use Sulu\Bundle\PreviewBundle\Preview\Preview;
  13. use Sulu\Bundle\WebsiteBundle\Resolver\ParameterResolverInterface;
  14. use Sulu\Component\Content\Compat\StructureInterface;
  15. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\Exception\HttpException;
  20. /**
  21.  * Basic class to render Website from phpcr content.
  22.  */
  23. abstract class WebsiteController extends AbstractController
  24. {
  25.     /**
  26.      * Returns a rendered structure.
  27.      *
  28.      * @param StructureInterface $structure The structure, which has been loaded for rendering
  29.      * @param array $attributes Additional attributes, which will be passed to twig
  30.      * @param bool $preview Defines if the site is rendered in preview mode
  31.      * @param bool $partial Defines if only the content block of the template should be rendered
  32.      *
  33.      * @return Response
  34.      */
  35.     protected function renderStructure(
  36.         StructureInterface $structure,
  37.         $attributes = [],
  38.         $preview false,
  39.         $partial false
  40.     ) {
  41.         /** @var Request $request */
  42.         $request $this->getRequest();
  43.         // extract format twig file
  44.         if (!$preview) {
  45.             $requestFormat $request->getRequestFormat();
  46.         } else {
  47.             $requestFormat 'html';
  48.         }
  49.         $viewTemplate $structure->getView() . '.' $requestFormat '.twig';
  50.         if (!$this->container->get('twig')->getLoader()->exists($viewTemplate)) {
  51.             throw new HttpException(
  52.                 406,
  53.                 \sprintf('Page does not exist in "%s" format.'$requestFormat)
  54.             );
  55.         }
  56.         // get attributes to render template
  57.         $data $this->getAttributes($attributes$structure$preview);
  58.         // if partial render only content block else full page
  59.         if ($partial) {
  60.             $content $this->renderBlock(
  61.                 $viewTemplate,
  62.                 'content',
  63.                 $data
  64.             );
  65.         } elseif ($preview) {
  66.             $content $this->renderPreview(
  67.                 $viewTemplate,
  68.                 $data
  69.             );
  70.         } else {
  71.             $content $this->renderView(
  72.                 $viewTemplate,
  73.                 $data
  74.             );
  75.         }
  76.         $response = new Response($content);
  77.         // we need to set the content type ourselves here
  78.         // else symfony will use the accept header of the client and the page could be cached with false content-type
  79.         // see following symfony issue: https://github.com/symfony/symfony/issues/35694
  80.         $mimeType $request->getMimeType($requestFormat);
  81.         if ($mimeType) {
  82.             $response->headers->set('Content-Type'$mimeType);
  83.         }
  84.         if (!$preview && $this->getCacheTimeLifeEnhancer()) {
  85.             $this->getCacheTimeLifeEnhancer()->enhance($response$structure);
  86.         }
  87.         return $response;
  88.     }
  89.     /**
  90.      * Generates attributes.
  91.      *
  92.      * @param mixed[] $attributes
  93.      * @param bool $preview
  94.      *
  95.      * @return mixed[]
  96.      */
  97.     protected function getAttributes($attributesStructureInterface $structure null$preview false)
  98.     {
  99.         return $this->container->get('sulu_website.resolver.parameter')->resolve(
  100.             $attributes,
  101.             $this->container->get('sulu_core.webspace.request_analyzer'),
  102.             $structure,
  103.             $preview
  104.         );
  105.     }
  106.     /**
  107.      * Returns rendered part of template specified by block.
  108.      */
  109.     protected function renderBlock($template$block$attributes = [])
  110.     {
  111.         $twig $this->container->get('twig');
  112.         $attributes $twig->mergeGlobals($attributes);
  113.         $template $twig->load($template);
  114.         $level \ob_get_level();
  115.         \ob_start();
  116.         try {
  117.             $rendered $template->renderBlock($block$attributes);
  118.             \ob_end_clean();
  119.             return $rendered;
  120.         } catch (\Exception $e) {
  121.             while (\ob_get_level() > $level) {
  122.                 \ob_end_clean();
  123.             }
  124.             throw $e;
  125.         }
  126.     }
  127.     protected function renderPreview(string $view, array $parameters = []): string
  128.     {
  129.         $parameters['previewParentTemplate'] = $view;
  130.         $parameters['previewContentReplacer'] = Preview::CONTENT_REPLACER;
  131.         return parent::renderView('@SuluWebsite/Preview/preview.html.twig'$parameters);
  132.     }
  133.     /**
  134.      * Returns the current request from the request stack.
  135.      *
  136.      * @return Request
  137.      *
  138.      * @deprecated will be remove with 2.0
  139.      */
  140.     public function getRequest()
  141.     {
  142.         return $this->container->get('request_stack')->getCurrentRequest();
  143.     }
  144.     protected function getCacheTimeLifeEnhancer(): ?CacheLifetimeEnhancerInterface
  145.     {
  146.         if (!$this->container->has('sulu_http_cache.cache_lifetime.enhancer')) {
  147.             return null;
  148.         }
  149.         /** @var CacheLifetimeEnhancerInterface $cacheLifetimeEnhancer */
  150.         $cacheLifetimeEnhancer $this->container->get('sulu_http_cache.cache_lifetime.enhancer');
  151.         return $cacheLifetimeEnhancer;
  152.     }
  153.     public static function getSubscribedServices(): array
  154.     {
  155.         $subscribedServices parent::getSubscribedServices();
  156.         $subscribedServices['sulu_website.resolver.parameter'] = ParameterResolverInterface::class;
  157.         $subscribedServices['sulu_core.webspace.request_analyzer'] = RequestAnalyzerInterface::class;
  158.         $subscribedServices['sulu_http_cache.cache_lifetime.enhancer'] = '?' CacheLifetimeEnhancerInterface::class;
  159.         return $subscribedServices;
  160.     }
  161. }