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

Open in your IDE?
  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\NotAcceptableHttpException;
  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->get('twig')->getLoader()->exists($viewTemplate)) {
  51.             throw new NotAcceptableHttpException(\sprintf('Page does not exist in "%s" format.'$requestFormat));
  52.         }
  53.         // get attributes to render template
  54.         $data $this->getAttributes($attributes$structure$preview);
  55.         // if partial render only content block else full page
  56.         if ($partial) {
  57.             $content $this->renderBlock(
  58.                 $viewTemplate,
  59.                 'content',
  60.                 $data
  61.             );
  62.         } elseif ($preview) {
  63.             $content $this->renderPreview(
  64.                 $viewTemplate,
  65.                 $data
  66.             );
  67.         } else {
  68.             $content $this->renderView(
  69.                 $viewTemplate,
  70.                 $data
  71.             );
  72.         }
  73.         $response = new Response($content);
  74.         // we need to set the content type ourselves here
  75.         // else symfony will use the accept header of the client and the page could be cached with false content-type
  76.         // see following symfony issue: https://github.com/symfony/symfony/issues/35694
  77.         $mimeType $request->getMimeType($requestFormat);
  78.         if ($mimeType) {
  79.             $response->headers->set('Content-Type'$mimeType);
  80.         }
  81.         if (!$preview && $this->getCacheTimeLifeEnhancer()) {
  82.             $this->getCacheTimeLifeEnhancer()->enhance($response$structure);
  83.         }
  84.         return $response;
  85.     }
  86.     /**
  87.      * Generates attributes.
  88.      *
  89.      * @param mixed[] $attributes
  90.      * @param bool $preview
  91.      *
  92.      * @return mixed[]
  93.      */
  94.     protected function getAttributes($attributes, ?StructureInterface $structure null$preview false)
  95.     {
  96.         return $this->get('sulu_website.resolver.parameter')->resolve(
  97.             $attributes,
  98.             $this->get('sulu_core.webspace.request_analyzer'),
  99.             $structure,
  100.             $preview
  101.         );
  102.     }
  103.     /**
  104.      * Returns rendered part of template specified by block.
  105.      */
  106.     protected function renderBlock($template$block$attributes = [])
  107.     {
  108.         $twig $this->get('twig');
  109.         $attributes $twig->mergeGlobals($attributes);
  110.         $template $twig->load($template);
  111.         $level = \ob_get_level();
  112.         \ob_start();
  113.         try {
  114.             $rendered $template->renderBlock($block$attributes);
  115.             \ob_end_clean();
  116.             return $rendered;
  117.         } catch (\Exception $e) {
  118.             while (\ob_get_level() > $level) {
  119.                 \ob_end_clean();
  120.             }
  121.             throw $e;
  122.         }
  123.     }
  124.     protected function renderPreview(string $view, array $parameters = []): string
  125.     {
  126.         $parameters['previewParentTemplate'] = $view;
  127.         $parameters['previewContentReplacer'] = Preview::CONTENT_REPLACER;
  128.         return parent::renderView('@SuluWebsite/Preview/preview.html.twig'$parameters);
  129.     }
  130.     /**
  131.      * Returns the current request from the request stack.
  132.      *
  133.      * @return Request
  134.      *
  135.      * @deprecated will be remove with 2.0
  136.      */
  137.     public function getRequest()
  138.     {
  139.         return $this->get('request_stack')->getCurrentRequest();
  140.     }
  141.     protected function getCacheTimeLifeEnhancer(): ?CacheLifetimeEnhancerInterface
  142.     {
  143.         if (!$this->has('sulu_http_cache.cache_lifetime.enhancer')) {
  144.             return null;
  145.         }
  146.         /** @var CacheLifetimeEnhancerInterface $cacheLifetimeEnhancer */
  147.         $cacheLifetimeEnhancer $this->get('sulu_http_cache.cache_lifetime.enhancer');
  148.         return $cacheLifetimeEnhancer;
  149.     }
  150.     public static function getSubscribedServices()
  151.     {
  152.         $subscribedServices parent::getSubscribedServices();
  153.         $subscribedServices['sulu_website.resolver.parameter'] = ParameterResolverInterface::class;
  154.         $subscribedServices['sulu_core.webspace.request_analyzer'] = RequestAnalyzerInterface::class;
  155.         $subscribedServices['sulu_http_cache.cache_lifetime.enhancer'] = '?' CacheLifetimeEnhancerInterface::class;
  156.         return $subscribedServices;
  157.     }
  158. }