vendor/sulu/article-bundle/Controller/WebsiteArticleController.php line 37

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\ArticleBundle\Controller;
  11. use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
  12. use Sulu\Bundle\ArticleBundle\Document\ArticleInterface;
  13. use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
  14. use Sulu\Bundle\ArticleBundle\Resolver\ArticleContentResolverInterface;
  15. use Sulu\Bundle\HttpCacheBundle\Cache\SuluHttpCache;
  16. use Sulu\Bundle\PreviewBundle\Preview\Preview;
  17. use Sulu\Bundle\WebsiteBundle\Resolver\ParameterResolverInterface;
  18. use Sulu\Bundle\WebsiteBundle\Resolver\TemplateAttributeResolverInterface;
  19. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Exception\HttpException;
  24. use Twig\Environment;
  25. /**
  26.  * Handles articles.
  27.  */
  28. class WebsiteArticleController extends AbstractController
  29. {
  30.     /**
  31.      * Article index action.
  32.      */
  33.     public function indexAction(
  34.         Request $request,
  35.         ArticleInterface $object,
  36.         string $view,
  37.         int $pageNumber 1,
  38.         bool $preview false,
  39.         bool $partial false
  40.     ): Response {
  41.         return $this->renderArticle($request$object$view$pageNumber$preview$partial);
  42.     }
  43.     /**
  44.      * Render article with given view.
  45.      */
  46.     protected function renderArticle(
  47.         Request $request,
  48.         ArticleInterface $object,
  49.         string $view,
  50.         int $pageNumber,
  51.         bool $preview,
  52.         bool $partial,
  53.         array $attributes = []
  54.     ): Response {
  55.         $object $this->normalizeArticle($object);
  56.         $requestFormat $request->getRequestFormat();
  57.         $viewTemplate $view '.' $requestFormat '.twig';
  58.         $content $this->resolveArticle($object$pageNumber);
  59.         $parameters $this->container->get('sulu_website.resolver.parameter')->resolve(
  60.             [],
  61.             $this->container->get('sulu_core.webspace.request_analyzer'),
  62.             null,
  63.             $preview
  64.         );
  65.         $data = \array_merge($parameters$content$attributes);
  66.         try {
  67.             if ($partial) {
  68.                 $response $this->createResponse($request);
  69.                 $response->setContent(
  70.                     $this->renderBlock(
  71.                         $viewTemplate,
  72.                         'content',
  73.                         $data
  74.                     )
  75.                 );
  76.                 return $response;
  77.             } elseif ($preview) {
  78.                 $parameters = [
  79.                     'previewParentTemplate' => $viewTemplate,
  80.                     'previewContentReplacer' => Preview::CONTENT_REPLACER,
  81.                 ];
  82.                 return $this->render(
  83.                     '@SuluWebsite/Preview/preview.html.twig',
  84.                     \array_merge($data$parameters),
  85.                     $this->createResponse($request)
  86.                 );
  87.             } else {
  88.                 return $this->render(
  89.                     $viewTemplate,
  90.                     $data,
  91.                     $this->createResponse($request)
  92.                 );
  93.             }
  94.         } catch (\InvalidArgumentException $exception) {
  95.             // template not found
  96.             throw new HttpException(406'Error encountered when rendering content'$exception);
  97.         }
  98.     }
  99.     /**
  100.      * Returns all the times the article-document.
  101.      * This is necessary because the preview system passes an article-page here.
  102.      */
  103.     protected function normalizeArticle(ArticleInterface $object): ArticleDocument
  104.     {
  105.         if ($object instanceof ArticlePageDocument) {
  106.             return $object->getParent();
  107.         }
  108.         return $object;
  109.     }
  110.     /**
  111.      * Serialize given article with page-number.
  112.      */
  113.     protected function resolveArticle(ArticleInterface $objectint $pageNumber): array
  114.     {
  115.         $articleContentResolver $this->getArticleContentResolver();
  116.         return $articleContentResolver->resolve($object$pageNumber);
  117.     }
  118.     /**
  119.      * Create response.
  120.      */
  121.     private function createResponse(Request $request): Response
  122.     {
  123.         $response = new Response();
  124.         $cacheLifetime $request->attributes->get('_cacheLifetime');
  125.         if ($cacheLifetime) {
  126.             $response->setPublic();
  127.             $response->headers->set(
  128.                 SuluHttpCache::HEADER_REVERSE_PROXY_TTL,
  129.                 $cacheLifetime
  130.             );
  131.             $response->setMaxAge($this->getParameter('sulu_http_cache.cache.max_age'));
  132.             $response->setSharedMaxAge($this->getParameter('sulu_http_cache.cache.shared_max_age'));
  133.         }
  134.         // we need to set the content type ourselves here
  135.         // else symfony will use the accept header of the client and the page could be cached with false content-type
  136.         // see following symfony issue: https://github.com/symfony/symfony/issues/35694
  137.         $mimeType $request->getMimeType($request->getRequestFormat());
  138.         if ($mimeType) {
  139.             $response->headers->set('Content-Type'$mimeType);
  140.         }
  141.         return $response;
  142.     }
  143.     protected function renderBlock(string $templatestring $block, array $attributes = []): string
  144.     {
  145.         $twig $this->getTwig();
  146.         $attributes $twig->mergeGlobals($attributes);
  147.         $template $twig->load($template);
  148.         $level = \ob_get_level();
  149.         \ob_start();
  150.         try {
  151.             $rendered $template->renderBlock($block$attributes);
  152.             \ob_end_clean();
  153.             return $rendered;
  154.         } catch (\Exception $e) {
  155.             while (\ob_get_level() > $level) {
  156.                 \ob_end_clean();
  157.             }
  158.             throw $e;
  159.         }
  160.     }
  161.     protected function getTwig(): Environment
  162.     {
  163.         return $this->container->get('twig');
  164.     }
  165.     /**
  166.      * @deprecated
  167.      */
  168.     protected function getTemplateAttributeResolver(): TemplateAttributeResolverInterface
  169.     {
  170.         @\trigger_error(__METHOD__ '() is deprecated since version 2.2 and will be removed in 3.0. If you need the service, you can inject it by yourself instead.', \E_USER_DEPRECATED);
  171.         return $this->container->get('sulu_website.resolver.template_attribute');
  172.     }
  173.     protected function getArticleContentResolver(): ArticleContentResolverInterface
  174.     {
  175.         return $this->container->get('sulu_article.article_content_resolver');
  176.     }
  177.     public static function getSubscribedServices(): array
  178.     {
  179.         $subscribedServices parent::getSubscribedServices();
  180.         $subscribedServices['twig'] = Environment::class;
  181.         $subscribedServices['sulu_website.resolver.template_attribute'] = TemplateAttributeResolverInterface::class;
  182.         $subscribedServices['sulu_article.article_content_resolver'] = ArticleContentResolverInterface::class;
  183.         $subscribedServices['sulu_website.resolver.parameter'] = ParameterResolverInterface::class;
  184.         $subscribedServices['sulu_core.webspace.request_analyzer'] = RequestAnalyzerInterface::class;
  185.         return $subscribedServices;
  186.     }
  187. }