Reader.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * Read config from different sources and aggregate them
  10. *
  11. * @package Magento\Framework\Config
  12. */
  13. class Reader implements \Magento\Framework\App\Config\Scope\ReaderInterface
  14. {
  15. /**
  16. * @var array
  17. */
  18. private $sources;
  19. /**
  20. * @param array $sources
  21. */
  22. public function __construct(array $sources)
  23. {
  24. $this->sources = $this->prepareSources($sources);
  25. }
  26. /**
  27. * Read configuration data
  28. *
  29. * @param null|string $scope
  30. * @throws LocalizedException Exception is thrown when scope other than default is given
  31. * @return array
  32. */
  33. public function read($scope = null)
  34. {
  35. $config = [];
  36. foreach ($this->sources as $sourceData) {
  37. /** @var \Magento\Framework\App\Config\Reader\Source\SourceInterface $source */
  38. $source = $sourceData['class'];
  39. $config = array_replace_recursive($config, $source->get($scope));
  40. }
  41. return $config;
  42. }
  43. /**
  44. * Prepare source for usage
  45. *
  46. * @param array $array
  47. * @return array
  48. */
  49. private function prepareSources(array $array)
  50. {
  51. $array = array_filter(
  52. $array,
  53. function ($item) {
  54. return (!isset($item['disable']) || !$item['disable']) && $item['class'];
  55. }
  56. );
  57. uasort(
  58. $array,
  59. function ($firstItem, $nexItem) {
  60. return (int)$firstItem['sortOrder'] <=> (int)$nexItem['sortOrder'];
  61. }
  62. );
  63. return $array;
  64. }
  65. }