ScopeFallbackResolver.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\ScopeFallbackResolverInterface;
  9. class ScopeFallbackResolver implements ScopeFallbackResolverInterface
  10. {
  11. /**
  12. * @var StoreManagerInterface
  13. */
  14. protected $storeManager;
  15. /**
  16. * @var array
  17. */
  18. protected $fallback = [];
  19. /**
  20. * @param StoreManagerInterface $storeManager
  21. */
  22. public function __construct(
  23. StoreManagerInterface $storeManager
  24. ) {
  25. $this->storeManager = $storeManager;
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function getFallbackScope($scope, $scopeId, $forConfig = true)
  31. {
  32. if (!isset($this->fallback[$scope][$scopeId][$forConfig])) {
  33. $fallback = [null, null];
  34. switch ($scope) {
  35. case ScopeInterface::SCOPE_WEBSITE:
  36. case ScopeInterface::SCOPE_WEBSITES:
  37. $fallback = [ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null];
  38. break;
  39. case ScopeInterface::SCOPE_GROUP:
  40. $fallback = [
  41. ScopeInterface::SCOPE_WEBSITES,
  42. $this->storeManager->getGroup($scopeId)->getWebsiteId()
  43. ];
  44. break;
  45. case ScopeInterface::SCOPE_STORE:
  46. case ScopeInterface::SCOPE_STORES:
  47. $fallback = $forConfig
  48. ? [ScopeInterface::SCOPE_WEBSITES, $this->storeManager->getStore($scopeId)->getWebsiteId()]
  49. : [ScopeInterface::SCOPE_GROUP, $this->storeManager->getStore($scopeId)->getStoreGroupId()];
  50. }
  51. $this->fallback[$scope][$scopeId][$forConfig] = $fallback;
  52. }
  53. return $this->fallback[$scope][$scopeId][$forConfig];
  54. }
  55. }