|
| 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 | +} |
0 commit comments