vendor/contao/core-bundle/src/Routing/ResponseContext/CoreResponseContextFactory.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Routing\ResponseContext;
  11. use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
  12. use Contao\CoreBundle\Routing\ResponseContext\JsonLd\ContaoPageSchema;
  13. use Contao\CoreBundle\Routing\ResponseContext\JsonLd\JsonLdManager;
  14. use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
  15. use Contao\PageModel;
  16. use Contao\StringUtil;
  17. use Spatie\SchemaOrg\WebPage;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. class CoreResponseContextFactory
  20. {
  21.     /**
  22.      * @var ResponseContextAccessor
  23.      */
  24.     private $responseContextAccessor;
  25.     /**
  26.      * @var EventDispatcherInterface
  27.      */
  28.     private $eventDispatcher;
  29.     /**
  30.      * @var TokenChecker
  31.      */
  32.     private $tokenChecker;
  33.     public function __construct(ResponseContextAccessor $responseContextAccessorEventDispatcherInterface $eventDispatcherTokenChecker $tokenChecker)
  34.     {
  35.         $this->responseContextAccessor $responseContextAccessor;
  36.         $this->eventDispatcher $eventDispatcher;
  37.         $this->tokenChecker $tokenChecker;
  38.     }
  39.     public function createResponseContext(): ResponseContext
  40.     {
  41.         $context = new ResponseContext();
  42.         $this->responseContextAccessor->setResponseContext($context);
  43.         return $context;
  44.     }
  45.     public function createWebpageResponseContext(): ResponseContext
  46.     {
  47.         $context $this->createResponseContext();
  48.         $context->add($this->eventDispatcher);
  49.         $context->addLazy(HtmlHeadBag::class);
  50.         $context->addLazy(
  51.             JsonLdManager::class,
  52.             static function () use ($context) {
  53.                 $manager = new JsonLdManager($context);
  54.                 $manager->getGraphForSchema(JsonLdManager::SCHEMA_ORG)->add(new WebPage());
  55.                 return $manager;
  56.             }
  57.         );
  58.         return $context;
  59.     }
  60.     public function createContaoWebpageResponseContext(PageModel $pageModel): ResponseContext
  61.     {
  62.         $context $this->createWebpageResponseContext();
  63.         /** @var HtmlHeadBag $htmlHeadBag */
  64.         $htmlHeadBag $context->get(HtmlHeadBag::class);
  65.         /** @var JsonLdManager $jsonLdManager */
  66.         $jsonLdManager $context->get(JsonLdManager::class);
  67.         $title StringUtil::inputEncodedToPlainText($pageModel->pageTitle ?: $pageModel->title ?: '');
  68.         $htmlHeadBag
  69.             ->setTitle($title ?: '')
  70.             ->setMetaDescription(StringUtil::inputEncodedToPlainText($pageModel->description ?: ''))
  71.         ;
  72.         if ($pageModel->robots) {
  73.             $htmlHeadBag->setMetaRobots($pageModel->robots);
  74.         }
  75.         $jsonLdManager
  76.             ->getGraphForSchema(JsonLdManager::SCHEMA_CONTAO)
  77.             ->set(
  78.                 new ContaoPageSchema(
  79.                     $title ?: '',
  80.                     (int) $pageModel->id,
  81.                     (bool) $pageModel->noSearch,
  82.                     (bool) $pageModel->protected,
  83.                     array_map('intval'array_filter((array) $pageModel->groups)),
  84.                     $this->tokenChecker->isPreviewMode()
  85.                 )
  86.             )
  87.         ;
  88.         return $context;
  89.     }
  90. }