Skip to content

Commit 38052e9

Browse files
committed
MAGETWO-34177: Simplification of Payment Configuration
- implementation configuration with the capability of processing nodes
1 parent 2015f5c commit 38052e9

File tree

28 files changed

+566
-172
lines changed

28 files changed

+566
-172
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Model\Config\Compiler;
7+
8+
use Magento\Framework\Object;
9+
use Magento\Framework\Filesystem;
10+
use Magento\Framework\Module\Dir\Reader;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
14+
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Element\ElementInterface;
15+
16+
/**
17+
* Class IncludeElement
18+
*/
19+
class IncludeElement implements ElementInterface
20+
{
21+
const INCLUDE_PATH = 'path';
22+
23+
/**
24+
* @var Reader
25+
*/
26+
protected $moduleReader;
27+
28+
/**
29+
* @var Filesystem
30+
*/
31+
protected $filesystem;
32+
33+
/**
34+
* Constructor
35+
*
36+
* @param Reader $moduleReader
37+
* @param Filesystem $filesystem
38+
*/
39+
public function __construct(Reader $moduleReader, Filesystem $filesystem)
40+
{
41+
$this->filesystem = $filesystem;
42+
$this->moduleReader = $moduleReader;
43+
}
44+
45+
/**
46+
* Compiles the Element node
47+
*
48+
* @param CompilerInterface $compiler
49+
* @param \DOMElement $node
50+
* @param Object $processedObject
51+
* @param Object $context
52+
* @return void
53+
*/
54+
public function compile(CompilerInterface $compiler, \DOMElement $node, Object $processedObject, Object $context)
55+
{
56+
$ownerDocument = $node->ownerDocument;
57+
58+
$document = new \DOMDocument();
59+
$document->loadXML($this->getContent($node->getAttribute(static::INCLUDE_PATH)));
60+
61+
$newFragment = $ownerDocument->createDocumentFragment();
62+
foreach ($document->documentElement->childNodes as $child) {
63+
$newFragment->appendXML($document->saveXML($child));
64+
}
65+
66+
$node = $node->parentNode->replaceChild($newFragment, $node);
67+
foreach ($this->getChildNodes($node) as $child) {
68+
$compiler->compile($child, $processedObject, $context);
69+
}
70+
}
71+
72+
/**
73+
* Get child nodes
74+
*
75+
* @param \DOMElement $node
76+
* @return \DOMElement[]
77+
*/
78+
protected function getChildNodes(\DOMElement $node)
79+
{
80+
$childNodes = [];
81+
foreach ($node->childNodes as $child) {
82+
$childNodes[] = $child;
83+
}
84+
85+
return $childNodes;
86+
}
87+
88+
/**
89+
* Get content include file (in adminhtml area)
90+
*
91+
* @param string $includePath
92+
* @return string
93+
* @throws LocalizedException
94+
*/
95+
protected function getContent($includePath)
96+
{
97+
$modulesDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MODULES);
98+
99+
// <include path="Magento_Payment::my_payment.xml" />
100+
list($moduleName, $filename) = explode('::', $includePath);
101+
102+
$file = $this->moduleReader->getModuleDir('etc', $moduleName) . '/adminhtml/' . $filename;
103+
$path = $modulesDirectory->getRelativePath($file);
104+
105+
if ($modulesDirectory->isExist($path) && $modulesDirectory->isFile($path)) {
106+
return $modulesDirectory->readFile($path);
107+
}
108+
109+
throw new LocalizedException(__('The file "' . $file . '" does not exist'));
110+
}
111+
}

app/code/Magento/Config/Model/Config/Structure/Reader.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
*/
99
namespace Magento\Config\Model\Config\Structure;
1010

