src/Eccube/EventListener/MobileTemplatePathListener.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\EventListener;
  13. use Detection\MobileDetect;
  14. use Eccube\Common\EccubeConfig;
  15. use Eccube\Request\Context;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Twig\Environment;
  19. class MobileTemplatePathListener implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var Context
  23.      */
  24.     protected $context;
  25.     /**
  26.      * @var Environment
  27.      */
  28.     protected $twig;
  29.     /**
  30.      * @var MobileDetect
  31.      */
  32.     protected $detector;
  33.     /**
  34.      * @var EccubeConfig
  35.      */
  36.     protected $eccubeConfig;
  37.     public function __construct(Context $contextEnvironment $twigMobileDetect $detectorEccubeConfig $eccubeConfig)
  38.     {
  39.         $this->context $context;
  40.         $this->twig $twig;
  41.         $this->detector $detector;
  42.         $this->eccubeConfig $eccubeConfig;
  43.     }
  44.     public function onKernelRequest(RequestEvent $event)
  45.     {
  46.         if (!$event->isMainRequest()) {
  47.             return;
  48.         }
  49.         // 管理画面の場合は実行しない.
  50.         if ($this->context->isAdmin()) {
  51.             return;
  52.         }
  53.         if (!$this->detector->isMobile()) {
  54.             return;
  55.         }
  56.         $paths = [
  57.             $this->eccubeConfig->get('eccube_theme_src_dir').'/smartphone',
  58.         ];
  59.         if (is_dir($this->eccubeConfig->get('eccube_theme_app_dir').'/smartphone')) {
  60.             $paths = [
  61.                 $this->eccubeConfig->get('eccube_theme_app_dir').'/smartphone',
  62.                 $this->eccubeConfig->get('eccube_theme_src_dir').'/smartphone',
  63.             ];
  64.         }
  65.         $loader = new \Twig\Loader\ChainLoader([
  66.             new \Twig\Loader\FilesystemLoader($paths),
  67.             $this->twig->getLoader(),
  68.         ]);
  69.         $this->twig->setLoader($loader);
  70.     }
  71.     public static function getSubscribedEvents()
  72.     {
  73.         return [
  74.             'kernel.request' => ['onKernelRequest'512],
  75.         ];
  76.     }
  77. }