Body.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Page\Config\Reader;
  7. use Magento\Framework\View\Layout;
  8. use Magento\Framework\View\Page\Config as PageConfig;
  9. /**
  10. * Body structure reader
  11. */
  12. class Body implements Layout\ReaderInterface
  13. {
  14. /**#@+
  15. * Supported types
  16. */
  17. const TYPE_BODY = 'body';
  18. /**#@-*/
  19. /**#@+
  20. * Supported body sub elements
  21. */
  22. const BODY_ATTRIBUTE = 'attribute';
  23. /**#@-*/
  24. /**#@-*/
  25. protected $readerPool;
  26. /**
  27. * Constructor
  28. *
  29. * @param Layout\ReaderPool $readerPool
  30. */
  31. public function __construct(Layout\ReaderPool $readerPool)
  32. {
  33. $this->readerPool = $readerPool;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. *
  38. * @return string[]
  39. */
  40. public function getSupportedNodes()
  41. {
  42. return [self::TYPE_BODY];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. *
  47. * @param Layout\Reader\Context $readerContext
  48. * @param Layout\Element $bodyElement
  49. * @return $this
  50. */
  51. public function interpret(
  52. Layout\Reader\Context $readerContext,
  53. Layout\Element $bodyElement
  54. ) {
  55. /** @var \Magento\Framework\View\Layout\Element $element */
  56. foreach ($bodyElement as $element) {
  57. if ($element->getName() === self::BODY_ATTRIBUTE) {
  58. $this->setBodyAttributeToStructure($readerContext, $element);
  59. }
  60. }
  61. $this->readerPool->interpret($readerContext, $bodyElement);
  62. return $this;
  63. }
  64. /**
  65. * Schedule attributes to the page config structure
  66. *
  67. * @param Layout\Reader\Context $readerContext
  68. * @param Layout\Element $element
  69. * @return $this
  70. */
  71. protected function setBodyAttributeToStructure(Layout\Reader\Context $readerContext, Layout\Element $element)
  72. {
  73. if ($element->getAttribute('name') == PageConfig::BODY_ATTRIBUTE_CLASS) {
  74. $readerContext->getPageConfigStructure()->setBodyClass($element->getAttribute('value'));
  75. } else {
  76. $readerContext->getPageConfigStructure()->setElementAttribute(
  77. PageConfig::ELEMENT_TYPE_BODY,
  78. $element->getAttribute('name'),
  79. $element->getAttribute('value')
  80. );
  81. }
  82. return $this;
  83. }
  84. }