Template.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\TemplateEngine\Xhtml;
  7. /**
  8. * Class Template
  9. */
  10. class Template
  11. {
  12. const XML_VERSION = '1.0';
  13. const XML_ENCODING = 'UTF-8';
  14. /**
  15. * @var \Psr\Log\LoggerInterface
  16. */
  17. protected $logger;
  18. /**
  19. * @var \DOMElement
  20. */
  21. protected $templateNode;
  22. /**
  23. * @param \Psr\Log\LoggerInterface $logger
  24. * @param string $content
  25. */
  26. public function __construct(
  27. \Psr\Log\LoggerInterface $logger,
  28. $content
  29. ) {
  30. $this->logger = $logger;
  31. $document = new \DOMDocument(static::XML_VERSION, static::XML_ENCODING);
  32. $document->loadXML($content);
  33. $this->templateNode = $document->documentElement;
  34. }
  35. /**
  36. * Get template root element
  37. *
  38. * @return \DOMElement
  39. */
  40. public function getDocumentElement()
  41. {
  42. return $this->templateNode;
  43. }
  44. /**
  45. * Append
  46. *
  47. * @param string $content
  48. * @return void
  49. */
  50. public function append($content)
  51. {
  52. $newFragment = $this->templateNode->ownerDocument->createDocumentFragment();
  53. $newFragment->appendXML($content);
  54. $this->templateNode->appendChild($newFragment);
  55. }
  56. /**
  57. * Returns the string representation
  58. *
  59. * @return string
  60. */
  61. public function __toString()
  62. {
  63. try {
  64. $this->templateNode->ownerDocument->normalizeDocument();
  65. $result = $this->templateNode->ownerDocument->saveHTML();
  66. } catch (\Exception $e) {
  67. $this->logger->critical($e->getMessage());
  68. $result = '';
  69. }
  70. return $result;
  71. }
  72. }