Skip to content

Commit b906120

Browse files
committed
New Template customConfigXml
1 parent a19b4b7 commit b906120

File tree

7 files changed

+210
-0
lines changed

7 files changed

+210
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
1. Configure xml schema:
2+
3+
${Vendorname}/${Modulename}/etc/${vendorname}_${modulename}_${configfile_name}.xsd
4+
5+
2. Setup your config data as in example:
6+
7+
${Vendorname}/${Modulename}/etc/${vendorname}_${modulename}_${configfile_name}_sample.xml
8+
9+
3. Get configuration data from anywhere:
10+
11+
<?php
12+
class SampleClass
13+
{
14+
/**
15+
* @var \${Vendorname}\${Modulename}\Config\MappingConfigFactory
16+
*/
17+
private $mappingConfigFactory;
18+
19+
public function __construct(
20+
\${Vendorname}\${Modulename}\Config\MappingConfigFactory $mappingConfigFactory,
21+
) {
22+
$this->mappingConfigFactory = $mappingConfigFactory;
23+
}
24+
25+
public function someFunction()
26+
{
27+
$mappingConfig = $this->mappingConfigFactory->create([
28+
'fileName' => '${vendorname}_${modulename}_${configfile_name}_sample.xml'
29+
]);
30+
$configData = $mappingConfig->getConfigData();
31+
32+
//...
33+
}
34+
}
35+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generate custom xml reader that you can use to setup own configuration or mappings files
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* MappingConfig
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
9+
namespace ${Vendorname}\${Modulename}\Config;
10+
11+
use Magento\Framework\Config\CacheInterface;
12+
use Magento\Framework\Config\Data as ConfigData;
13+
use Magento\Framework\Config\Reader\FilesystemFactory as ConfigReaderFactory;
14+
15+
class MappingConfig extends ConfigData
16+
{
17+
/**
18+
* @var \Magento\Framework\Config\Data
19+
*/
20+
protected $mappingConfigData;
21+
22+
public function __construct(
23+
$fileName,
24+
Processor\ConverterConfigProcessor $converterConfigProcessor,
25+
Processor\SchemaLocatorConfigProcessor $schemaLocatorConfigProcessor,
26+
ConfigReaderFactory $configReaderFactory,
27+
CacheInterface $cache
28+
) {
29+
30+
$reader = $configReaderFactory->create([
31+
'converter' => $converterConfigProcessor,
32+
'schemaLocator' => $schemaLocatorConfigProcessor,
33+
'fileName' => $fileName
34+
]);
35+
$cacheId = $this->getConfigFileWithoutExtension($fileName);
36+
parent::__construct($reader, $cache, $cacheId);
37+
}
38+
39+
protected function getConfigFileWithoutExtension(string $configFile): string
40+
{
41+
return substr($configFile, 0, (strpos($configFile, '.xml')));
42+
}
43+
44+
public function getConfigData()
45+
{
46+
return $this->get('${main_element_name}_list');
47+
}
48+
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* ConverterConfigProcessor
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
namespace ${Vendorname}\${Modulename}\Config\Processor;
9+
10+
use Magento\Framework\Config\ConverterInterface;
11+
12+
class ConverterConfigProcessor implements ConverterInterface
13+
{
14+
const DEFAULT_SOURCE_TYPE = 'column';
15+
16+
public function convert($source)
17+
{
18+
$xmlEntries = [];
19+
foreach ($source->getElementsByTagName('${main_element_name}') as $attribute) {
20+
$data = [];
21+
foreach ($attribute->childNodes as $childNode) {
22+
if ($childNode->nodeType == XML_TEXT_NODE) {
23+
continue;
24+
}
25+
$data[$childNode->nodeName] = $childNode->nodeValue;
26+
}
27+
$xmlEntries[] = $data;
28+
}
29+
return ['${main_element_name}_list' => $xmlEntries];
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* SchemaLocatorConfigProcessor
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
9+
namespace ${Vendorname}\${Modulename}\Config\Processor;
10+
11+
use Magento\Framework\Config\SchemaLocatorInterface;
12+
use Magento\Framework\Module\Dir;
13+
use Magento\Framework\Module\Dir\Reader;
14+
15+
class SchemaLocatorConfigProcessor implements SchemaLocatorInterface
16+
{
17+
protected $_schema = null;
18+
19+
/**
20+
* @param Reader $moduleReader
21+
*/
22+
public function __construct(Reader $moduleReader)
23+
{
24+
$etcDir = $moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, '${Vendorname}_${Modulename}');
25+
$this->_schema = $etcDir . '/${vendorname}_${modulename}_${configfile_name}.xsd';
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function getSchema()
32+
{
33+
return $this->_schema;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getPerFileSchema()
40+
{
41+
return $this->_schema;
42+
}
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* ${vendorname}_${modulename}_${configfile_name}.xsd
5+
*
6+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
7+
* @author ${commentsUserEmail}
8+
*/
9+
-->
10+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
11+
<xs:element name="config">
12+
<xs:complexType>
13+
<xs:sequence>
14+
<xs:element minOccurs="1" maxOccurs="unbounded" name="${configfile_name}" type="${configfile_name}Type" />
15+
</xs:sequence>
16+
</xs:complexType>
17+
</xs:element>
18+
<xs:complexType name="${configfile_name}Type">
19+
<xs:sequence>
20+
<xs:element minOccurs="1" maxOccurs="unbounded" name="${main_element_name}" type="${main_element_name}Type" />
21+
</xs:sequence>
22+
</xs:complexType>
23+
<xs:complexType name="${main_element_name}Type">
24+
<xs:sequence>
25+
<xs:element name="subelement_1" type="xs:string" minOccurs="1" maxOccurs="1"/>
26+
<xs:element name="subelement_2" type="xs:string" minOccurs="1" maxOccurs="1"/>
27+
</xs:sequence>
28+
</xs:complexType>
29+
</xs:schema>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* ${vendorname}_${modulename}_${configfile_name}_sample.xml
5+
*
6+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
7+
* @author ${commentsUserEmail}
8+
*/
9+
-->
10+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:noNamespaceSchemaLocation="urn:magento:module:${Vendorname}_${Modulename}:etc/${vendorname}_${modulename}_${configfile_name}.xsd">
12+
<${configfile_name}>
13+
<${main_element_name}>
14+
<subelement_1>Sample Value A</subelement_1>
15+
<subelement_2>Sample Value B</subelement_2>
16+
</${main_element_name}>
17+
<${main_element_name}>
18+
<subelement_1>Sample Value C</subelement_1>
19+
<subelement_2>Sample Value D</subelement_2>
20+
</${main_element_name}>
21+
</${configfile_name}>
22+
</config>

0 commit comments

Comments
 (0)