Resolver.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Locale;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. class Resolver implements ResolverInterface
  9. {
  10. /**
  11. * Default locale
  12. */
  13. const DEFAULT_LOCALE = 'en_US';
  14. /**
  15. * Default locale code
  16. *
  17. * @var string
  18. */
  19. protected $defaultLocale;
  20. /**
  21. * Scope type
  22. *
  23. * @var string
  24. */
  25. protected $scopeType;
  26. /**
  27. * Locale code
  28. *
  29. * @var string
  30. */
  31. protected $locale;
  32. /**
  33. * @var ScopeConfigInterface
  34. */
  35. protected $scopeConfig;
  36. /**
  37. * Emulated locales stack
  38. *
  39. * @var array
  40. */
  41. protected $emulatedLocales = [];
  42. /**
  43. * @var string
  44. */
  45. private $defaultLocalePath;
  46. /**
  47. * @param ScopeConfigInterface $scopeConfig
  48. * @param string $defaultLocalePath
  49. * @param string $scopeType
  50. * @param mixed $locale
  51. */
  52. public function __construct(
  53. ScopeConfigInterface $scopeConfig,
  54. $defaultLocalePath,
  55. $scopeType,
  56. $locale = null
  57. ) {
  58. $this->scopeConfig = $scopeConfig;
  59. $this->defaultLocalePath = $defaultLocalePath;
  60. $this->scopeType = $scopeType;
  61. $this->setLocale($locale);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getDefaultLocalePath()
  67. {
  68. return $this->defaultLocalePath;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function setDefaultLocale($locale)
  74. {
  75. $this->defaultLocale = $locale;
  76. return $this;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getDefaultLocale()
  82. {
  83. if (!$this->defaultLocale) {
  84. $locale = $this->scopeConfig->getValue($this->getDefaultLocalePath(), $this->scopeType);
  85. if (!$locale) {
  86. $locale = self::DEFAULT_LOCALE;
  87. }
  88. $this->defaultLocale = $locale;
  89. }
  90. return $this->defaultLocale;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function setLocale($locale = null)
  96. {
  97. if ($locale !== null && is_string($locale)) {
  98. $this->locale = $locale;
  99. } else {
  100. $this->locale = $this->getDefaultLocale();
  101. }
  102. return $this;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getLocale()
  108. {
  109. if ($this->locale === null) {
  110. $this->setLocale();
  111. }
  112. return $this->locale;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function emulate($scopeId)
  118. {
  119. $result = null;
  120. if ($scopeId) {
  121. $this->emulatedLocales[] = $this->getLocale();
  122. $this->locale = $this->scopeConfig->getValue(
  123. $this->getDefaultLocalePath(),
  124. $this->scopeType,
  125. $scopeId
  126. );
  127. $result = $this->locale;
  128. } else {
  129. $this->emulatedLocales[] = false;
  130. }
  131. return $result;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function revert()
  137. {
  138. $result = null;
  139. $localeCode = array_pop($this->emulatedLocales);
  140. if ($localeCode) {
  141. $this->locale = $localeCode;
  142. $result = $this->locale;
  143. }
  144. return $result;
  145. }
  146. }