src/EventListener/LayoutsListener.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the OrbitaleCmsBundle package.
  4. *
  5. * (c) Alexandre Rock Ancelet <alex@orbitale.io>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace App\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Twig\Environment;
  15. use Twig\Error\LoaderError;
  16. use Twig\Source;
  17. class LayoutsListener implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var array
  21.      */
  22.     private $layouts;
  23.     /**
  24.      * @var Environment
  25.      */
  26.     private $twig;
  27.     public function __construct(array $layouts = [], Environment $twig)
  28.     {
  29.         $this->layouts $layouts;
  30.         $this->twig    $twig;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::REQUEST => ['setRequestLayout'1],
  39.         ];
  40.     }
  41.     public function setRequestLayout(RequestEvent $event): void
  42.     {
  43.        
  44.         $request $event->getRequest();
  45.         // Get the necessary informations to check them in layout configurations
  46.         $path $request->getPathInfo();
  47.         $host $request->getHost();
  48.         // As a layout must be set, we force it to be empty if no layout is properly configured.
  49.         // Then this will throw an exception, and the user will be warned of the "no-layout" config problem.
  50.         $finalLayout null;
  51.         foreach ($this->layouts as $layoutConfig) {
  52.             $match false;
  53.             // First check host
  54.             if ($layoutConfig['host'] && $host === $layoutConfig['host']) {
  55.                 $match true;
  56.             }
  57.             // Check pattern
  58.             if ($layoutConfig['pattern'] && preg_match('~'.$layoutConfig['pattern'].'~'$path)) {
  59.                 $match true;
  60.             }
  61.             if ($match) {
  62.                 $finalLayout $layoutConfig;
  63.                 break;
  64.             }
  65.         }
  66.         // EDIT 01/05/2021
  67.         // IF NO LAYOUT DO NOTHING
  68.         if(null == $finalLayout)
  69.         return;
  70.         // END EDIT
  71.         // If nothing matches, we take the first layout that has no "host" or "pattern" configuration.
  72.         if (null === $finalLayout) {
  73.             
  74.             $layouts $this->layouts;
  75.             
  76.             do {
  77.                 $finalLayout array_shift($layouts);
  78.                 if ($finalLayout['host'] || $finalLayout['pattern']) {
  79.                     $finalLayout null;
  80.                 }
  81.             } while (null === $finalLayout && count($layouts));
  82.         }
  83.         if (null === $finalLayout || !$this->twig->getLoader()->exists($finalLayout['resource'])) {
  84.             
  85.             $source = new Source(''$finalLayout['resource']);
  86.             throw new LoaderError(sprintf(
  87.                 'Unable to find template %s for layout %s. The "layout" parameter must be a valid twig view to be used as a layout.',
  88.                 $finalLayout['resource'], $finalLayout['name']
  89.             ), 0$source);
  90.         }
  91.         $event->getRequest()->attributes->set('_orbitale_cms_layout'$finalLayout);
  92.     }
  93. }