RuntimeConfigSource.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\App\Config\Source;
  7. use Magento\Framework\App\Config\ConfigSourceInterface;
  8. use Magento\Framework\App\Config\ScopeCodeResolver;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\DataObject;
  11. use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
  12. use Magento\Framework\App\Config\Scope\Converter;
  13. /**
  14. * Class for retrieving runtime configuration from database.
  15. *
  16. * @api
  17. * @since 100.1.2
  18. */
  19. class RuntimeConfigSource implements ConfigSourceInterface
  20. {
  21. /**
  22. * @var CollectionFactory
  23. */
  24. private $collectionFactory;
  25. /**
  26. * @var Converter
  27. */
  28. private $converter;
  29. /**
  30. * @var ScopeCodeResolver
  31. */
  32. private $scopeCodeResolver;
  33. /**
  34. * @param CollectionFactory $collectionFactory
  35. * @param ScopeCodeResolver $scopeCodeResolver
  36. * @param Converter $converter
  37. */
  38. public function __construct(
  39. CollectionFactory $collectionFactory,
  40. ScopeCodeResolver $scopeCodeResolver,
  41. Converter $converter
  42. ) {
  43. $this->collectionFactory = $collectionFactory;
  44. $this->converter = $converter;
  45. $this->scopeCodeResolver = $scopeCodeResolver;
  46. }
  47. /**
  48. * Get initial data.
  49. *
  50. * @param string $path Format is scope type and scope code separated by slash: e.g. "type/code"
  51. * @return array
  52. * @since 100.1.2
  53. */
  54. public function get($path = '')
  55. {
  56. $data = new DataObject($this->loadConfig());
  57. return $data->getData($path) ?: [];
  58. }
  59. /**
  60. * Load config from database.
  61. *
  62. * Load collection from db and presents it in array with path keys, like:
  63. * * scope/key/key *
  64. *
  65. * @return array
  66. */
  67. private function loadConfig()
  68. {
  69. try {
  70. $collection = $this->collectionFactory->create();
  71. } catch (\DomainException $e) {
  72. $collection = [];
  73. }
  74. $config = [];
  75. foreach ($collection as $item) {
  76. if ($item->getScope() === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  77. $config[$item->getScope()][$item->getPath()] = $item->getValue();
  78. } else {
  79. $code = $this->scopeCodeResolver->resolve($item->getScope(), $item->getScopeId());
  80. $config[$item->getScope()][$code][$item->getPath()] = $item->getValue();
  81. }
  82. }
  83. foreach ($config as $scope => &$item) {
  84. if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  85. $item = $this->converter->convert($item);
  86. } else {
  87. foreach ($item as &$scopeItems) {
  88. $scopeItems = $this->converter->convert($scopeItems);
  89. }
  90. }
  91. }
  92. return $config;
  93. }
  94. }