@@ -97,13 +97,20 @@ private function processXmlFiles($xmlFiles, $type, $urn)
97
97
{
98
98
foreach ($ xmlFiles as $ file ) {
99
99
$ contents = $ file ->getContents ();
100
- $ entityContents = $ this ->getEntityContents ($ contents , $ type );
101
- if (count ($ entityContents ) > 1 ) {
100
+ $ domDocument = new \DOMDocument ();
101
+ $ domDocument ->loadXML ($ contents );
102
+ $ entities = $ domDocument ->getElementsByTagName (lcfirst ($ type ));
103
+
104
+ if ($ entities ->length > 1 ) {
102
105
$ filename = $ file ->getRealPath ();
103
106
if ($ this ->output ->isVerbose ()) {
104
107
$ this ->output ->writeln ('Processing file: ' . $ filename );
105
108
}
106
- foreach ($ entityContents as $ entityName => $ entityContent ) {
109
+ foreach ($ entities as $ entity ) {
110
+ /** @var \DOMElement $entity*/
111
+ $ entityName = $ entity ->getAttribute ('name ' );
112
+ $ entityContent = $ entity ->ownerDocument ->saveXML ($ entity );
113
+
107
114
$ dir = dirname ($ file );
108
115
$ dir .= DIRECTORY_SEPARATOR . ucfirst (basename ($ file , '.xml ' ));
109
116
$ splitFileName = $ this ->formatName ($ entityName , $ type );
@@ -128,50 +135,6 @@ private function processXmlFiles($xmlFiles, $type, $urn)
128
135
}
129
136
}
130
137
131
- /**
132
- * Parse contents and return array of single entity content according to entity type
133
- *
134
- * @param string $contents
135
- * @param string $type
136
- * @return array
137
- */
138
- private function getEntityContents ($ contents , $ type )
139
- {
140
- $ nonEmptyTag = '/[\S\s]+\n(?<entity>[\s]*< ' . lcfirst ($ type )
141
- . '[^\<\>]+name[\s]*=[\s]*"(?<name>[^\" \'\<\>\&]+)"[^\<\>]*>[\S\s]+<\/ '
142
- . lcfirst ($ type ) . '[\s]*>)+[\S\s]+/ ' ;
143
- $ entityContents = $ this ->scanForPattern ($ contents , $ type , $ nonEmptyTag );
144
-
145
- $ emptyTag = '/[\S\s]+\n(?<entity>[\s]*< ' . lcfirst ($ type )
146
- . '[^\<\>]+name[\s]*=[\s]*"(?<name>[^\" \'\<\>\&]+)"[^\<\>]*\/>)/ ' ;
147
- $ entityContents = array_merge ($ entityContents , $ this ->scanForPattern ($ contents , $ type , $ emptyTag ));
148
-
149
- return $ entityContents ;
150
- }
151
-
152
- /**
153
- * Scan contents to match given pattern
154
- *
155
- * @param string $contents
156
- * @param string $type
157
- * @param string $pattern
158
- * @return array
159
- */
160
- private function scanForPattern ($ contents , $ type , $ pattern )
161
- {
162
- preg_match ($ pattern , $ contents , $ matches );
163
- if (isset ($ matches ['entity ' ]) && isset ($ matches ['name ' ])) {
164
- $ contents = str_replace ($ matches ['entity ' ], '' , $ contents );
165
- $ entityContents [trim ($ matches ['name ' ])] = $ matches ['entity ' ];
166
- $ restEntityContents = $ this ->scanForPattern ($ contents , $ type , $ pattern );
167
- if (!empty ($ restEntityContents )) {
168
- $ entityContents = array_merge ($ entityContents , $ restEntityContents );
169
- }
170
- return $ entityContents ;
171
- } else {
172
- return [];
173
- }
174
- }
175
138
176
139
/**
177
140
* Create file with contents and create dir if needed
@@ -198,7 +161,7 @@ private function filePutContents($fullPath, $type, $urn, $contents)
198
161
. '< ' . lcfirst ($ type ) . 's '
199
162
. self ::XML_NAMESPACE
200
163
. self ::XML_SCHEMA_LOCATION . $ urn . '"> ' . PHP_EOL
201
- . $ contents . PHP_EOL
164
+ . ' ' . $ contents . PHP_EOL
202
165
. '</ ' . lcfirst ($ type ) . 's> ' . PHP_EOL ;
203
166
204
167
file_put_contents ($ fullPath , $ fullContents );
0 commit comments