1 | <?php |
||
14 | class XMLReader extends \XMLReader |
||
15 | { |
||
16 | use XMLInternalErrorsHelper; |
||
17 | |||
18 | const ZIP_WRAPPER = 'zip://'; |
||
19 | |||
20 | /** |
||
21 | * Opens the XML Reader to read a file located inside a ZIP file. |
||
22 | * |
||
23 | * @param string $zipFilePath Path to the ZIP file |
||
24 | * @param string $fileInsideZipPath Relative or absolute path of the file inside the zip |
||
25 | * @return bool TRUE on success or FALSE on failure |
||
26 | */ |
||
27 | 107 | public function openFileInZip($zipFilePath, $fileInsideZipPath) |
|
28 | { |
||
29 | 107 | $wasOpenSuccessful = false; |
|
30 | 107 | $realPathURI = $this->getRealPathURIForFileInZip($zipFilePath, $fileInsideZipPath); |
|
31 | |||
32 | // We need to check first that the file we are trying to read really exist because: |
||
33 | // - PHP emits a warning when trying to open a file that does not exist. |
||
34 | // - HHVM does not check if file exists within zip file (@link https://github.com/facebook/hhvm/issues/5779) |
||
35 | 107 | if ($this->fileExistsWithinZip($realPathURI)) { |
|
36 | 105 | $wasOpenSuccessful = $this->open($realPathURI, null, LIBXML_NONET); |
|
37 | } |
||
38 | |||
39 | 107 | return $wasOpenSuccessful; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns the real path for the given path components. |
||
44 | * This is useful to avoid issues on some Windows setup. |
||
45 | * |
||
46 | * @param string $zipFilePath Path to the ZIP file |
||
47 | * @param string $fileInsideZipPath Relative or absolute path of the file inside the zip |
||
48 | * @return string The real path URI |
||
49 | */ |
||
50 | 109 | public function getRealPathURIForFileInZip($zipFilePath, $fileInsideZipPath) |
|
54 | |||
55 | /** |
||
56 | * Returns whether the file at the given location exists |
||
57 | * |
||
58 | * @param string $zipStreamURI URI of a zip stream, e.g. "zip://file.zip#path/inside.xml" |
||
59 | * @return bool TRUE if the file exists, FALSE otherwise |
||
60 | */ |
||
61 | 112 | protected function fileExistsWithinZip($zipStreamURI) |
|
62 | { |
||
63 | 112 | $doesFileExists = false; |
|
64 | |||
65 | 112 | $pattern = '/zip:\/\/([^#]+)#(.*)/'; |
|
66 | 112 | if (preg_match($pattern, $zipStreamURI, $matches)) { |
|
67 | 112 | $zipFilePath = $matches[1]; |
|
68 | 112 | $innerFilePath = $matches[2]; |
|
69 | |||
70 | 112 | $zip = new \ZipArchive(); |
|
71 | 112 | if ($zip->open($zipFilePath) === true) { |
|
72 | 112 | $doesFileExists = ($zip->locateName($innerFilePath) !== false); |
|
73 | 112 | $zip->close(); |
|
74 | } |
||
75 | } |
||
76 | |||
77 | 112 | return $doesFileExists; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Move to next node in document |
||
82 | * @see \XMLReader::read |
||
83 | * |
||
84 | * @return bool TRUE on success or FALSE on failure |
||
85 | * @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred |
||
86 | */ |
||
87 | 105 | public function read() |
|
97 | |||
98 | /** |
||
99 | * Read until the element with the given name is found, or the end of the file. |
||
100 | * |
||
101 | * @param string $nodeName Name of the node to find |
||
102 | * @return bool TRUE on success or FALSE on failure |
||
103 | * @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred |
||
104 | */ |
||
105 | 86 | public function readUntilNodeFound($nodeName) |
|
114 | |||
115 | /** |
||
116 | * Move cursor to next node skipping all subtrees |
||
117 | * @see \XMLReader::next |
||
118 | * |
||
119 | * @param string|void $localName The name of the next node to move to |
||
120 | * @return bool TRUE on success or FALSE on failure |
||
121 | * @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred |
||
122 | */ |
||
123 | 38 | public function next($localName = null) |
|
133 | |||
134 | /** |
||
135 | * @param string $nodeName |
||
136 | * @return bool Whether the XML Reader is currently positioned on the starting node with given name |
||
137 | */ |
||
138 | 104 | public function isPositionedOnStartingNode($nodeName) |
|
142 | |||
143 | /** |
||
144 | * @param string $nodeName |
||
145 | * @return bool Whether the XML Reader is currently positioned on the ending node with given name |
||
146 | */ |
||
147 | 38 | public function isPositionedOnEndingNode($nodeName) |
|
151 | |||
152 | /** |
||
153 | * @param string $nodeName |
||
154 | * @param int $nodeType |
||
155 | * @return bool Whether the XML Reader is currently positioned on the node with given name and type |
||
156 | */ |
||
157 | 104 | private function isPositionedOnNode($nodeName, $nodeType) |
|
167 | |||
168 | /** |
||
169 | * @return string The name of the current node, un-prefixed |
||
170 | */ |
||
171 | 36 | public function getCurrentNodeName() |
|
175 | } |
||
176 |