e49f9d7b974c50dd09ba2f520445133020da39ff
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Parser / MirrorXmlPullParser.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
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
17 /**
18 * Parser for TYPO3's mirrors.xml file.
19 *
20 * Depends on PHP ext/xmlreader which should be available
21 * with PHP >= 5.1.0.
22 */
23 class MirrorXmlPullParser extends AbstractMirrorXmlParser
24 {
25 /**
26 * Class constructor.
27 *
28 * @access public
29 */
30 public function __construct()
31 {
32 $this->requiredPhpExtensions = 'xmlreader';
33 }
34
35 /**
36 * Create required parser
37 *
38 * @return void
39 */
40 protected function createParser()
41 {
42 $this->objXml = new \XMLReader();
43 }
44
45 /**
46 * Method parses an extensions.xml file.
47 *
48 * @param string $file file resource, typically a stream
49 * @return void
50 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
51 */
52 public function parseXml($file)
53 {
54 $this->createParser();
55 if (!(is_object($this->objXml) && get_class($this->objXml) == 'XMLReader')) {
56 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640820);
57 }
58 if ($this->objXml->open($file, 'utf-8') === false) {
59 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640893);
60 }
61 while ($this->objXml->read()) {
62 if ($this->objXml->nodeType == \XMLReader::ELEMENT) {
63 $this->startElement($this->objXml->name);
64 } else {
65 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT) {
66 $this->endElement($this->objXml->name);
67 } else {
68 continue;
69 }
70 }
71 }
72 $this->objXml->close();
73 }
74
75 /**
76 * Method is invoked when parser accesses start tag of an element.
77 *
78 * @param string $elementName element name at parser's current position
79 * @return void
80 * @see endElement()
81 */
82 protected function startElement($elementName)
83 {
84 switch ($elementName) {
85 case 'title':
86 $this->title = $this->getElementValue($elementName);
87 break;
88 case 'host':
89 $this->host = $this->getElementValue($elementName);
90 break;
91 case 'path':
92 $this->path = $this->getElementValue($elementName);
93 break;
94 case 'country':
95 $this->country = $this->getElementValue($elementName);
96 break;
97 case 'name':
98 $this->sponsorname = $this->getElementValue($elementName);
99 break;
100 case 'link':
101 $this->sponsorlink = $this->getElementValue($elementName);
102 break;
103 case 'logo':
104 $this->sponsorlogo = $this->getElementValue($elementName);
105 break;
106 default:
107 // Do nothing
108 }
109 }
110
111 /**
112 * Method is invoked when parser accesses end tag of an element.
113 *
114 * @param string $elementName element name at parser's current position
115 * @return void
116 * @see startElement()
117 */
118 protected function endElement($elementName)
119 {
120 switch ($elementName) {
121 case 'mirror':
122 $this->notify();
123 $this->resetProperties();
124 break;
125 default:
126 // Do nothing
127 }
128 }
129
130 /**
131 * Method returns the value of an element at XMLReader's current
132 * position.
133 *
134 * Method will read until it finds the end of the given element.
135 * If element has no value, method returns NULL.
136 *
137 * @param string &$elementName name of element to retrieve it's value from
138 * @return string an element's value if it has a value, otherwise NULL
139 */
140 protected function getElementValue(&$elementName)
141 {
142 $value = null;
143 if (!$this->objXml->isEmptyElement) {
144 $value = '';
145 while ($this->objXml->read()) {
146 if ($this->objXml->nodeType == \XMLReader::TEXT || $this->objXml->nodeType == \XMLReader::CDATA || $this->objXml->nodeType == \XMLReader::WHITESPACE || $this->objXml->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
147 $value .= $this->objXml->value;
148 } else {
149 if ($this->objXml->nodeType == \XMLReader::END_ELEMENT && $this->objXml->name === $elementName) {
150 break;
151 }
152 }
153 }
154 }
155 return $value;
156 }
157 }