如何在Magento 2中合并为一个模块的模块中创建自定义XML文件?(MageStackDay之谜2)


22

MageStackDay奖励500pts悬赏的奖金问题,并有可能赢得一年的免费Z-Ray许可证。更多信息可以在这里 >>

这些问题是由Magento 2核心开发人员Anton Kril提供/启发的。

题:

我正在创建具有一组单独配置的扩展。
这意味着我无法使用config.xmlroutes.xmlfieldset.xmlmagento拥有的任何其他配置xml文件。
例。

假设我正在定义一个具有行和列的“表”配置。我可以在下面使用此xml。(称之为table.xml

<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="path/to/table.xsd">
    <row id="row1">
        <column id="col1" sort="10" attr1="val1">
            <label>Col 1</label>
        </column>
    </row>
    <row id="row2">
        <column id="col1" sort="10" attr1="val1">
            <label>Col 1</label>
        </column>
        <column id="col2" sort="20" disabled="true" attr1="val2" >
            <label>Col 2</label>
        </column>
        <column id="col3" sort="15" attr1="val1">
            <label>Col 3</label>
        </column>
    </row>
</table>

但是,如果包含其他扩展名,则table.xml希望配置读取器将其选中,并且应合并2个或更多xml文件。我的意思是如果第二个文件看起来像这样

<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="path/to/table.xsd">
    <row id="row1">
        <column id="col2" sort="10" attr1="val2">
            <label>Col 2</label>
        </column>
    </row>
    <row id="row2">
        <column id="col1" sort="10" attr1="val5" />
    </row>
</table>

结果将是第二列添加到第一行,并且的值attr1被第二个xml覆盖:

<table ....>
    <row id="row1">
        <column id="col1" sort="10" attr1="val1"> <!-- from first xml -->
            <label>Col 1</label>
        </column>
        <column id="col2" sort="10" attr1="val2"><!-- from second xml-->
            <label>Col 2</label>
        </column>
    </row>
    <row id="row2">
        <column id="col1" sort="10" attr1="val5"><!--they apear in both xmls with the same path and id and second one overrides the value for `attr1`-->
            <label>Col 1</label>
        </column>
        <column id="col2" sort="20" disabled="true" attr1="val2"><!-- from first xml -->
            <label>Col 2</label>
        </column>
        <column id="col3" sort="15" attr1="val1"><!-- from first xml -->
            <label>Col 3</label>
        </column>
    </row>
</table>

在Magento 1中,我可以通过调用

 $merged = Mage::getConfig()->loadModulesConfiguration('table.xml')
            ->applyExtends();

Magento 2我该如何做?

Answers:


15

在Magento 2中,由\Magento\Framework\Config\Reader\Filesystem类处理。此类允许您指定要合并的xml文件。

以下部分将合并在可用模块中找到的所有文件,并合并输出(来自的片段\Magento\Framework\Config\Reader\Filesystem

/**
 * Load configuration scope
 *
 * @param string|null $scope
 * @return array
 */
public function read($scope = null)
{
    $scope = $scope ?: $this->_defaultScope;
    $fileList = $this->_fileResolver->get($this->_fileName, $scope);
    if (!count($fileList)) {
        return [];
    }
    $output = $this->_readFiles($fileList);

    return $output;
}

/**
 * Read configuration files
 *
 * @param array $fileList
 * @return array
 * @throws \Magento\Framework\Exception
 */
protected function _readFiles($fileList)
{
    /** @var \Magento\Framework\Config\Dom $configMerger */
    $configMerger = null;
    foreach ($fileList as $key => $content) {
        try {
            if (!$configMerger) {
                $configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
            } else {
                $configMerger->merge($content);
            }
        } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
            throw new \Magento\Framework\Exception("Invalid XML in file " . $key . ":\n" . $e->getMessage());
        }
    }
    if ($this->_isValidated) {
        $errors = [];
        if ($configMerger && !$configMerger->validate($this->_schemaFile, $errors)) {
            $message = "Invalid Document \n";
            throw new \Magento\Framework\Exception($message . implode("\n", $errors));
        }
    }

    $output = [];
    if ($configMerger) {
        $output = $this->_converter->convert($configMerger->getDom());
    }
    return $output;
}

在我创建的解决方案中,上述类被扩展为提供所需的xml文件,并指定可以在何处找到要验证的xsd文件(有关完整示例,请参见https://github.com/Genmato/MageStackTable):

namespace Genmato\TableXml\Model\Table;

class Reader extends \Magento\Framework\Config\Reader\Filesystem
{
    protected $_idAttributes = [
        '/table/row' => 'id',
        '/table/row/column' => 'id',
    ];

    /**
     * @param \Magento\Framework\Config\FileResolverInterface $fileResolver
     * @param \Magento\Framework\Config\ConverterInterface $converter
     * @param \Genmato\TableXml\Model\Table\SchemaLocator $schemaLocator
     * @param \Magento\Framework\Config\ValidationStateInterface $validationState
     * @param string $fileName
     * @param array $idAttributes
     * @param string $domDocumentClass
     * @param string $defaultScope
     */
    public function __construct(
        \Magento\Framework\Config\FileResolverInterface $fileResolver,
        \Magento\Framework\Config\ConverterInterface $converter,
        \Genmato\TableXml\Model\Table\SchemaLocator $schemaLocator,
        \Magento\Framework\Config\ValidationStateInterface $validationState,
        $fileName = 'table.xml',
        $idAttributes = [],
        $domDocumentClass = 'Magento\Framework\Config\Dom',
        $defaultScope = 'global'
    ) {
        parent::__construct(
            $fileResolver,
            $converter,
            $schemaLocator,
            $validationState,
            $fileName,
            $idAttributes,
            $domDocumentClass,
            $defaultScope
        );
    }

要获取合并的数据,您可以调用:

$output = $this->_objectManager->get('Genmato\TableXml\Model\Table\Reader')->read();

然后,输出是合并的xml的数组表示形式。

编辑:

为了测试文件的读取方式,我创建了一个工作示例(请参阅https://github.com/Genmato/MageStackTable)。使用解决方案版本更新了答案。


弗拉基米尔(Vladimir),今天早些时候,我看到了您先前的Dom上课示例示例答案。我开始研究利用Reader课堂的答案。同时,我刷新了问题页面,意识到您做到了:-) +1
Wojtek Naruniec

感谢您提供完整的详细答案以及来自github的POC模块。请留在那里以备将来参考。在这里...有一些赏金。
马里斯(Marius)

马吕斯,谢谢!将使该模块在GitHub上可用。
弗拉基米尔·科克霍夫(Fladimir Kerkhoff),2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.