11+
use Magento\Framework\Object;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
14+
15+
/**
16+
* Class Reader
17+
*/
1118
class Reader extends \Magento\Framework\Config\Reader\Filesystem
1219
{
1320
/**
@@ -25,10 +32,18 @@ class Reader extends \Magento\Framework\Config\Reader\Filesystem
2532
];
2633

2734
/**
35+
* @var CompilerInterface
36+
*/
37+
protected $compiler;
38+
39+
/**
40+
* Constructor
41+
*
2842
* @param \Magento\Framework\Config\FileResolverInterface $fileResolver
2943
* @param Converter $converter
3044
* @param \Magento\Config\Model\Config\SchemaLocator $schemaLocator
3145
* @param \Magento\Framework\Config\ValidationStateInterface $validationState
46+
* @param CompilerInterface $compiler
3247
* @param string $fileName
3348
* @param array $idAttributes
3449
* @param string $domDocumentClass
@@ -39,11 +54,13 @@ public function __construct(
3954
Converter $converter,
4055
\Magento\Config\Model\Config\SchemaLocator $schemaLocator,
4156
\Magento\Framework\Config\ValidationStateInterface $validationState,
57+
CompilerInterface $compiler,
4258
$fileName = 'system.xml',
4359
$idAttributes = [],
4460
$domDocumentClass = 'Magento\Framework\Config\Dom',
4561
$defaultScope = 'global'
4662
) {
63+
$this->compiler = $compiler;
4764
parent::__construct(
4865
$fileResolver,
4966
$converter,
@@ -55,4 +72,66 @@ public function __construct(
5572
$defaultScope
5673
);
5774
}
75+
76+
/**
77+
* Read configuration files
78+
*
79+
* @param array $fileList
80+
* @return array
81+
* @throws LocalizedException
82+
*/
83+
protected function _readFiles($fileList)
84+
{
85+
86+
/** @var \Magento\Framework\Config\Dom $configMerger */
87+
$configMerger = null;
88+
foreach ($fileList as $key => $content) {
89+
try {
90+
$content = $this->processingDocument($content);
91+
if (!$configMerger) {
92+
$configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
93+
} else {
94+
$configMerger->merge($content);
95+
}
96+
} catch (\Magento\Framework\Config\Dom\ValidationException $e) {
97+
throw new LocalizedException(
98+
new \Magento\Framework\Phrase("Invalid XML in file %1:\n%2", [$key, $e->getMessage()])
99+
);
100+
}
101+
}
102+
103+
if ($this->_isValidated) {
104+
$errors = [];
105+
if ($configMerger && !$configMerger->validate($this->_schemaFile, $errors)) {
106+
$message = "Invalid Document \n";
107+
throw new LocalizedException(
108+
new \Magento\Framework\Phrase($message . implode("\n", $errors))
109+
);
110+
}
111+
}
112+
113+
$output = [];
114+
if ($configMerger) {
115+
$output = $this->_converter->convert($configMerger->getDom());
116+
}
117+
118+
return $output;
119+
}
120+
121+
/**
122+
* Processing nodes of the document before merging
123+
*
124+
* @param string $content
125+
* @return string
126+
*/
127+
protected function processingDocument($content)
128+
{
129+
$object = new Object();
130+
$document = new \DOMDocument();
131+
132+
$document->loadXML($content);
133+
$this->compiler->compile($document->documentElement, $object, $object);
134+
135+
return $document->saveXML();
136+
}
58137
}

app/code/Magento/Config/etc/di.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
<preference for="Magento\Config\Model\Config\Structure\SearchInterface" type="Magento\Config\Model\Config\Structure" />
1010
<preference for="Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface" type="Magento\Config\Model\Config\Backend\File\RequestData" />
1111
<preference for="Magento\Framework\App\Config\Resource\ConfigInterface" type="Magento\Config\Model\Resource\Config" />
12+
<virtualType name="Magento\Framework\View\TemplateEngine\Xhtml\ConfigCompiler" type="Magento\Framework\View\TemplateEngine\Xhtml\Compiler" shared="false">
13+
<arguments>
14+
<argument name="compilerText" xsi:type="object">Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Text</argument>
15+
<argument name="compilerAttribute" xsi:type="object">Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Attribute</argument>
16+
<argument name="compilerCdata" xsi:type="object">Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Cdata</argument>
17+
<argument name="compilerComment" xsi:type="object">Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Comment</argument>
18+
<argument name="elementCompilers" xsi:type="array">
19+
<item name="include" xsi:type="object">Magento\Config\Model\Config\Compiler\IncludeElement</item>
20+
</argument>
21+
</arguments>
22+
</virtualType>
23+
<type name="Magento\Config\Model\Config\Structure\Reader">
24+
<arguments>
25+
<argument name="compiler" xsi:type="object">Magento\Framework\View\TemplateEngine\Xhtml\ConfigCompiler</argument>
26+
</arguments>
27+
</type>
1228
<type name="Magento\Config\Controller\Adminhtml\System\Config\Save">
1329
<arguments>
1430
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Layout</argument>

