TYPO3 CMS  TYPO3_8-7
ExtensionXmlPullParser.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
24 {
28  public function __construct()
29  {
30  $this->requiredPhpExtensions = 'xmlreader';
31  }
32 
36  protected function createParser()
37  {
38  $this->objXml = new \XMLReader();
39  }
40 
47  public function parseXml($file)
48  {
49  $this->createParser();
50  if (!(is_object($this->objXml) && get_class($this->objXml) === 'XMLReader')) {
51  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640540);
52  }
53  if ($this->objXml->open($file, 'utf-8') === false) {
54  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
55  sprintf('Unable to open file resource %s.', $file),
56  1476108651
57  );
58  }
59  while ($this->objXml->read()) {
60  if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
61  $this->startElement($this->objXml->name);
62  } else {
63  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
64  $this->endElement($this->objXml->name);
65  } else {
66  continue;
67  }
68  }
69  }
70  $this->objXml->close();
71  }
72 
78  protected function startElement($elementName)
79  {
80  switch ($elementName) {
81  case 'extension':
82  $this->extensionKey = $this->objXml->getAttribute('extensionkey');
83  break;
84  case 'version':
85  $this->version = $this->objXml->getAttribute('version');
86  break;
87  case 'downloadcounter':
88  // downloadcounter could be a child node of
89  // extension or version
90  if ($this->version == null) {
91  $this->extensionDownloadCounter = $this->getElementValue($elementName);
92  } else {
93  $this->versionDownloadCounter = $this->getElementValue($elementName);
94  }
95  break;
96  case 'title':
97  $this->title = $this->getElementValue($elementName);
98  break;
99  case 'description':
100  $this->description = $this->getElementValue($elementName);
101  break;
102  case 'state':
103  $this->state = $this->getElementValue($elementName);
104  break;
105  case 'reviewstate':
106  $this->reviewstate = $this->getElementValue($elementName);
107  break;
108  case 'category':
109  $this->category = $this->getElementValue($elementName);
110  break;
111  case 'lastuploaddate':
112  $this->lastuploaddate = $this->getElementValue($elementName);
113  break;
114  case 'uploadcomment':
115  $this->uploadcomment = $this->getElementValue($elementName);
116  break;
117  case 'dependencies':
118  $this->dependencies = $this->convertDependencies($this->getElementValue($elementName));
119  break;
120  case 'authorname':
121  $this->authorname = $this->getElementValue($elementName);
122  break;
123  case 'authoremail':
124  $this->authoremail = $this->getElementValue($elementName);
125  break;
126  case 'authorcompany':
127  $this->authorcompany = $this->getElementValue($elementName);
128  break;
129  case 'ownerusername':
130  $this->ownerusername = $this->getElementValue($elementName);
131  break;
132  case 't3xfilemd5':
133  $this->t3xfilemd5 = $this->getElementValue($elementName);
134  break;
135  }
136  }
137 
143  protected function endElement($elementName)
144  {
145  switch ($elementName) {
146  case 'extension':
147  $this->resetProperties(true);
148  break;
149  case 'version':
150  $this->notify();
151  $this->resetProperties();
152  break;
153  }
154  }
155 
166  protected function getElementValue(&$elementName)
167  {
168  $value = null;
169  if (!$this->objXml->isEmptyElement) {
170  $value = '';
171  while ($this->objXml->read()) {
172  if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
173  $value .= $this->objXml->value;
174  } else {
175  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
176  break;
177  }
178  }
179  }
180  }
181  return $value;
182  }
183 }