Block.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * PageCache controller
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\PageCache\Controller;
  9. use Magento\Framework\Serialize\Serializer\Base64Json;
  10. use Magento\Framework\Serialize\Serializer\Json;
  11. use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
  12. abstract class Block extends \Magento\Framework\App\Action\Action
  13. {
  14. /**
  15. * @var \Magento\Framework\Translate\InlineInterface
  16. */
  17. protected $translateInline;
  18. /**
  19. * @var Json
  20. */
  21. private $jsonSerializer;
  22. /**
  23. * @var Base64Json
  24. */
  25. private $base64jsonSerializer;
  26. /**
  27. * Layout cache keys to be able to generate different cache id for same handles
  28. *
  29. * @var LayoutCacheKeyInterface
  30. */
  31. private $layoutCacheKey;
  32. /**
  33. * @var string
  34. */
  35. private $layoutCacheKeyName = 'mage_pagecache';
  36. /**
  37. * @param \Magento\Framework\App\Action\Context $context
  38. * @param \Magento\Framework\Translate\InlineInterface $translateInline
  39. * @param Json $jsonSerializer
  40. * @param Base64Json $base64jsonSerializer
  41. * @param LayoutCacheKeyInterface $layoutCacheKey
  42. */
  43. public function __construct(
  44. \Magento\Framework\App\Action\Context $context,
  45. \Magento\Framework\Translate\InlineInterface $translateInline,
  46. Json $jsonSerializer = null,
  47. Base64Json $base64jsonSerializer = null,
  48. LayoutCacheKeyInterface $layoutCacheKey = null
  49. ) {
  50. parent::__construct($context);
  51. $this->translateInline = $translateInline;
  52. $this->jsonSerializer = $jsonSerializer
  53. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
  54. $this->base64jsonSerializer = $base64jsonSerializer
  55. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Base64Json::class);
  56. $this->layoutCacheKey = $layoutCacheKey
  57. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
  58. }
  59. /**
  60. * Get blocks from layout by handles
  61. *
  62. * @return array [\Element\BlockInterface]
  63. */
  64. protected function _getBlocks()
  65. {
  66. $blocks = $this->getRequest()->getParam('blocks', '');
  67. $handles = $this->getRequest()->getParam('handles', '');
  68. if (!$handles || !$blocks) {
  69. return [];
  70. }
  71. $blocks = $this->jsonSerializer->unserialize($blocks);
  72. $handles = $this->base64jsonSerializer->unserialize($handles);
  73. $layout = $this->_view->getLayout();
  74. $this->layoutCacheKey->addCacheKeys($this->layoutCacheKeyName);
  75. $this->_view->loadLayout($handles, true, true, false);
  76. $data = [];
  77. foreach ($blocks as $blockName) {
  78. $blockInstance = $layout->getBlock($blockName);
  79. if (is_object($blockInstance)) {
  80. $data[$blockName] = $blockInstance;
  81. }
  82. }
  83. return $data;
  84. }
  85. }