Load.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Section;
  7. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  8. use Magento\Customer\CustomerData\Section\Identifier;
  9. use Magento\Customer\CustomerData\SectionPoolInterface;
  10. use Magento\Framework\App\Action\Context;
  11. use Magento\Framework\Controller\Result\JsonFactory;
  12. /**
  13. * Customer section controller
  14. */
  15. class Load extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
  16. {
  17. /**
  18. * @var JsonFactory
  19. */
  20. protected $resultJsonFactory;
  21. /**
  22. * @var Identifier
  23. * @deprecated 101.0.0
  24. */
  25. protected $sectionIdentifier;
  26. /**
  27. * @var SectionPoolInterface
  28. */
  29. protected $sectionPool;
  30. /**
  31. * @var \Magento\Framework\Escaper
  32. */
  33. private $escaper;
  34. /**
  35. * @param Context $context
  36. * @param JsonFactory $resultJsonFactory
  37. * @param Identifier $sectionIdentifier
  38. * @param SectionPoolInterface $sectionPool
  39. * @param Escaper $escaper
  40. */
  41. public function __construct(
  42. Context $context,
  43. JsonFactory $resultJsonFactory,
  44. Identifier $sectionIdentifier,
  45. SectionPoolInterface $sectionPool,
  46. \Magento\Framework\Escaper $escaper = null
  47. ) {
  48. parent::__construct($context);
  49. $this->resultJsonFactory = $resultJsonFactory;
  50. $this->sectionIdentifier = $sectionIdentifier;
  51. $this->sectionPool = $sectionPool;
  52. $this->escaper = $escaper ?: $this->_objectManager->get(\Magento\Framework\Escaper::class);
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function execute()
  58. {
  59. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  60. $resultJson = $this->resultJsonFactory->create();
  61. $resultJson->setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store', true);
  62. $resultJson->setHeader('Pragma', 'no-cache', true);
  63. try {
  64. $sectionNames = $this->getRequest()->getParam('sections');
  65. $sectionNames = $sectionNames ? array_unique(\explode(',', $sectionNames)) : null;
  66. $forceNewSectionTimestamp = $this->getRequest()->getParam('force_new_section_timestamp');
  67. if ('false' === $forceNewSectionTimestamp) {
  68. $forceNewSectionTimestamp = false;
  69. }
  70. $response = $this->sectionPool->getSectionsData($sectionNames, (bool)$forceNewSectionTimestamp);
  71. } catch (\Exception $e) {
  72. $resultJson->setStatusHeader(
  73. \Zend\Http\Response::STATUS_CODE_400,
  74. \Zend\Http\AbstractMessage::VERSION_11,
  75. 'Bad Request'
  76. );
  77. $response = ['message' => $this->escaper->escapeHtml($e->getMessage())];
  78. }
  79. return $resultJson->setData($response);
  80. }
  81. }