Result.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\TemplateEngine\Xhtml;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\JsonHexTag;
  9. use Magento\Framework\View\Layout\Generator\Structure;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Framework\View\TemplateEngine\Xhtml\Template;
  12. use Magento\Framework\View\TemplateEngine\Xhtml\ResultInterface;
  13. use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Class Result
  17. */
  18. class Result implements ResultInterface
  19. {
  20. /**
  21. * @var Template
  22. */
  23. protected $template;
  24. /**
  25. * @var CompilerInterface
  26. */
  27. protected $compiler;
  28. /**
  29. * @var UiComponentInterface
  30. */
  31. protected $component;
  32. /**
  33. * @var Structure
  34. */
  35. protected $structure;
  36. /**
  37. * @var LoggerInterface
  38. */
  39. protected $logger;
  40. /**
  41. * @var JsonHexTag
  42. */
  43. private $jsonSerializer;
  44. /**
  45. * @param Template $template
  46. * @param CompilerInterface $compiler
  47. * @param UiComponentInterface $component
  48. * @param Structure $structure
  49. * @param LoggerInterface $logger
  50. * @param JsonHexTag $jsonSerializer
  51. */
  52. public function __construct(
  53. Template $template,
  54. CompilerInterface $compiler,
  55. UiComponentInterface $component,
  56. Structure $structure,
  57. LoggerInterface $logger,
  58. JsonHexTag $jsonSerializer = null
  59. ) {
  60. $this->template = $template;
  61. $this->compiler = $compiler;
  62. $this->component = $component;
  63. $this->structure = $structure;
  64. $this->logger = $logger;
  65. $this->jsonSerializer = $jsonSerializer ?? ObjectManager::getInstance()->get(JsonHexTag::class);
  66. }
  67. /**
  68. * Get result document root element \DOMElement
  69. *
  70. * @return \DOMElement
  71. */
  72. public function getDocumentElement()
  73. {
  74. return $this->template->getDocumentElement();
  75. }
  76. /**
  77. * Append layout configuration
  78. *
  79. * @return void
  80. */
  81. public function appendLayoutConfiguration()
  82. {
  83. $layoutConfiguration = $this->wrapContent(
  84. $this->jsonSerializer->serialize($this->structure->generate($this->component))
  85. );
  86. $this->template->append($layoutConfiguration);
  87. }
  88. /**
  89. * Returns the string representation
  90. *
  91. * @return string
  92. */
  93. public function __toString()
  94. {
  95. try {
  96. $templateRootElement = $this->getDocumentElement();
  97. foreach ($templateRootElement->attributes as $name => $attribute) {
  98. if ('noNamespaceSchemaLocation' === $name) {
  99. $this->getDocumentElement()->removeAttributeNode($attribute);
  100. break;
  101. }
  102. }
  103. $templateRootElement->removeAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi');
  104. $this->compiler->compile($templateRootElement, $this->component, $this->component);
  105. $this->appendLayoutConfiguration();
  106. $result = $this->compiler->postprocessing($this->template->__toString());
  107. } catch (\Exception $e) {
  108. $this->logger->critical($e->getMessage());
  109. $result = $e->getMessage();
  110. }
  111. return $result;
  112. }
  113. /**
  114. * Wrap content
  115. *
  116. * @param string $content
  117. * @return string
  118. */
  119. protected function wrapContent($content)
  120. {
  121. return '<script type="text/x-magento-init"><![CDATA['
  122. . '{"*": {"Magento_Ui/js/core/app": ' . str_replace(['<![CDATA[', ']]>'], '', $content) . '}}'
  123. . ']]></script>';
  124. }
  125. }