Skip to content

Commit 0c4cb77

Browse files
committed
Add Visual Code catalog generator
1 parent 87b0a4b commit 0c4cb77

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Model\XmlCatalog\Format;
8+
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\DomDocument\DomDocumentFactory;
11+
use Magento\Framework\Exception\FileSystemException;
12+
use Magento\Framework\Filesystem\Directory\ReadFactory;
13+
use Magento\Framework\Filesystem\Directory\ReadInterface;
14+
use Magento\Framework\Filesystem\File\WriteFactory;
15+
16+
/**
17+
* Class VsCode generates URN catalog for VsCode
18+
*/
19+
class VsCode implements FormatInterface
20+
{
21+
private const PROJECT_PATH_IDENTIFIER = '..';
22+
23+
/**
24+
* @var ReadInterface
25+
*/
26+
private $currentDirRead;
27+
28+
/**
29+
* @var WriteFactory
30+
*/
31+
private $fileWriteFactory;
32+
33+
/**
34+
* @var DomDocumentFactory
35+
*/
36+
private $domDocumentFactory;
37+
38+
/**
39+
* @param ReadFactory $readFactory
40+
* @param WriteFactory $fileWriteFactory
41+
* @param DomDocumentFactory $domDocumentFactory
42+
*/
43+
public function __construct(
44+
ReadFactory $readFactory,
45+
WriteFactory $fileWriteFactory,
46+
DomDocumentFactory $domDocumentFactory = null
47+
) {
48+
$this->currentDirRead = $readFactory->create(getcwd());
49+
$this->fileWriteFactory = $fileWriteFactory;
50+
$this->domDocumentFactory = $domDocumentFactory ?: ObjectManager::getInstance()->get(DomDocumentFactory::class);
51+
}
52+
53+
/**
54+
* Generate Catalog of URNs for the VsCode
55+
*
56+
* @param string[] $dictionary
57+
* @param string $configFilePath relative path to the PhpStorm misc.xml
58+
* @return void
59+
*/
60+
public function generateCatalog(array $dictionary, $configFilePath)
61+
{
62+
$catalogNode = null;
63+
64+
try {
65+
$file = $this->fileWriteFactory->create(
66+
$configFilePath,
67+
\Magento\Framework\Filesystem\DriverPool::FILE,
68+
'r'
69+
);
70+
$dom = $this->domDocumentFactory->create();
71+
$fileContent = $file->readAll();
72+
if (!empty($fileContent)) {
73+
$dom->loadXML($fileContent);
74+
} else {
75+
$this->initEmptyFile($dom);
76+
}
77+
$xpath = new \DOMXPath($dom);
78+
$nodeList = $xpath->query('/catalog');
79+
$catalogNode = $nodeList->item(0);
80+
$file->close();
81+
} catch (FileSystemException $f) {
82+
//create file if does not exists
83+
$dom = $this->domDocumentFactory->create();
84+
$catalogNode = $this->initEmptyFile($dom);
85+
}
86+
87+
foreach ($dictionary as $urn => $xsdPath) {
88+
$node = $dom->createElement('system');
89+
$node->setAttribute('systemId', $urn);
90+
$node->setAttribute('uri', $this->getFileLocationInProject($xsdPath));
91+
$catalogNode->appendChild($node);
92+
}
93+
$dom->formatOutput = true;
94+
$file = $this->fileWriteFactory->create(
95+
$configFilePath,
96+
\Magento\Framework\Filesystem\DriverPool::FILE,
97+
'w'
98+
);
99+
$file->write($dom->saveXML());
100+
$file->close();
101+
}
102+
103+
/**
104+
* Setup basic empty dom elements
105+
*
106+
* @param \DOMDocument $dom
107+
* @return \DOMElement
108+
*/
109+
private function initEmptyFile(\DOMDocument $dom)
110+
{
111+
$catalogNode = $dom->createElement('catalog');
112+
113+
$catalogNode->setAttribute('xmlns', 'urn:oasis:names:tc:entity:xmlns:xml:catalog');
114+
$dom->appendChild($catalogNode);
115+
116+
return $catalogNode;
117+
}
118+
119+
/**
120+
* Resolve xsdpath to xml project path
121+
*
122+
* @param string $xsdPath
123+
* @return string
124+
*/
125+
private function getFileLocationInProject(string $xsdPath): string
126+
{
127+
return self::PROJECT_PATH_IDENTIFIER . DIRECTORY_SEPARATOR . $this->currentDirRead->getRelativePath($xsdPath);
128+
}
129+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<arguments>
1919
<argument name="formats" xsi:type="array">
2020
<item name="phpstorm" xsi:type="object">Magento\Developer\Model\XmlCatalog\Format\PhpStorm</item>
21+
<item name="vscode" xsi:type="object">Magento\Developer\Model\XmlCatalog\Format\VsCode</item>
2122
</argument>
2223
</arguments>
2324
</type>

0 commit comments

Comments
 (0)