StoreView.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /***
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\Config;
  7. use Magento\Directory\Helper\Data;
  8. use Magento\Framework\View\DesignInterface;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Retrieves theme and locale info associated with store-views
  12. */
  13. class StoreView
  14. {
  15. /**
  16. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  17. */
  18. private $scopeConfig;
  19. /**
  20. * @var \Magento\Store\Model\StoreManagerInterface
  21. */
  22. private $storeManager;
  23. /**
  24. * @var \Magento\Framework\View\Design\Theme\ThemeProviderInterface
  25. */
  26. private $themeProvider;
  27. /**
  28. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  29. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  30. * @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
  31. */
  32. public function __construct(
  33. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  34. \Magento\Store\Model\StoreManagerInterface $storeManager,
  35. \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
  36. ) {
  37. $this->scopeConfig = $scopeConfig;
  38. $this->storeManager = $storeManager;
  39. $this->themeProvider = $themeProvider;
  40. }
  41. /**
  42. * Retrieves a unique list of pairs representing the theme/locale for each store view
  43. *
  44. * @return array
  45. */
  46. public function retrieveThemeLocalePairs()
  47. {
  48. $stores = $this->storeManager->getStores();
  49. $localeThemeData = [];
  50. /** @var \Magento\Store\Api\Data\StoreInterface $store */
  51. foreach ($stores as $store) {
  52. $code = $store->getCode();
  53. $themeId = $this->scopeConfig->getValue(
  54. DesignInterface::XML_PATH_THEME_ID,
  55. ScopeInterface::SCOPE_STORE,
  56. $code
  57. );
  58. $localeThemeData[] = [
  59. 'theme' => $this->themeProvider->getThemeById($themeId)->getCode(),
  60. 'locale' => $this->scopeConfig->getValue(
  61. Data::XML_PATH_DEFAULT_LOCALE,
  62. ScopeInterface::SCOPE_STORE,
  63. $code
  64. )
  65. ];
  66. }
  67. return $this->removeDuplicates($localeThemeData);
  68. }
  69. /**
  70. * Retrieves a unique list of locales that are used by store views
  71. *
  72. * @return array
  73. */
  74. public function retrieveLocales()
  75. {
  76. $stores = $this->storeManager->getStores();
  77. $locales = [];
  78. /** @var \Magento\Store\Api\Data\StoreInterface $store */
  79. foreach ($stores as $store) {
  80. $locales[] = $this->scopeConfig->getValue(
  81. Data::XML_PATH_DEFAULT_LOCALE,
  82. ScopeInterface::SCOPE_STORE,
  83. $store->getCode()
  84. );
  85. }
  86. return $this->removeDuplicates($locales);
  87. }
  88. /**
  89. * Remove duplicate entries in an array
  90. *
  91. * @param array $arr
  92. * @return array
  93. */
  94. private function removeDuplicates($arr)
  95. {
  96. $len = count($arr);
  97. for ($out = 0; $out < $len; $out++) {
  98. $outVal = $arr[$out];
  99. for ($in = $out + 1; $in < $len; $in++) {
  100. $inVal = $arr[$in];
  101. if ($outVal === $inVal) {
  102. unset($arr[$out]);
  103. }
  104. }
  105. }
  106. return $arr;
  107. }
  108. }