app/code/Magento/Ui/TemplateEngine/Xhtml/Compiler/Element/Content.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
namespace Magento\Ui\TemplateEngine\Xhtml\Compiler\Element;
77

88
use Magento\Framework\Object;
9-
use Magento\Ui\TemplateEngine\Xhtml\Compiler;
109
use Magento\Framework\View\Element\UiComponentInterface;
10+
use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
11+
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Element\ElementInterface;
1112

1213
/**
1314
* Class Content
@@ -17,25 +18,22 @@ class Content implements ElementInterface
1718
/**
1819
* Compiles the Element node
1920
*
20-
* @param Compiler $compiler
21+
* @param CompilerInterface $compiler
2122
* @param \DOMElement $node
22-
* @param UiComponentInterface $component
23+
* @param Object $processedObject
2324
* @param Object $context
2425
* @return void
2526
*/
26-
public function compile(
27-
Compiler $compiler,
28-
\DOMElement $node,
29-
UiComponentInterface $component,
30-
Object $context
31-
) {
27+
public function compile(CompilerInterface $compiler, \DOMElement $node, Object $processedObject, Object $context)
28+
{
3229
$name = $node->getAttribute('name');
33-
$content = (string)$component->renderChildComponent($name);
30+
/** @var UiComponentInterface $processedObject */
31+
$content = (string)$processedObject->renderChildComponent($name);
3432
$name .= '_' . sprintf('%x', crc32(spl_object_hash($context)));
3533
if (!empty($content)) {
3634
$compiler->setPostprocessingData($name, $content);
3735
$newNode = $node->ownerDocument->createTextNode(
38-
Compiler::PATTERN_TAG . $name . Compiler::PATTERN_TAG
36+
CompilerInterface::PATTERN_TAG . $name . CompilerInterface::PATTERN_TAG
3937
);
4038
$node->parentNode->replaceChild($newNode, $node);
4139
} else {

app/code/Magento/Ui/TemplateEngine/Xhtml/Compiler/Element/ElementInterface.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

app/code/Magento/Ui/TemplateEngine/Xhtml/Compiler/Element/Form.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\Ui\TemplateEngine\Xhtml\Compiler\Element;
77

88
use Magento\Framework\Object;
9-
use Magento\Ui\TemplateEngine\Xhtml\Compiler;
10-
use Magento\Framework\View\Element\UiComponentInterface;
9+
use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
10+
use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Element\ElementInterface;
1111

1212
/**
1313
* Class Form
@@ -17,20 +17,16 @@ class Form implements ElementInterface
1717
/**
1818
* Compiles the Element node
1919
*
20-
* @param Compiler $compiler
20+
* @param CompilerInterface $compiler
2121
* @param \DOMElement $node
22-
* @param UiComponentInterface $component
22+
* @param Object $processedObject
2323
* @param Object $context
2424
* @return void
2525
*/
26-
public function compile(
27-
Compiler $compiler,
28-
\DOMElement $node,
29-
UiComponentInterface $component,
30-
Object $context
31-
) {
26+
public function compile(CompilerInterface $compiler, \DOMElement $node, Object $processedObject, Object $context)
27+
{
3228
foreach ($this->getChildNodes($node) as $child) {
33-
$compiler->compile($child, $component, $context);
29+
$compiler->compile($child, $processedObject, $context);
3430
}
3531
}
3632

0 commit comments

Comments
 (0)