SectionPool.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\CustomerData;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\ObjectManagerInterface;
  9. /**
  10. * Section pool
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class SectionPool implements SectionPoolInterface
  16. {
  17. /**
  18. * Object Manager
  19. *
  20. * @var ObjectManagerInterface
  21. */
  22. protected $objectManager;
  23. /**
  24. * Section map. Key is section name, value is section source object class
  25. *
  26. * @var array
  27. */
  28. protected $sectionSourceMap;
  29. /**
  30. * @var \Magento\Customer\CustomerData\Section\Identifier
  31. */
  32. protected $identifier;
  33. /**
  34. * Construct
  35. *
  36. * @param ObjectManagerInterface $objectManager
  37. * @param \Magento\Customer\CustomerData\Section\Identifier $identifier
  38. * @param array $sectionSourceMap
  39. */
  40. public function __construct(
  41. ObjectManagerInterface $objectManager,
  42. \Magento\Customer\CustomerData\Section\Identifier $identifier,
  43. array $sectionSourceMap = []
  44. ) {
  45. $this->objectManager = $objectManager;
  46. $this->identifier = $identifier;
  47. $this->sectionSourceMap = $sectionSourceMap;
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function getSectionsData(array $sectionNames = null, $forceNewTimestamp = false)
  53. {
  54. $sectionsData = $sectionNames ? $this->getSectionDataByNames($sectionNames) : $this->getAllSectionData();
  55. $sectionsData = $this->identifier->markSections($sectionsData, $sectionNames, $forceNewTimestamp);
  56. return $sectionsData;
  57. }
  58. /**
  59. * Get section sources by section names
  60. *
  61. * @param array $sectionNames
  62. * @return array
  63. * @throws \Magento\Framework\Exception\LocalizedException
  64. */
  65. protected function getSectionDataByNames($sectionNames)
  66. {
  67. $data = [];
  68. foreach ($sectionNames as $sectionName) {
  69. if (!isset($this->sectionSourceMap[$sectionName])) {
  70. throw new LocalizedException(__('The "%1" section source isn\'t supported.', $sectionName));
  71. }
  72. $data[$sectionName] = $this->get($this->sectionSourceMap[$sectionName])->getSectionData();
  73. }
  74. return $data;
  75. }
  76. /**
  77. * Get all section sources
  78. *
  79. * @return array
  80. */
  81. protected function getAllSectionData()
  82. {
  83. $data = [];
  84. foreach ($this->sectionSourceMap as $sectionName => $sectionClass) {
  85. $data[$sectionName] = $this->get($sectionClass)->getSectionData();
  86. }
  87. return $data;
  88. }
  89. /**
  90. * Get section source by name
  91. *
  92. * @param string $name
  93. * @return SectionSourceInterface
  94. * @throws LocalizedException
  95. */
  96. protected function get($name)
  97. {
  98. $sectionSource = $this->objectManager->get($name);
  99. if (!$sectionSource instanceof SectionSourceInterface) {
  100. throw new LocalizedException(
  101. __('%1 doesn\'t extend \Magento\Customer\CustomerData\SectionSourceInterface', $name)
  102. );
  103. }
  104. return $sectionSource;
  105. }
  106. }