Initial.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Initial configuration data container. Provides interface for reading initial config values
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Config;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. class Initial
  12. {
  13. /**
  14. * Cache identifier used to store initial config
  15. */
  16. const CACHE_ID = 'initial_config';
  17. /**
  18. * Config data
  19. *
  20. * @var array
  21. */
  22. protected $_data = [];
  23. /**
  24. * Config metadata
  25. *
  26. * @var array
  27. */
  28. protected $_metadata = [];
  29. /**
  30. * @var SerializerInterface
  31. */
  32. private $serializer;
  33. /**
  34. * Initial constructor
  35. *
  36. * @param Initial\Reader $reader
  37. * @param \Magento\Framework\App\Cache\Type\Config $cache
  38. * @param SerializerInterface|null $serializer
  39. */
  40. public function __construct(
  41. \Magento\Framework\App\Config\Initial\Reader $reader,
  42. \Magento\Framework\App\Cache\Type\Config $cache,
  43. SerializerInterface $serializer = null
  44. ) {
  45. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  46. ->get(SerializerInterface::class);
  47. $data = $cache->load(self::CACHE_ID);
  48. if (!$data) {
  49. $data = $reader->read();
  50. $cache->save($this->serializer->serialize($data), self::CACHE_ID);
  51. } else {
  52. $data = $this->serializer->unserialize($data);
  53. }
  54. $this->_data = $data['data'];
  55. $this->_metadata = $data['metadata'];
  56. }
  57. /**
  58. * Get initial data by given scope
  59. *
  60. * @param string $scope Format is scope type and scope code separated by pipe: e.g. "type|code"
  61. * @return array
  62. */
  63. public function getData($scope)
  64. {
  65. list($scopeType, $scopeCode) = array_pad(explode('|', $scope), 2, null);
  66. if (ScopeConfigInterface::SCOPE_TYPE_DEFAULT == $scopeType) {
  67. return $this->_data[$scopeType] ?? [];
  68. } elseif ($scopeCode) {
  69. return $this->_data[$scopeType][$scopeCode] ?? [];
  70. }
  71. return [];
  72. }
  73. /**
  74. * Get configuration metadata
  75. *
  76. * @return array
  77. */
  78. public function getMetadata()
  79. {
  80. return $this->_metadata;
  81. }
  82. }