ConfigSourceAggregated.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Application configuration object. Used to access configuration when application is initialized and installed.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Config;
  9. class ConfigSourceAggregated implements ConfigSourceInterface
  10. {
  11. /**
  12. * @var ConfigSourceInterface[]
  13. */
  14. private $sources;
  15. /**
  16. * ConfigSourceAggregated constructor.
  17. *
  18. * @param array $sources
  19. */
  20. public function __construct(array $sources = [])
  21. {
  22. $this->sources = $sources;
  23. }
  24. /**
  25. * Retrieve aggregated configuration from all available sources.
  26. *
  27. * @param string $path
  28. * @return string|array
  29. */
  30. public function get($path = '')
  31. {
  32. $this->sortSources();
  33. $data = [];
  34. foreach ($this->sources as $sourceConfig) {
  35. /** @var ConfigSourceInterface $source */
  36. $source = $sourceConfig['source'];
  37. $configData = $source->get($path);
  38. if (!is_array($configData)) {
  39. $data = $configData;
  40. } elseif (!empty($configData)) {
  41. $data = array_replace_recursive(is_array($data) ? $data : [], $configData);
  42. }
  43. }
  44. return $data;
  45. }
  46. /**
  47. * Sort sources
  48. *
  49. * @return void
  50. */
  51. private function sortSources()
  52. {
  53. uasort($this->sources, function ($firstItem, $secondItem) {
  54. return $firstItem['sortOrder'] > $secondItem['sortOrder'];
  55. });
  56. }
  57. }