vendor/netbull/mobile-detect-bundle/src/DataCollector/DeviceDataCollector.php line 161

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the MobileDetectBundle.
  4.  *
  5.  * (c) Nikolay Ivlev <nikolay.kotovsky@gmail.com>
  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 SunCat\MobileDetectBundle\DataCollector;
  11. use SunCat\MobileDetectBundle\EventListener\RequestResponseListener;
  12. use SunCat\MobileDetectBundle\Helper\DeviceView;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  16. use Throwable;
  17. /**
  18.  * DeviceDataCollector class
  19.  *
  20.  * @author Jonas HAOUZI <haouzijonas@gmail.com>
  21.  *
  22.  */
  23. class DeviceDataCollector extends DataCollector
  24. {
  25.     /**
  26.      * @var DeviceView
  27.      */
  28.     protected $deviceView;
  29.     /**
  30.      * @var array
  31.      */
  32.     protected $redirectConfig;
  33.     /**
  34.      * DeviceDataCollector constructor.
  35.      *
  36.      * @param DeviceView $deviceView Device View Detector
  37.      */
  38.     public function __construct(DeviceView $deviceView)
  39.     {
  40.         $this->deviceView $deviceView;
  41.     }
  42.     /**
  43.      * Collects data for the given Request and Response.
  44.      *
  45.      * @param Request $request A Request instance
  46.      * @param Response $response A Response instance
  47.      * @param Throwable|null $exception An Exception instance
  48.      *
  49.      * @api
  50.      */
  51.     public function collect(
  52.         Request $request,
  53.         Response $response,
  54.         Throwable $exception null
  55.     ) {
  56.         $this->data['currentView'] = $this->deviceView->getViewType();
  57.         $this->data['views'] = array(
  58.             array(
  59.                 'type' => DeviceView::VIEW_FULL,
  60.                 'label' => 'Full',
  61.                 'link' => $this->generateSwitchLink(
  62.                     $request,
  63.                     DeviceView::VIEW_FULL
  64.                 ),
  65.                 'isCurrent' => $this->deviceView->isFullView(),
  66.                 'enabled' => $this->canUseView(DeviceView::VIEW_FULL$request->getSchemeAndHttpHost()),
  67.             ),
  68.             array(
  69.                 'type' => DeviceView::VIEW_TABLET,
  70.                 'label' => 'Tablet',
  71.                 'link' => $this->generateSwitchLink(
  72.                     $request,
  73.                     DeviceView::VIEW_TABLET
  74.                 ),
  75.                 'isCurrent' => $this->deviceView->isTabletView(),
  76.                 'enabled' => $this->canUseView(DeviceView::VIEW_TABLET$request->getSchemeAndHttpHost()),
  77.             ),
  78.             array(
  79.                 'type' => DeviceView::VIEW_MOBILE,
  80.                 'label' => 'Mobile',
  81.                 'link' => $this->generateSwitchLink(
  82.                     $request,
  83.                     DeviceView::VIEW_MOBILE
  84.                 ),
  85.                 'isCurrent' => $this->deviceView->isMobileView(),
  86.                 'enabled' => $this->canUseView(DeviceView::VIEW_MOBILE$request->getSchemeAndHttpHost()),
  87.             ),
  88.         );
  89.     }
  90.     public function getCurrentView(): string
  91.     {
  92.         return $this->data['currentView'];
  93.     }
  94.     public function getViews(): array
  95.     {
  96.         return $this->data['views'];
  97.     }
  98.     public function setRedirectConfig(array $redirectConfig)
  99.     {
  100.         $this->redirectConfig $redirectConfig;
  101.     }
  102.     /**
  103.      * Returns the name of the collector.
  104.      *
  105.      * @api
  106.      */
  107.     public function getName(): string
  108.     {
  109.         return 'device.collector';
  110.     }
  111.     protected function canUseView(string $view, ?string $host): bool
  112.     {
  113.         if (!is_array($this->redirectConfig)) {
  114.             return true;
  115.         }
  116.         if (!isset($this->redirectConfig[$view])) {
  117.             return true;
  118.         }
  119.         if (!isset($this->redirectConfig[$view]['is_enabled']) ||
  120.             false === $this->redirectConfig[$view]['is_enabled']
  121.         ) {
  122.             return true;
  123.         }
  124.         if (true === $this->redirectConfig[$view]['is_enabled'] &&
  125.             isset($this->redirectConfig[$view]['host']) &&
  126.             isset($this->redirectConfig[$view]['action']) &&
  127.             !empty($this->redirectConfig[$view]['host']) &&
  128.             in_array($this->redirectConfig[$view]['action'], [RequestResponseListener::REDIRECTRequestResponseListener::REDIRECT_WITHOUT_PATH])
  129.         ) {
  130.             $parseHost parse_url($this->redirectConfig[$view]['host']);
  131.             $redirectHost $parseHost['scheme'].'://'.$parseHost['host'];
  132.             if (!empty($parseHost['port'])) {
  133.                 $redirectHost .= ':'.$parseHost['port'];
  134.             }
  135.             if ($redirectHost !== $host) {
  136.                 return false;
  137.             }
  138.         }
  139.         return true;
  140.     }
  141.     private function generateSwitchLink(Request $requeststring $view): ?string
  142.     {
  143.         $requestSwitchView $request->duplicate();
  144.         $requestSwitchView->query->set($this->deviceView->getSwitchParam(), $view);
  145.         $requestSwitchView->server->set(
  146.             'QUERY_STRING',
  147.             Request::normalizeQueryString(
  148.                 http_build_query($requestSwitchView->query->all(), null'&')
  149.             )
  150.         );
  151.         return $requestSwitchView->getUri();
  152.     }
  153.     public function reset()
  154.     {
  155.         $this->data = [];
  156.     }
  157. }