Skip to content

Commit 177e540

Browse files
committed
Fix error when generating urn catalog for empty misc.xml
1 parent 4d26093 commit 177e540

File tree

3 files changed

+89
-21
lines changed

3 files changed

+89
-21
lines changed

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

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

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

9+
use Magento\Developer\Model\XmlCatalog\Format\PhpStorm\DomDocumentFactory;
910
use Magento\Framework\Exception\FileSystemException;
1011
use Magento\Framework\Filesystem\Directory\ReadFactory;
1112
use Magento\Framework\Filesystem\Directory\ReadInterface;
12-
use Magento\Framework\Filesystem\Directory\WriteFactory;
13-
use Magento\Framework\Filesystem\Directory\WriteInterface;
13+
use Magento\Framework\Filesystem\File\WriteFactory as FileWriteFactory;
1414

1515
/**
1616
* Class PhpStorm generates URN catalog for PhpStorm 9
@@ -23,20 +23,28 @@ class PhpStorm implements FormatInterface
2323
private $currentDirRead;
2424

2525
/**
26-
* @var \Magento\Framework\Filesystem\File\WriteFactory
26+
* @var FileWriteFactory
2727
*/
2828
private $fileWriteFactory;
2929

30+
/**
31+
* @var DomDocumentFactory
32+
*/
33+
private $domDocumentFactory;
34+
3035
/**
3136
* @param ReadFactory $readFactory
32-
* @param \Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory
37+
* @param FileWriteFactory $fileWriteFactory
38+
* @param DomDocumentFactory $domDocumentFactory
3339
*/
3440
public function __construct(
3541
ReadFactory $readFactory,
36-
\Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory
42+
FileWriteFactory $fileWriteFactory,
43+
DomDocumentFactory $domDocumentFactory
3744
) {
3845
$this->currentDirRead = $readFactory->create(getcwd());
3946
$this->fileWriteFactory = $fileWriteFactory;
47+
$this->domDocumentFactory = $domDocumentFactory;
4048
}
4149

4250
/**
@@ -57,26 +65,14 @@ public function generateCatalog(array $dictionary, $configFilePath)
5765
\Magento\Framework\Filesystem\DriverPool::FILE,
5866
'r'
5967
);
60-
$dom = new \DOMDocument();
61-
$dom->loadXML($file->readAll());
68+
$dom = $this->domDocumentFactory->create($file->readAll());
6269
$xpath = new \DOMXPath($dom);
6370
$nodeList = $xpath->query('/project');
6471
$projectNode = $nodeList->item(0);
6572
$file->close();
6673
} catch (FileSystemException $f) {
6774
//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);
75+
$dom = $this->domDocumentFactory->create();
8076
}
8177

8278
$xpath = new \DOMXPath($dom);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 extends \Magento\Framework\DomDocument\DomDocumentFactory
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function create($data = null)
16+
{
17+
$dom = parent::create($data);
18+
19+
if (empty($data)) {
20+
$this->initializeDocument($dom);
21+
}
22+
23+
return $dom;
24+
}
25+
26+
/**
27+
* Initialize document to be used as 'misc.xml'
28+
*
29+
* @param DOMDocument $document
30+
* @return DOMDocument
31+
*/
32+
private function initializeDocument(DOMDocument $document)
33+
{
34+
$document->xmlVersion = '1.0';
35+
$projectNode = $document->createElement('project');
36+
37+
//PhpStorm 9 version for component is "4"
38+
$projectNode->setAttribute('version', '4');
39+
$document->appendChild($projectNode);
40+
$rootComponentNode = $document->createElement('component');
41+
42+
//PhpStorm 9 version for ProjectRootManager is "2"
43+
$rootComponentNode->setAttribute('version', '2');
44+
$rootComponentNode->setAttribute('name', 'ProjectRootManager');
45+
$projectNode->appendChild($rootComponentNode);
46+
47+
return $document;
48+
}
49+
}

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

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

89
/**
910
* DOM document factory
1011
*/
1112
class DomDocumentFactory
1213
{
14+
/**
15+
* @var \Magento\Framework\ObjectManagerInterface
16+
*/
17+
private $objectManager;
18+
19+
/**
20+
* DomDocumentFactory constructor.
21+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
22+
*/
23+
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
24+
{
25+
$this->objectManager = $objectManager;
26+
}
27+
1328
/**
1429
* Create empty DOM document instance.
1530
*
31+
* @param string $data the data to be loaded into the object
32+
*
1633
* @return \DOMDocument
1734
*/
18-
public function create()
35+
public function create($data = null)
1936
{
20-
return new \DOMDocument();
37+
$dom = $this->objectManager->create('DOMDocument');
38+
39+
if (!empty($data) && is_string($data)) {
40+
$dom->loadXML($data);
41+
}
42+
43+
return $dom;
2144
}
2245
}

0 commit comments

Comments
 (0)