6
6
7
7
namespace Magento \Developer \Model \XmlCatalog \Format ;
8
8
9
+ use Magento \Framework \App \ObjectManager ;
10
+ use Magento \Framework \DomDocument \DomDocumentFactory ;
9
11
use Magento \Framework \Exception \FileSystemException ;
10
12
use Magento \Framework \Filesystem \Directory \ReadFactory ;
11
13
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 ;
14
15
15
16
/**
16
17
* Class PhpStorm generates URN catalog for PhpStorm 9
@@ -23,20 +24,28 @@ class PhpStorm implements FormatInterface
23
24
private $ currentDirRead ;
24
25
25
26
/**
26
- * @var \Magento\Framework\Filesystem\File\ WriteFactory
27
+ * @var WriteFactory
27
28
*/
28
29
private $ fileWriteFactory ;
29
30
31
+ /**
32
+ * @var DomDocumentFactory
33
+ */
34
+ private $ domDocumentFactory ;
35
+
30
36
/**
31
37
* @param ReadFactory $readFactory
32
- * @param \Magento\Framework\Filesystem\File\WriteFactory $fileWriteFactory
38
+ * @param WriteFactory $fileWriteFactory
39
+ * @param DomDocumentFactory $domDocumentFactory
33
40
*/
34
41
public function __construct (
35
42
ReadFactory $ readFactory ,
36
- \Magento \Framework \Filesystem \File \WriteFactory $ fileWriteFactory
43
+ WriteFactory $ fileWriteFactory ,
44
+ DomDocumentFactory $ domDocumentFactory = null
37
45
) {
38
46
$ this ->currentDirRead = $ readFactory ->create (getcwd ());
39
47
$ this ->fileWriteFactory = $ fileWriteFactory ;
48
+ $ this ->domDocumentFactory = $ domDocumentFactory ?: ObjectManager::getInstance ()->get (DomDocumentFactory::class);
40
49
}
41
50
42
51
/**
@@ -57,26 +66,21 @@ public function generateCatalog(array $dictionary, $configFilePath)
57
66
\Magento \Framework \Filesystem \DriverPool::FILE ,
58
67
'r '
59
68
);
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
+ }
62
76
$ xpath = new \DOMXPath ($ dom );
63
77
$ nodeList = $ xpath ->query ('/project ' );
64
78
$ projectNode = $ nodeList ->item (0 );
65
79
$ file ->close ();
66
80
} catch (FileSystemException $ f ) {
67
81
//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 );
80
84
}
81
85
82
86
$ xpath = new \DOMXPath ($ dom );
@@ -103,4 +107,26 @@ public function generateCatalog(array $dictionary, $configFilePath)
103
107
$ file ->write ($ dom ->saveXML ());
104
108
$ file ->close ();
105
109
}
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
+ }
106
132
}
0 commit comments