StoresConfig.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Exception\NoSuchEntityException;
  8. class StoresConfig
  9. {
  10. /**
  11. * @var \Magento\Store\Model\StoreManagerInterface
  12. */
  13. protected $_storeManager;
  14. /**
  15. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  16. */
  17. protected $_config;
  18. /**
  19. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  20. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  21. */
  22. public function __construct(
  23. \Magento\Store\Model\StoreManagerInterface $storeManager,
  24. \Magento\Framework\App\Config\ScopeConfigInterface $config
  25. ) {
  26. $this->_storeManager = $storeManager;
  27. $this->_config = $config;
  28. }
  29. /**
  30. * Retrieve store Ids for $path with checking
  31. *
  32. * return array($storeId => $pathValue)
  33. *
  34. * @param string $path
  35. * @return array
  36. */
  37. public function getStoresConfigByPath($path)
  38. {
  39. $stores = $this->_storeManager->getStores(true);
  40. $storeValues = [];
  41. /** @var $store \Magento\Store\Model\Store */
  42. foreach ($stores as $store) {
  43. try {
  44. $value = $this->_config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode());
  45. $storeValues[$store->getId()] = $value;
  46. } catch (NoSuchEntityException $e) {
  47. // Store doesn't really exist, so move on.
  48. continue;
  49. }
  50. }
  51. return $storeValues;
  52. }
  53. }