DesignExceptions.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Class DesignExceptions
  11. */
  12. class DesignExceptions
  13. {
  14. /**
  15. * Core store config
  16. *
  17. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  18. */
  19. protected $scopeConfig;
  20. /**
  21. * Exception config path
  22. *
  23. * @var string
  24. */
  25. protected $exceptionConfigPath;
  26. /**
  27. * Scope Type
  28. *
  29. * @var string
  30. */
  31. protected $scopeType;
  32. /**
  33. * @var Json
  34. */
  35. private $serializer;
  36. /**
  37. * DesignExceptions constructor
  38. *
  39. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  40. * @param string $exceptionConfigPath
  41. * @param string $scopeType
  42. * @param Json|null $serializer
  43. */
  44. public function __construct(
  45. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  46. $exceptionConfigPath,
  47. $scopeType,
  48. Json $serializer = null
  49. ) {
  50. $this->scopeConfig = $scopeConfig;
  51. $this->exceptionConfigPath = $exceptionConfigPath;
  52. $this->scopeType = $scopeType;
  53. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  54. }
  55. /**
  56. * Get theme that should be applied for current user-agent according to design exceptions configuration
  57. *
  58. * @param \Magento\Framework\App\Request\Http $request
  59. * @return string|bool
  60. */
  61. public function getThemeByRequest(\Magento\Framework\App\Request\Http $request)
  62. {
  63. $userAgent = $request->getServer('HTTP_USER_AGENT');
  64. if (empty($userAgent)) {
  65. return false;
  66. }
  67. $expressions = $this->scopeConfig->getValue(
  68. $this->exceptionConfigPath,
  69. $this->scopeType
  70. );
  71. if (!$expressions) {
  72. return false;
  73. }
  74. $expressions = $this->serializer->unserialize($expressions);
  75. foreach ($expressions as $rule) {
  76. if (preg_match($rule['regexp'], $userAgent)) {
  77. return $rule['value'];
  78. }
  79. }
  80. return false;
  81. }
  82. }