vendor/sulu/headless-bundle/Controller/HeadlessWebsiteController.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Sulu.
  5.  *
  6.  * (c) Sulu GmbH
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace Sulu\Bundle\HeadlessBundle\Controller;
  12. use Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface;
  13. use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController;
  14. use Sulu\Component\Content\Compat\PageInterface;
  15. use Sulu\Component\Content\Compat\StructureInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. class HeadlessWebsiteController extends WebsiteController
  19. {
  20.     /**
  21.      * We cannot set the typehint of the $structure parameter to PageInterface because the ArticleBundle does not
  22.      * implement that interface. Therefore we need to define the type via phpdoc to satisfy phpstan.
  23.      *
  24.      * @param PageInterface $structure
  25.      */
  26.     public function indexAction(
  27.         Request $request,
  28.         StructureInterface $structure,
  29.         bool $preview false,
  30.         bool $partial false
  31.     ): Response {
  32.         $headlessData $this->resolveStructure($structure);
  33.         if ('json' !== $request->getRequestFormat()) {
  34.             return $this->renderStructure($structure, ['headless' => $headlessData], $preview$partial);
  35.         }
  36.         $response = new Response($this->serializeData($headlessData));
  37.         $response->headers->set('Content-Type''application/json');
  38.         $cacheLifetimeEnhancer $this->getCacheTimeLifeEnhancer();
  39.         if (!$preview && $cacheLifetimeEnhancer) {
  40.             $cacheLifetimeEnhancer->enhance($response$structure);
  41.         }
  42.         return $response;
  43.     }
  44.     /**
  45.      * @return mixed[]
  46.      */
  47.     protected function resolveStructure(StructureInterface $structure): array
  48.     {
  49.         /** @var StructureResolverInterface $structureResolver */
  50.         $structureResolver $this->container->get('sulu_headless.structure_resolver');
  51.         return $structureResolver->resolve($structure$structure->getLanguageCode());
  52.     }
  53.     /**
  54.      * @param mixed[] $data
  55.      */
  56.     protected function serializeData(array $data): string
  57.     {
  58.         return \json_encode($data\JSON_THROW_ON_ERROR);
  59.     }
  60.     /**
  61.      * @return mixed[]
  62.      */
  63.     public static function getSubscribedServices(): array
  64.     {
  65.         $subscribedServices parent::getSubscribedServices();
  66.         $subscribedServices['sulu_headless.structure_resolver'] = StructureResolverInterface::class;
  67.         return $subscribedServices;
  68.     }
  69. }