vendor/tattali/mobile-detect-bundle/src/DataCollector/DeviceDataCollector.php line 45

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