vendor/orbitale/cms-bundle/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 Orbitale\Bundle\CmsBundle\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 $layoutsEnvironment $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.         $request $event->getRequest();
  44.         // Get the necessary informations to check them in layout configurations
  45.         $path $request->getPathInfo();
  46.         $host $request->getHost();
  47.         // As a layout must be set, we force it to be empty if no layout is properly configured.
  48.         // Then this will throw an exception, and the user will be warned of the "no-layout" config problem.
  49.         $finalLayout null;
  50.         foreach ($this->layouts as $layoutConfig) {
  51.             $match false;
  52.             // First check host
  53.             if ($layoutConfig['host'] && $host === $layoutConfig['host']) {
  54.                 $match true;
  55.             }
  56.             // Check pattern
  57.             if ($layoutConfig['pattern'] && preg_match('~'.$layoutConfig['pattern'].'~'$path)) {
  58.                 $match true;
  59.             }
  60.             if ($match) {
  61.                 $finalLayout $layoutConfig;
  62.                 break;
  63.             }
  64.         }
  65.         // If nothing matches, we take the first layout that has no "host" or "pattern" configuration.
  66.         if (null === $finalLayout) {
  67.             $layouts $this->layouts;
  68.             do {
  69.                 $finalLayout array_shift($layouts);
  70.                 if ($finalLayout['host'] || $finalLayout['pattern']) {
  71.                     $finalLayout null;
  72.                 }
  73.             } while (null === $finalLayout && count($layouts));
  74.         }
  75.         if (null === $finalLayout || !$this->twig->getLoader()->exists($finalLayout['resource'])) {
  76.             $source = new Source(''$finalLayout['resource']);
  77.             throw new LoaderError(sprintf(
  78.                 'Unable to find template %s for layout %s. The "layout" parameter must be a valid twig view to be used as a layout.',
  79.                 $finalLayout['resource'], $finalLayout['name']
  80.             ), 0$source);
  81.         }
  82.         $event->getRequest()->attributes->set('_orbitale_cms_layout'$finalLayout);
  83.     }
  84. }