|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Task\File\Xml; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Model\AbstractConfigurableTask; |
| 17 | +use CleverAge\ProcessBundle\Model\FinalizableTaskInterface; |
| 18 | +use CleverAge\ProcessBundle\Model\IterableTaskInterface; |
| 19 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 20 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 21 | + |
| 22 | +/** |
| 23 | + * Read an XML file element by element with \XMLReader, so that the whole file is never loaded in memory. |
| 24 | + */ |
| 25 | +class XmlStreamReaderTask extends AbstractConfigurableTask implements IterableTaskInterface, FinalizableTaskInterface |
| 26 | +{ |
| 27 | + protected ?\XMLReader $reader = null; |
| 28 | + |
| 29 | + protected ?string $filePath = null; |
| 30 | + |
| 31 | + public function execute(ProcessState $state): void |
| 32 | + { |
| 33 | + $nodeName = (string) $this->getOption($state, 'node_name'); |
| 34 | + if ($this->reader instanceof \XMLReader) { |
| 35 | + // Skip the subtree of the current element: nested elements with the same name are not iterated |
| 36 | + $found = $this->moveToElement($nodeName, true); |
| 37 | + } else { |
| 38 | + $this->open($this->getFilePath($state)); |
| 39 | + $found = $this->moveToElement($nodeName, false); |
| 40 | + } |
| 41 | + |
| 42 | + if (!$found) { |
| 43 | + // Release the file to allow the following iteration to read it again |
| 44 | + $this->close(); |
| 45 | + $state->setSkipped(true); |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $state->setOutput($this->expand()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * The reader is moved in execute(), so that XML errors are handled by the error strategy of the task. |
| 55 | + */ |
| 56 | + public function next(ProcessState $state): bool |
| 57 | + { |
| 58 | + return $this->reader instanceof \XMLReader; |
| 59 | + } |
| 60 | + |
| 61 | + public function finalize(ProcessState $state): void |
| 62 | + { |
| 63 | + $this->close(); |
| 64 | + } |
| 65 | + |
| 66 | + protected function getFilePath(ProcessState $state): string |
| 67 | + { |
| 68 | + $filePath = $this->getOption($state, 'file_path') ?? $state->getInput(); |
| 69 | + if (!\is_string($filePath) || '' === $filePath) { |
| 70 | + throw new \UnexpectedValueException('No file path given in the "file_path" option or as input'); |
| 71 | + } |
| 72 | + |
| 73 | + return $filePath; |
| 74 | + } |
| 75 | + |
| 76 | + protected function open(string $filePath): void |
| 77 | + { |
| 78 | + if (!is_file($filePath) || !is_readable($filePath)) { |
| 79 | + throw new \UnexpectedValueException(\sprintf('XML file "%s" does not exist or is not readable', $filePath)); |
| 80 | + } |
| 81 | + |
| 82 | + $reader = new \XMLReader(); |
| 83 | + if (!@$reader->open($filePath)) { |
| 84 | + throw new \RuntimeException(\sprintf('Could not open XML file "%s"', $filePath)); |
| 85 | + } |
| 86 | + $this->reader = $reader; |
| 87 | + $this->filePath = $filePath; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Move the reader to the next element named $nodeName, return false at the end of the file. |
| 92 | + */ |
| 93 | + protected function moveToElement(string $nodeName, bool $skipCurrent): bool |
| 94 | + { |
| 95 | + $reader = $this->getReader(); |
| 96 | + |
| 97 | + $previousUseErrors = libxml_use_internal_errors(true); |
| 98 | + libxml_clear_errors(); |
| 99 | + try { |
| 100 | + $found = $skipCurrent ? $reader->next() : $reader->read(); |
| 101 | + while ($found && (\XMLReader::ELEMENT !== $reader->nodeType || $reader->name !== $nodeName)) { |
| 102 | + $found = $reader->read(); |
| 103 | + } |
| 104 | + $errors = libxml_get_errors(); |
| 105 | + } finally { |
| 106 | + libxml_clear_errors(); |
| 107 | + libxml_use_internal_errors($previousUseErrors); |
| 108 | + } |
| 109 | + |
| 110 | + $this->throwOnErrors($errors); |
| 111 | + |
| 112 | + return $found; |
| 113 | + } |
| 114 | + |
| 115 | + protected function expand(): \DOMElement |
| 116 | + { |
| 117 | + $reader = $this->getReader(); |
| 118 | + $document = new \DOMDocument(); |
| 119 | + |
| 120 | + $previousUseErrors = libxml_use_internal_errors(true); |
| 121 | + libxml_clear_errors(); |
| 122 | + try { |
| 123 | + $node = $reader->expand($document); |
| 124 | + $errors = libxml_get_errors(); |
| 125 | + } finally { |
| 126 | + libxml_clear_errors(); |
| 127 | + libxml_use_internal_errors($previousUseErrors); |
| 128 | + } |
| 129 | + |
| 130 | + $this->throwOnErrors($errors); |
| 131 | + if (!$node instanceof \DOMElement) { |
| 132 | + throw new \UnexpectedValueException(\sprintf('Could not expand element "%s" of XML file "%s"', $reader->name, $this->filePath)); |
| 133 | + } |
| 134 | + |
| 135 | + // Each element gets its own document, so that it can be evaluated with xpath like the output of XmlReaderTask |
| 136 | + $document->appendChild($node); |
| 137 | + |
| 138 | + return $node; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Warnings are tolerated, errors (e.g. mismatched tags) and fatal errors are not. |
| 143 | + * |
| 144 | + * @param array<\LibXMLError> $errors |
| 145 | + */ |
| 146 | + protected function throwOnErrors(array $errors): void |
| 147 | + { |
| 148 | + $errors = array_filter($errors, static fn (\LibXMLError $error): bool => \LIBXML_ERR_WARNING !== $error->level); |
| 149 | + if ([] === $errors) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + $messages = array_map( |
| 154 | + static fn (\LibXMLError $error): string => \sprintf('%s (line %d, column %d)', trim($error->message), $error->line, $error->column), |
| 155 | + $errors, |
| 156 | + ); |
| 157 | + $filePath = $this->filePath; |
| 158 | + $this->close(); |
| 159 | + |
| 160 | + throw new \UnexpectedValueException(\sprintf('Invalid XML in file "%s": %s', $filePath, implode('; ', $messages))); |
| 161 | + } |
| 162 | + |
| 163 | + protected function getReader(): \XMLReader |
| 164 | + { |
| 165 | + if (!$this->reader instanceof \XMLReader) { |
| 166 | + throw new \LogicException('No XML file opened'); |
| 167 | + } |
| 168 | + |
| 169 | + return $this->reader; |
| 170 | + } |
| 171 | + |
| 172 | + protected function close(): void |
| 173 | + { |
| 174 | + $this->reader?->close(); |
| 175 | + $this->reader = null; |
| 176 | + $this->filePath = null; |
| 177 | + } |
| 178 | + |
| 179 | + protected function configureOptions(OptionsResolver $resolver): void |
| 180 | + { |
| 181 | + $resolver->setDefault('file_path', null); |
| 182 | + $resolver->setAllowedTypes('file_path', ['string', 'null']); |
| 183 | + |
| 184 | + $resolver->setRequired('node_name'); |
| 185 | + $resolver->setAllowedTypes('node_name', 'string'); |
| 186 | + } |
| 187 | +} |
0 commit comments