Options.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Locale\Deployed;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\App\State;
  11. use Magento\Framework\Config\ConfigOptionsListConstants as Constants;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Locale\AvailableLocalesInterface;
  14. use Magento\Framework\Locale\ListsInterface;
  15. use Magento\Framework\Locale\OptionInterface;
  16. use Magento\Framework\View\DesignInterface;
  17. /**
  18. * Returns options array of locales that have deployed static content.
  19. */
  20. class Options implements OptionInterface
  21. {
  22. /**
  23. * Application state class.
  24. *
  25. * @var State
  26. */
  27. private $state;
  28. /**
  29. * Operates with available locales.
  30. *
  31. * @var AvailableLocalesInterface
  32. */
  33. private $availableLocales;
  34. /**
  35. * Operates with magento design settings.
  36. *
  37. * @var DesignInterface
  38. */
  39. private $design;
  40. /**
  41. * Locales lists.
  42. *
  43. * @var ListsInterface
  44. */
  45. private $localeLists;
  46. /**
  47. * @var DeploymentConfig
  48. */
  49. private $deploymentConfig;
  50. /**
  51. * @param ListsInterface $localeLists locales list
  52. * @param State $state application state class
  53. * @param AvailableLocalesInterface $availableLocales operates with available locales
  54. * @param DesignInterface $design operates with magento design settings
  55. * @param DeploymentConfig $deploymentConfig
  56. */
  57. public function __construct(
  58. ListsInterface $localeLists,
  59. State $state,
  60. AvailableLocalesInterface $availableLocales,
  61. DesignInterface $design,
  62. DeploymentConfig $deploymentConfig = null
  63. ) {
  64. $this->localeLists = $localeLists;
  65. $this->state = $state;
  66. $this->availableLocales = $availableLocales;
  67. $this->design = $design;
  68. $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getOptionLocales(): array
  74. {
  75. return $this->filterLocales($this->localeLists->getOptionLocales());
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getTranslatedOptionLocales(): array
  81. {
  82. return $this->filterLocales($this->localeLists->getTranslatedOptionLocales());
  83. }
  84. /**
  85. * Filter list of locales by available locales for current theme and depends on running application mode.
  86. *
  87. * Applies filters only in production mode when flag 'static_content_on_demand_in_production' is not enabled.
  88. * For example, if the current design theme has only one generated locale en_GB then for given array of locales:
  89. * ```php
  90. * $locales = [
  91. * 0 => [
  92. * 'value' => 'da_DK'
  93. * 'label' => 'Danish (Denmark)'
  94. * ],
  95. * 1 => [
  96. * 'value' => 'de_DE'
  97. * 'label' => 'German (Germany)'
  98. * ],
  99. * 2 => [
  100. * 'value' => 'en_GB'
  101. * 'label' => 'English (United Kingdom)'
  102. * ],
  103. * ]
  104. * ```
  105. * result will be:
  106. * ```php
  107. * [
  108. * 2 => [
  109. * 'value' => 'en_GB'
  110. * 'label' => 'English (United Kingdom)'
  111. * ],
  112. * ]
  113. * ```
  114. *
  115. * @param array $locales list of locales for filtering
  116. * @return array of filtered locales
  117. */
  118. private function filterLocales(array $locales): array
  119. {
  120. if ($this->state->getMode() != State::MODE_PRODUCTION
  121. || $this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)) {
  122. return $locales;
  123. }
  124. $theme = $this->design->getDesignTheme();
  125. try {
  126. $availableLocales = $this->availableLocales->getList($theme->getCode(), $theme->getArea());
  127. } catch (LocalizedException $e) {
  128. $availableLocales = [];
  129. }
  130. return array_filter($locales, function ($localeData) use ($availableLocales) {
  131. return in_array($localeData['value'], $availableLocales);
  132. });
  133. }
  134. }