Skip to content

Commit b857c02

Browse files
Merge forwardport of #11686 to 2.3-develop branch
Applied pull request patch https://github.com/magento/magento2/pull/11686.patch (created by @tdgroot) based on commit(s): 1. 177e540 2. 0d65213 3. a781bf6 4. dcc480d 5. 5d4e1c2 6. b552dd1 7. b85b91e Fixed GitHub Issues in 2.3-develop branch: - #5188: Error generating URN-catalog when blank one exists (reported by @JamesonNetworks)
2 parents 7aeccea + 402b719 commit b857c02

File tree

4 files changed

+130
-19
lines changed

4 files changed

+130
-19
lines changed

app/code/Magento/Developer/Model/XmlCatalog/Format/PhpStorm.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
namespace Magento\Developer\Model\XmlCatalog\Format;
88

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\DomDocument\DomDocumentFactory;
911
use Magento\Framework\Exception\FileSystemException;
1012
use Magento\Framework\Filesystem\Directory\ReadFactory;
1113
use Magento\Framework\Filesystem\Directory\ReadInterface;
12-
use Magento\Framework\Filesystem\Directory\WriteFactory;
13-
use Magento\Framework\Filesystem\Directory\WriteInterface;
14+
use Magento\Framework\Filesystem\File\WriteFactory;
1415

1516
/**
1617
* Class PhpStorm generates URN catalog for PhpStorm 9
@@ -23,20 +24,28 @@ class PhpStorm implements FormatInterface
2324
private $currentDirRead;
2425

2526
/**
26-
* @var \Magento\Framework\Filesystem\File\WriteFactory
27+
* @var WriteFactory
2728
*/
2829
private $fileWriteFactory;
2930

31+
/**
32+
* @var DomDocumentFactory
33+
*/
34+
private $domDocumentFactory;
35+
3036
/**
3137
* @param ReadFactory $readFactory
32-
* @param \Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory
38+
* @param WriteFactory $fileWriteFactory
39+
* @param DomDocumentFactory $domDocumentFactory
3340
*/
3441
public function __construct(
3542
ReadFactory $readFactory,
36-
\Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory
43+
WriteFactory $fileWriteFactory,
44+
DomDocumentFactory $domDocumentFactory = null
3745
) {
3846
$this->currentDirRead = $readFactory->create(getcwd());
3947
$this->fileWriteFactory = $fileWriteFactory;
48+
$this->domDocumentFactory = $domDocumentFactory ?: ObjectManager::getInstance()->get(DomDocumentFactory::class);
4049
}
4150

4251
/**
@@ -57,26 +66,21 @@ public function generateCatalog(array $dictionary, $configFilePath)
5766
\Magento\Framework\Filesystem\DriverPool::FILE,
5867
'r'
5968
);
60-
$dom = new \DOMDocument();
61-
$dom->loadXML($file->readAll());
69+
$dom = $this->domDocumentFactory->create();
70+
$fileContent = $file->readAll();
71+
if (!empty($fileContent)) {
72+
$dom->loadXML($fileContent);
73+
} else {
74+
$this->initEmptyFile($dom);
75+
}
6276
$xpath = new \DOMXPath($dom);
6377
$nodeList = $xpath->query('/project');
6478
$projectNode = $nodeList->item(0);
6579
$file->close();
6680
} catch (FileSystemException $f) {
6781
//create file if does not exists
68-
$dom = new \DOMDocument();
69-
$projectNode = $dom->createElement('project');
70-
71-
//PhpStorm 9 version for component is "4"
72-
$projectNode->setAttribute('version', '4');
73-
$dom->appendChild($projectNode);
74-
$rootComponentNode = $dom->createElement('component');
75-
76-
//PhpStorm 9 version for ProjectRootManager is "2"
77-
$rootComponentNode->setAttribute('version', '2');
78-
$rootComponentNode->setAttribute('name', 'ProjectRootManager');
79-
$projectNode->appendChild($rootComponentNode);
82+
$dom = $this->domDocumentFactory->create();
83+
$projectNode = $this->initEmptyFile($dom);
8084
}
8185

8286
$xpath = new \DOMXPath($dom);
@@ -103,4 +107,26 @@ public function generateCatalog(array $dictionary, $configFilePath)
103107
$file->write($dom->saveXML());
104108
$file->close();
105109
}
110+
111+
/**
112+
* Setup basic empty dom elements
113+
*
114+
* @param \DOMDocument $dom
115+
* @return \DOMElement
116+
*/
117+
private function initEmptyFile(\DOMDocument $dom)
118+
{
119+
$projectNode = $dom->createElement('project');
120+
121+
//PhpStorm 9 version for component is "4"
122+
$projectNode->setAttribute('version', '4');
123+
$dom->appendChild($projectNode);
124+
$rootComponentNode = $dom->createElement('component');
125+
126+
//PhpStorm 9 version for ProjectRootManager is "2"
127+
$rootComponentNode->setAttribute('version', '2');
128+
$rootComponentNode->setAttribute('name', 'ProjectRootManager');
129+
$projectNode->appendChild($rootComponentNode);
130+
return $projectNode;
131+
}
106132
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Developer\Model\XmlCatalog\Format\PhpStorm;
7+
8+
use DOMDocument;
9+
10+
class DomDocumentFactory
11+
{
12+
/**
13+
* @var \Magento\Framework\DomDocument\DomDocumentFactory
14+
*/
15+
private $documentFactory;
16+
17+
/**
18+
* DomDocumentFactory constructor.
19+
* @param \Magento\Framework\DomDocument\DomDocumentFactory $documentFactory
20+
*/
21+
public function __construct(\Magento\Framework\DomDocument\DomDocumentFactory $documentFactory)
22+
{
23+
$this->documentFactory = $documentFactory;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function create(string $data = null)
30+
{
31+
$dom = $this->documentFactory->create($data);
32+
33+
if (empty($data)) {
34+
$this->initializeDocument($dom);
35+
}
36+
37+
return $dom;
38+
}
39+
40+
/**
41+
* Initialize document to be used as 'misc.xml'
42+
*
43+
* @param DOMDocument $document
44+
* @return DOMDocument
45+
*/
46+
private function initializeDocument(DOMDocument $document)
47+
{
48+
$document->xmlVersion = '1.0';
49+
$projectNode = $document->createElement('project');
50+
51+
//PhpStorm 9 version for component is "4"
52+
$projectNode->setAttribute('version', '4');
53+
$document->appendChild($projectNode);
54+
$rootComponentNode = $document->createElement('component');
55+
56+
//PhpStorm 9 version for ProjectRootManager is "2"
57+
$rootComponentNode->setAttribute('version', '2');
58+
$rootComponentNode->setAttribute('name', 'ProjectRootManager');
59+
$projectNode->appendChild($rootComponentNode);
60+
61+
return $document;
62+
}
63+
}

lib/internal/Magento/Framework/DomDocument/DomDocumentFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\DomDocument;
78

89
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Test\Unit\DomDocument;
8+
9+
use Magento\Framework\DomDocument\DomDocumentFactory;
10+
11+
class DomDocumentFactoryTest extends \PHPUnit\Framework\TestCase
12+
{
13+
public function testCreateReturnsDomDocument()
14+
{
15+
$domDocumentFactory = new DomDocumentFactory();
16+
$this->assertInstanceOf(
17+
\DOMDocument::class,
18+
$domDocumentFactory->create()
19+
);
20+
}
21+
}

0 commit comments

Comments
 (0)