src/Controller/Page/RoutingController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Page;
  3. use App\Component\Configuration\Util\Config;
  4. use App\Entity\HeroConfiguration;
  5. use App\Entity\OgImage;
  6. use App\Entity\Route;
  7. use App\Event\PageEvent;
  8. use App\EventListener\FeatureFlagListener;
  9. use App\Model\Tealium\Base as TealiumBase;
  10. use App\Renderer\Page as PageRenderer;
  11. use App\Util\Breadcrumb;
  12. use App\Util\CookiescriptHelper;
  13. use App\Util\Seo;
  14. use Flagception\Manager\FeatureManagerInterface;
  15. use FOS\HttpCacheBundle\Http\SymfonyResponseTagger;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  20. /**
  21.  * A custom controller to handle a content specified by a route.
  22.  */
  23. class RoutingController extends AbstractController
  24. {
  25.     private PageRenderer $pageRenderer;
  26.     private Breadcrumb $breadcrumb;
  27.     private Seo $seo;
  28.     private EventDispatcherInterface $dispatcher;
  29.     private FeatureManagerInterface $featureManager;
  30.     public function __construct(
  31.         PageRenderer $pageRenderer,
  32.         Breadcrumb $breadcrumb,
  33.         Seo $seo,
  34.         EventDispatcherInterface $dispatcher,
  35.         FeatureManagerInterface $featureManager
  36.     ) {
  37.         $this->pageRenderer $pageRenderer;
  38.         $this->breadcrumb $breadcrumb;
  39.         $this->seo $seo;
  40.         $this->dispatcher $dispatcher;
  41.         $this->featureManager $featureManager;
  42.     }
  43.     public function resolve(Request $requestConfig $configSymfonyResponseTagger $responseTagger): Response
  44.     {
  45.         /** @var Route $route */
  46.         $route $this->getDoctrine()->getRepository(Route::class)->findOneBy([
  47.             'name' => $request->get('_route'),
  48.         ]);
  49.         if (!$page $route->getPage()) {
  50.             throw $this->createNotFoundException();
  51.         }
  52.         $responseTagger->addTags(['pages''pages-'.$page->getId()]);
  53.         $this->getDoctrine()->getManager()->getFilters()->disable('softdeleteable');
  54.         if ($page->isDeleted()) {
  55.             throw $this->createNotFoundException();
  56.         }
  57.         $this->getDoctrine()->getManager()->getFilters()->enable('softdeleteable');
  58.         $this->seo->setSeoInformationForPage([], $page, (new OgImage())->setImage($config->get('site_open_graph_image')));
  59.         $cacheable true;
  60.         $this->dispatcher->dispatch(new PageEvent(page$pagerequest$request), PageEvent::PAGE_BEFORE_RENDER);
  61.         if (str_contains($page->getBody(), '{open_applicant_form}')) {
  62.             $cacheable false;
  63.             $body $this->forward('App\Controller\Page\ApplicantSnippetController:snippetAction')->getContent();
  64.             $page->setBody(str_replace('{open_applicant_form}'$body$page->getBody()));
  65.         }
  66.         if (str_contains($page->getBody(), '{product_invoice_form')) {
  67.             preg_match('{product_invoice_form:?(\d*)}'$page->getBody(), $matches);
  68.             if (!empty($matches[1])) {
  69.                 $cacheable false;
  70.                 $body $this->forward(
  71.                     ProductController::class.':snippet',
  72.                     ['id' => $matches[1]]
  73.                 )->getContent();
  74.                 $page->setBody(str_replace('{'.$matches[0].'}'$body$page->getBody()));
  75.             }
  76.         }
  77.         if (str_contains($page->getBody(), '{products_by_tag:')) {
  78.             preg_match('{products_by_tag:?(\d*)}'$page->getBody(), $matches);
  79.             if (!empty($matches[1])) {
  80.                 $cacheable false;
  81.                 $body $this->forward(
  82.                     ProductsController::class.':snippet',
  83.                     ['tagId' => $matches[1]]
  84.                 )->getContent();
  85.                 $page->setBody(str_replace('{'.$matches[0].'}'$body$page->getBody()));
  86.             }
  87.         }
  88.         $hero null;
  89.         if ($this->featureManager->isActive(FeatureFlagListener::FEATURE_HERO) && $page->getHeroConfiguration() instanceof HeroConfiguration) {
  90.             $hero $this->renderView(
  91.                 '@default/pages/hero_configuration.html.twig',
  92.                 [
  93.                     'heroConfiguration' => $page->getHeroConfiguration(),
  94.                 ]
  95.             );
  96.         }
  97.         $response $this->render(
  98.             '@default/pages/pages_index.html.twig',
  99.             [
  100.                 'header' => $this->pageRenderer->getHeader($page),
  101.                 'footer' => $this->pageRenderer->getFooter(),
  102.                 'page' => $page,
  103.                 'breadcrumbs' => $this->breadcrumb->getBreadcrumbsForPage($page),
  104.                 'locale' => $route->getLocale(),
  105.                 'hero' => $hero,
  106.                 'tealium_data' => TealiumBase::createForPage($request$page$route)->toJson(),
  107.                 'tealium_push_directly' => true,
  108.             ]
  109.         );
  110.         $response->setContent(
  111.             CookiescriptHelper::cookiefyMarkup($response->getContent())
  112.         );
  113.         if ('404_page' === $page->getAlias()) {
  114.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  115.         }
  116.         if ('500_page' === $page->getAlias()) {
  117.             $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  118.         }
  119.         if (!$cacheable) {
  120.             $response
  121.                 ->setMaxAge(0)
  122.                 ->setSharedMaxAge(0)
  123.                 ->setPrivate()
  124.                 ->headers->addCacheControlDirective('must-revalidate')
  125.             ;
  126.         }
  127.         return $response;
  128.     }
  129. }