Move.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Layout\Reader;
  7. use Magento\Framework\View\Layout;
  8. class Move implements Layout\ReaderInterface
  9. {
  10. /**#@+
  11. * Supported types
  12. */
  13. const TYPE_MOVE = 'move';
  14. /**#@-*/
  15. /**
  16. * {@inheritdoc}
  17. *
  18. * @return string[]
  19. */
  20. public function getSupportedNodes()
  21. {
  22. return [self::TYPE_MOVE];
  23. }
  24. /**
  25. * {@inheritdoc}
  26. *
  27. * @param Context $readerContext
  28. * @param Layout\Element $currentElement
  29. * @return $this
  30. */
  31. public function interpret(Context $readerContext, Layout\Element $currentElement)
  32. {
  33. $this->scheduleMove($readerContext->getScheduledStructure(), $currentElement);
  34. return $this;
  35. }
  36. /**
  37. * Schedule structural changes for move directive
  38. *
  39. * @param \Magento\Framework\View\Layout\ScheduledStructure $scheduledStructure
  40. * @param \Magento\Framework\View\Layout\Element $currentElement
  41. * @throws \Magento\Framework\Exception\LocalizedException
  42. * @return $this
  43. */
  44. protected function scheduleMove(Layout\ScheduledStructure $scheduledStructure, Layout\Element $currentElement)
  45. {
  46. $elementName = (string)$currentElement->getAttribute('element');
  47. $destination = (string)$currentElement->getAttribute('destination');
  48. $alias = (string)$currentElement->getAttribute('as') ?: '';
  49. if ($elementName && $destination) {
  50. list($siblingName, $isAfter) = $this->beforeAfterToSibling($currentElement);
  51. $scheduledStructure->setElementToMove(
  52. $elementName,
  53. [$destination, $siblingName, $isAfter, $alias]
  54. );
  55. } else {
  56. throw new \Magento\Framework\Exception\LocalizedException(
  57. new \Magento\Framework\Phrase('Element name and destination must be specified.')
  58. );
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Analyze "before" and "after" information in the node and return sibling name and whether "after" or "before"
  64. *
  65. * @param \Magento\Framework\View\Layout\Element $node
  66. * @return array
  67. */
  68. protected function beforeAfterToSibling($node)
  69. {
  70. $result = [null, true];
  71. if (isset($node['after'])) {
  72. $result[0] = (string)$node['after'];
  73. } elseif (isset($node['before'])) {
  74. $result[0] = (string)$node['before'];
  75. $result[1] = false;
  76. }
  77. return $result;
  78. }
  79. }