|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Variable\Model\Config\Structure; |
| 10 | + |
| 11 | +use Magento\Framework\App\ObjectManager; |
| 12 | +use Magento\Framework\Config\ValidationStateInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Filter dom structure to required components only |
| 16 | + */ |
| 17 | +class Dom extends \Magento\Framework\Config\Dom |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var AvailableVariables |
| 21 | + */ |
| 22 | + private $structureConfig; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + private $filters; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param string $xml |
| 31 | + * @param ValidationStateInterface $validationState |
| 32 | + * @param array $idAttributes |
| 33 | + * @param string $typeAttributeName |
| 34 | + * @param string $schemaFile |
| 35 | + * @param string $errorFormat |
| 36 | + * @param AvailableVariables|null $availableVariables |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + $xml, |
| 40 | + ValidationStateInterface $validationState, |
| 41 | + array $idAttributes = [], |
| 42 | + $typeAttributeName = null, |
| 43 | + $schemaFile = null, |
| 44 | + $errorFormat = self::ERROR_FORMAT_DEFAULT, |
| 45 | + AvailableVariables $availableVariables = null |
| 46 | + ) { |
| 47 | + $this->structureConfig = $availableVariables |
| 48 | + ?: ObjectManager::getInstance()->get(AvailableVariables::class); |
| 49 | + parent::__construct($xml, $validationState, $idAttributes, $typeAttributeName, $schemaFile, $errorFormat); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritdoc |
| 54 | + */ |
| 55 | + protected function _initDom($xml) |
| 56 | + { |
| 57 | + $dom = parent::_initDom($xml); |
| 58 | + foreach (['tab', 'section', 'group', 'field'] as $element) { |
| 59 | + $this->filterElements($dom, $element, $this->getElementFilters($element)); |
| 60 | + } |
| 61 | + return $dom; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @inheritdoc |
| 66 | + */ |
| 67 | + public function merge($xml) |
| 68 | + { |
| 69 | + $dom = $this->_initDom($xml); |
| 70 | + if ($dom->documentElement->getElementsByTagName('section')->length >0) { |
| 71 | + $this->_mergeNode($dom->documentElement, ''); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Filter DOMDocument elements and keep only allowed |
| 77 | + * |
| 78 | + * @param \DOMDocument $dom |
| 79 | + * @param string $tag |
| 80 | + * @param array $ids |
| 81 | + */ |
| 82 | + private function filterElements($dom, $tag, $ids) |
| 83 | + { |
| 84 | + $removeElements = []; |
| 85 | + foreach ($dom->documentElement->getElementsByTagName($tag) as $removeElement) { |
| 86 | + if (!in_array($removeElement->getAttribute('id'), $ids)) { |
| 87 | + $removeElements[] = $removeElement; |
| 88 | + } |
| 89 | + } |
| 90 | + foreach ($removeElements as $removeElement) { |
| 91 | + $removeElement->parentNode->removeChild($removeElement); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get allowed identifiers by element tag |
| 97 | + * |
| 98 | + * @param string $tag |
| 99 | + * @return array|mixed |
| 100 | + */ |
| 101 | + private function getElementFilters($tag) |
| 102 | + { |
| 103 | + if (!isset($this->filters[$tag])) { |
| 104 | + $configPaths = $this->structureConfig->getFlatConfigPaths(); |
| 105 | + $filterData = []; |
| 106 | + foreach (array_keys($configPaths) as $path) { |
| 107 | + list($section, $group, $field) = explode('/', $path); |
| 108 | + $filterData['section'][] = $section; |
| 109 | + $filterData['group'][] = $group; |
| 110 | + $filterData['field'][] = $field; |
| 111 | + } |
| 112 | + $filterData['tab'] = []; |
| 113 | + $this->filters = array_map('array_unique', $filterData); |
| 114 | + } |
| 115 | + |
| 116 | + return $this->filters[$tag] ?? []; |
| 117 | + } |
| 118 | +} |
0 commit comments