vendor/pimcore/pimcore/lib/Document/Renderer/DocumentRenderer.php line 145

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Document\Renderer;
  16. use Pimcore\Event\DocumentEvents;
  17. use Pimcore\Event\Model\DocumentEvent;
  18. use Pimcore\Http\RequestHelper;
  19. use Pimcore\Localization\LocaleService;
  20. use Pimcore\Model\Document;
  21. use Pimcore\Routing\Dynamic\DocumentRouteHandler;
  22. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  23. use Pimcore\Templating\Renderer\ActionRenderer;
  24. use Pimcore\Twig\Extension\Templating\Placeholder\ContainerService;
  25. use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
  26. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  27. class DocumentRenderer implements DocumentRendererInterface
  28. {
  29.     /**
  30.      * @var RequestHelper
  31.      */
  32.     private $requestHelper;
  33.     /**
  34.      * @var ActionRenderer
  35.      */
  36.     private $actionRenderer;
  37.     /**
  38.      * @var FragmentRendererInterface
  39.      */
  40.     private $fragmentRenderer;
  41.     /**
  42.      * @var DocumentRouteHandler
  43.      */
  44.     private $documentRouteHandler;
  45.     /**
  46.      * @var DocumentTargetingConfigurator
  47.      */
  48.     private $targetingConfigurator;
  49.     /**
  50.      * @var EventDispatcherInterface
  51.      */
  52.     private $eventDispatcher;
  53.     /**
  54.      * @var LocaleService
  55.      */
  56.     private $localeService;
  57.     /**
  58.      * @param RequestHelper $requestHelper
  59.      * @param ActionRenderer $actionRenderer
  60.      * @param FragmentRendererInterface $fragmentRenderer
  61.      * @param DocumentRouteHandler $documentRouteHandler
  62.      * @param DocumentTargetingConfigurator $targetingConfigurator
  63.      * @param EventDispatcherInterface $eventDispatcher
  64.      * @param LocaleService $localeService
  65.      */
  66.     public function __construct(
  67.         RequestHelper $requestHelper,
  68.         ActionRenderer $actionRenderer,
  69.         FragmentRendererInterface $fragmentRenderer,
  70.         DocumentRouteHandler $documentRouteHandler,
  71.         DocumentTargetingConfigurator $targetingConfigurator,
  72.         EventDispatcherInterface $eventDispatcher,
  73.         LocaleService $localeService
  74.     ) {
  75.         $this->requestHelper $requestHelper;
  76.         $this->actionRenderer $actionRenderer;
  77.         $this->fragmentRenderer $fragmentRenderer;
  78.         $this->documentRouteHandler $documentRouteHandler;
  79.         $this->targetingConfigurator $targetingConfigurator;
  80.         $this->eventDispatcher $eventDispatcher;
  81.         $this->localeService $localeService;
  82.     }
  83.     /**
  84.      * @required
  85.      *
  86.      * @param ContainerService $containerService
  87.      */
  88.     public function setContainerService(ContainerService $containerService)
  89.     {
  90.         // we have to ensure that the ContainerService was initialized at the time this service is created
  91.         // this is necessary, since the ContainerService registers a listener for DocumentEvents::RENDERER_PRE_RENDER
  92.         // which wouldn't be called if the ContainerService would be lazy initialized when the first
  93.         // placeholder service/templating helper is used during the rendering process
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function render(Document\PageSnippet $document, array $attributes = [], array $query = [], array $options = []): string
  99.     {
  100.         $this->eventDispatcher->dispatch(
  101.             new DocumentEvent($document$attributes),
  102.             DocumentEvents::RENDERER_PRE_RENDER
  103.         );
  104.         // apply best matching target group (if any)
  105.         $this->targetingConfigurator->configureTargetGroup($document);
  106.         // add document route to request if no route is set
  107.         // this is needed for logic relying on the current route (e.g. pimcoreUrl helper)
  108.         if (!isset($attributes['_route'])) {
  109.             $route $this->documentRouteHandler->buildRouteForDocument($document);
  110.             if (null !== $route) {
  111.                 $attributes['_route'] = $route->getRouteKey();
  112.             }
  113.         }
  114.         try {
  115.             $request $this->requestHelper->getCurrentRequest();
  116.         } catch (\Exception $e) {
  117.             $request $this->requestHelper->createRequestWithContext();
  118.         }
  119.         $documentLocale $document->getProperty('language');
  120.         $tempLocale $this->localeService->getLocale();
  121.         if ($documentLocale) {
  122.             $this->localeService->setLocale($documentLocale);
  123.             $request->setLocale($documentLocale);
  124.         }
  125.         $uri $this->actionRenderer->createDocumentReference($document$attributes$query);
  126.         $response $this->fragmentRenderer->render($uri$request$options);
  127.         $this->localeService->setLocale($tempLocale);
  128.         $this->eventDispatcher->dispatch(
  129.             new DocumentEvent($document$attributes),
  130.             DocumentEvents::RENDERER_POST_RENDER
  131.         );
  132.         return $response->getContent();
  133.     }
  134. }