Passed
Push — master ( b8dac1...9827a7 )
by
unknown
24:45 queued 10:04
created

ExtensionXmlPullParser::endElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
17
18
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
19
20
/**
21
 * Parser for TYPO3's extension.xml file.
22
 *
23
 * Depends on PHP ext/xmlreader which should be available
24
 * with PHP >= 5.1.0.
25
 * @internal This class is a specific ExtensionManager implementation and is not part of the Public TYPO3 API.
26
 */
27
class ExtensionXmlPullParser extends AbstractExtensionXmlParser
28
{
29
    protected \XMLReader $xmlReader;
30
31
    /**
32
     * Class constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->requiredPhpExtensions = 'xmlreader';
37
        $this->createParser();
38
    }
39
40
    /**
41
     * Create required parser
42
     */
43
    protected function createParser()
44
    {
45
        $this->xmlReader = new \XMLReader();
46
    }
47
48
    /**
49
     * Method parses an extensions.xml file.
50
     *
51
     * @param string $file GZIP stream resource
52
     * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of parser error
53
     */
54
    public function parseXml($file)
55
    {
56
        $this->createParser();
57
        if ($this->xmlReader->open($file, 'utf-8') === false) {
58
            throw new ExtensionManagerException(
59
                sprintf('Unable to open file resource %s.', $file),
60
                1476108651
61
            );
62
        }
63
        while ($this->xmlReader->read()) {
64
            if ($this->xmlReader->nodeType == \XMLReader::ELEMENT) {
65
                $this->startElement($this->xmlReader->name);
66
            } else {
67
                if ($this->xmlReader->nodeType == \XMLReader::END_ELEMENT) {
68
                    $this->endElement($this->xmlReader->name);
69
                } else {
70
                    continue;
71
                }
72
            }
73
        }
74
        $this->xmlReader->close();
75
    }
76
77
    /**
78
     * Method is invoked when parser accesses start tag of an element.
79
     *
80
     * @param string $elementName element name at parser's current position
81
     */
82
    protected function startElement($elementName)
83
    {
84
        switch ($elementName) {
85
            case 'extension':
86
                $this->extensionKey = $this->xmlReader->getAttribute('extensionkey');
87
                break;
88
            case 'version':
89
                $this->version = $this->xmlReader->getAttribute('version');
90
                break;
91
            case 'downloadcounter':
92
                // downloadcounter could be a child node of
93
                // extension or version
94
                if ($this->version == null) {
95
                    $this->extensionDownloadCounter = $this->getElementValue($elementName);
96
                } else {
97
                    $this->versionDownloadCounter = $this->getElementValue($elementName);
98
                }
99
                break;
100
            case 'title':
101
                $this->title = $this->getElementValue($elementName);
102
                break;
103
            case 'description':
104
                $this->description = $this->getElementValue($elementName);
105
                break;
106
            case 'state':
107
                $this->state = $this->getElementValue($elementName);
108
                break;
109
            case 'reviewstate':
110
                $this->reviewstate = $this->getElementValue($elementName);
111
                break;
112
            case 'category':
113
                $this->category = $this->getElementValue($elementName);
114
                break;
115
            case 'lastuploaddate':
116
                $this->lastuploaddate = $this->getElementValue($elementName);
117
                break;
118
            case 'uploadcomment':
119
                $this->uploadcomment = $this->getElementValue($elementName);
120
                break;
121
            case 'dependencies':
122
                $this->dependencies = $this->convertDependencies($this->getElementValue($elementName));
123
                break;
124
            case 'authorname':
125
                $this->authorname = $this->getElementValue($elementName);
126
                break;
127
            case 'authoremail':
128
                $this->authoremail = $this->getElementValue($elementName);
129
                break;
130
            case 'authorcompany':
131
                $this->authorcompany = $this->getElementValue($elementName);
132
                break;
133
            case 'ownerusername':
134
                $this->ownerusername = $this->getElementValue($elementName);
135
                break;
136
            case 't3xfilemd5':
137
                $this->t3xfilemd5 = $this->getElementValue($elementName);
138
                break;
139
            case 'documentation_link':
140
                $this->documentationLink = $this->getElementValue($elementName);
141
                break;
142
        }
143
    }
144
145
    /**
146
     * Method is invoked when parser accesses end tag of an element.
147
     *
148
     * @param string $elementName element name at parser's current position
149
     */
150
    protected function endElement($elementName)
151
    {
152
        switch ($elementName) {
153
            case 'extension':
154
                $this->resetProperties(true);
155
                break;
156
            case 'version':
157
                $this->notify();
158
                $this->resetProperties();
159
                break;
160
        }
161
    }
162
163
    /**
164
     * Method returns the value of an element at XMLReader's current
165
     * position.
166
     *
167
     * Method will read until it finds the end of the given element.
168
     * If element has no value, method returns NULL.
169
     *
170
     * @param string $elementName name of element to retrieve it's value from
171
     * @return string an element's value if it has a value, otherwise NULL
172
     */
173
    protected function getElementValue(&$elementName)
174
    {
175
        $value = null;
176
        if (!$this->xmlReader->isEmptyElement) {
177
            $value = '';
178
            while ($this->xmlReader->read()) {
179
                if ($this->xmlReader->nodeType == \XMLReader::TEXT || $this->xmlReader->nodeType == \XMLReader::CDATA || $this->xmlReader->nodeType == \XMLReader::WHITESPACE || $this->xmlReader->nodeType == \XMLReader::SIGNIFICANT_WHITESPACE) {
180
                    $value .= $this->xmlReader->value;
181
                } else {
182
                    if ($this->xmlReader->nodeType == \XMLReader::END_ELEMENT && $this->xmlReader->name === $elementName) {
183
                        break;
184
                    }
185
                }
186
            }
187
        }
188
        return $value;
189
    }
190
}
191