vendor/kunstmaan/node-bundle/Controller/SlugController.php line 65

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\NodeBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface;
  5. use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
  6. use Kunstmaan\NodeBundle\Entity\NodeTranslation;
  7. use Kunstmaan\NodeBundle\Entity\NodeVersion;
  8. use Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface;
  9. use Kunstmaan\NodeBundle\Event\Events;
  10. use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
  11. use Kunstmaan\NodeBundle\Helper\NodeMenu;
  12. use Kunstmaan\NodeBundle\Helper\RenderContext;
  13. use Psr\Container\ContainerInterface as PsrContainerInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. final class SlugController extends AbstractController
  22. {
  23.     /** @var NodeMenu */
  24.     private $nodeMenu;
  25.     /** @var PsrContainerInterface */
  26.     private $viewDataProviderServiceLocator;
  27.     /** @var EventDispatcherInterface */
  28.     private $eventDispatcher;
  29.     /** @var EntityManagerInterface */
  30.     private $em;
  31.     public function __construct(NodeMenu $nodeMenuPsrContainerInterface $viewDataProviderServiceLocatorEventDispatcherInterface $eventDispatcherEntityManagerInterface $em)
  32.     {
  33.         $this->nodeMenu $nodeMenu;
  34.         $this->viewDataProviderServiceLocator $viewDataProviderServiceLocator;
  35.         $this->eventDispatcher $eventDispatcher;
  36.         $this->em $em;
  37.     }
  38.     /**
  39.      * Handle the page requests
  40.      *
  41.      * @param Request $request The request
  42.      * @param string  $url     The url
  43.      * @param bool    $preview Show in preview mode
  44.      *
  45.      * @throws NotFoundHttpException
  46.      * @throws AccessDeniedException
  47.      */
  48.     public function slugAction(Request $request$url null$preview false): Response|array
  49.     {
  50.         $locale $request->getLocale();
  51.         /* @var NodeTranslation $nodeTranslation */
  52.         $nodeTranslation $request->attributes->get('_nodeTranslation');
  53.         // If no node translation -> 404
  54.         if (!$nodeTranslation) {
  55.             throw $this->createNotFoundException('No page found for slug ' $url);
  56.         }
  57.         $entity $this->getPageEntity(
  58.             $request,
  59.             $preview,
  60.             $this->em,
  61.             $nodeTranslation
  62.         );
  63.         $node $nodeTranslation->getNode();
  64.         $securityEvent = new SlugSecurityEvent();
  65.         $securityEvent
  66.             ->setNode($node)
  67.             ->setEntity($entity)
  68.             ->setRequest($request)
  69.             ->setNodeTranslation($nodeTranslation);
  70.         $nodeMenu $this->nodeMenu;
  71.         $nodeMenu->setLocale($locale);
  72.         $nodeMenu->setCurrentNode($node);
  73.         $nodeMenu->setIncludeOffline($preview);
  74.         $this->eventDispatcher->dispatch($securityEventEvents::SLUG_SECURITY);
  75.         // render page
  76.         $renderContext = new RenderContext(
  77.             [
  78.                 'nodetranslation' => $nodeTranslation,
  79.                 'slug' => $url,
  80.                 'page' => $entity,
  81.                 'resource' => $entity,
  82.                 'nodemenu' => $nodeMenu,
  83.             ]
  84.         );
  85.         if (method_exists($entity'getDefaultView')) {
  86.             $renderContext->setView($entity->getDefaultView());
  87.         }
  88.         $response null;
  89.         if ($entity instanceof CustomViewDataProviderInterface) {
  90.             $serviceId $entity->getViewDataProviderServiceId();
  91.             if (!$this->viewDataProviderServiceLocator->has($serviceId)) {
  92.                 throw new \RuntimeException(sprintf('Missing page renderer service "%s"'$serviceId));
  93.             }
  94.             /** @var PageViewDataProviderInterface $service */
  95.             $service $this->viewDataProviderServiceLocator->get($serviceId);
  96.             $service->provideViewData($nodeTranslation$renderContext);
  97.             $response $renderContext->getResponse();
  98.         }
  99.         if ($response instanceof Response) {
  100.             return $response;
  101.         }
  102.         $view $renderContext->getView();
  103.         if (empty($view)) {
  104.             throw $this->createNotFoundException(sprintf('Missing view path for page "%s"'\get_class($entity)));
  105.         }
  106.         $template = new Template([]);
  107.         $template->setTemplate($view);
  108.         $template->setOwner([SlugController::class, 'slugAction']);
  109.         $request->attributes->set('_template'$template);
  110.         return $renderContext->getArrayCopy();
  111.     }
  112.     /**
  113.      * @param bool $preview
  114.      */
  115.     private function getPageEntity(Request $request$previewEntityManagerInterface $emNodeTranslation $nodeTranslation): \Kunstmaan\NodeBundle\Entity\HasNodeInterface
  116.     {
  117.         /* @var HasNodeInterface $entity */
  118.         $entity null;
  119.         if ($preview) {
  120.             $version $request->query->get('version');
  121.             if (!empty($version) && is_numeric($version)) {
  122.                 $nodeVersion $em->getRepository(NodeVersion::class)->find($version);
  123.                 if (!\is_null($nodeVersion)) {
  124.                     $entity $nodeVersion->getRef($em);
  125.                 }
  126.             }
  127.         }
  128.         if (\is_null($entity)) {
  129.             $entity $nodeTranslation->getPublicNodeVersion()->getRef($em);
  130.             return $entity;
  131.         }
  132.         return $entity;
  133.     }
  134. }