TYPO3 CMS  TYPO3_7-6
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 
28 {
32  public function __construct()
33  {
34  $this->requiredPhpExtensions = 'xmlreader';
35  }
36 
42  protected function createParser()
43  {
44  $this->objXml = new \XMLReader();
45  }
46 
54  public function parseXml($file)
55  {
56  $this->createParser();
57  if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
58  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640540);
59  }
60  if ($this->objXml->open($file, 'utf-8') === false) {
61  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file));
62  }
63  while ($this->objXml->read()) {
64  if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
65  $this->startElement($this->objXml->name);
66  } else {
67  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
68  $this->endElement($this->objXml->name);
69  } else {
70  continue;
71  }
72  }
73  }
74  $this->objXml->close();
75  }
76 
83  protected function startElement($elementName)
84  {
85  switch ($elementName) {
86  case 'extension':
87  $this->extensionKey = $this->objXml->getAttribute('extensionkey');
88  break;
89  case 'version':
90  $this->version = $this->objXml->getAttribute('version');
91  break;
92  case 'downloadcounter':
93  // downloadcounter could be a child node of
94  // extension or version
95  if ($this->version == null) {
96  $this->extensionDownloadCounter = $this->getElementValue($elementName);
97  } else {
98  $this->versionDownloadCounter = $this->getElementValue($elementName);
99  }
100  break;
101  case 'title':
102  $this->title = $this->getElementValue($elementName);
103  break;
104  case 'description':
105  $this->description = $this->getElementValue($elementName);
106  break;
107  case 'state':
108  $this->state = $this->getElementValue($elementName);
109  break;
110  case 'reviewstate':
111  $this->reviewstate = $this->getElementValue($elementName);
112  break;
113  case 'category':
114  $this->category = $this->getElementValue($elementName);
115  break;
116  case 'lastuploaddate':
117  $this->lastuploaddate = $this->getElementValue($elementName);
118  break;
119  case 'uploadcomment':
120  $this->uploadcomment = $this->getElementValue($elementName);
121  break;
122  case 'dependencies':
123  $this->dependencies = $this->convertDependencies($this->getElementValue($elementName));
124  break;
125  case 'authorname':
126  $this->authorname = $this->getElementValue($elementName);
127  break;
128  case 'authoremail':
129  $this->authoremail = $this->getElementValue($elementName);
130  break;
131  case 'authorcompany':
132  $this->authorcompany = $this->getElementValue($elementName);
133  break;
134  case 'ownerusername':
135  $this->ownerusername = $this->getElementValue($elementName);
136  break;
137  case 't3xfilemd5':
138  $this->t3xfilemd5 = $this->getElementValue($elementName);
139  break;
140  }
141  }
142 
149  protected function endElement($elementName)
150  {
151  switch ($elementName) {
152  case 'extension':
153  $this->resetProperties(true);
154  break;
155  case 'version':
156  $this->notify();
157  $this->resetProperties();
158  break;
159  }
160  }
161 
172  protected function getElementValue(&$elementName)
173  {
174  $value = null;
175  if (!$this->objXml->isEmptyElement) {
176  $value = '';
177  while ($this->objXml->read()) {
178  if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
179  $value .= $this->objXml->value;
180  } else {
181  if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
182  break;
183  }
184  }
185  }
186  }
187  return $value;
188  }
189 }