diff --git a/src/Xml/Reader/Loader/xml_file_loader.php b/src/Xml/Reader/Loader/xml_file_loader.php index 214ebd4a..b684bf0c 100644 --- a/src/Xml/Reader/Loader/xml_file_loader.php +++ b/src/Xml/Reader/Loader/xml_file_loader.php @@ -13,13 +13,13 @@ /** * @return Closure(): XMLReader */ -function xml_file_loader(string $file): Closure +function xml_file_loader(string $file, ?string $encoding = null, int $flags = 0): Closure { return static fn (): XMLReader => disallow_issues( - static function () use ($file): XMLReader { + static function () use ($file, $encoding, $flags): XMLReader { Assert::fileExists($file); return disallow_libxml_false_returns( - XMLReader::open($file), + XMLReader::fromUri($file, $encoding, $flags), 'Could not open the provided XML file!' ); } diff --git a/src/Xml/Reader/Loader/xml_stream_loader.php b/src/Xml/Reader/Loader/xml_stream_loader.php new file mode 100644 index 00000000..c62cfbbd --- /dev/null +++ b/src/Xml/Reader/Loader/xml_stream_loader.php @@ -0,0 +1,27 @@ + disallow_issues( + static function () use ($stream, $encoding, $flags, $documentUri): XMLReader { + return disallow_libxml_false_returns( + XMLReader::fromStream($stream, $encoding, $flags, $documentUri), + 'Could not read the provided XML stream!' + ); + } + ); +} diff --git a/src/Xml/Reader/Loader/xml_string_loader.php b/src/Xml/Reader/Loader/xml_string_loader.php index 2b781282..bdc480db 100644 --- a/src/Xml/Reader/Loader/xml_string_loader.php +++ b/src/Xml/Reader/Loader/xml_string_loader.php @@ -13,14 +13,14 @@ /** * @return Closure(): XMLReader */ -function xml_string_loader(string $xml): Closure +function xml_string_loader(string $xml, ?string $encoding = null, int $flags = 0): Closure { return static fn (): XMLReader => disallow_issues( - static function () use ($xml): XMLReader { + static function () use ($xml, $encoding, $flags): XMLReader { Assert::notEmpty($xml, 'The provided XML can not be empty!'); return disallow_libxml_false_returns( - XMLReader::XML($xml), + XMLReader::fromString($xml, $encoding, $flags), 'Could not read the provided XML!' ); } diff --git a/src/Xml/Reader/Reader.php b/src/Xml/Reader/Reader.php index 2bb39272..3759f2ed 100644 --- a/src/Xml/Reader/Reader.php +++ b/src/Xml/Reader/Reader.php @@ -14,6 +14,7 @@ use function VeeWee\Xml\ErrorHandling\stop_on_first_issue; use function VeeWee\Xml\Internal\configure; use function VeeWee\Xml\Reader\Loader\xml_file_loader; +use function VeeWee\Xml\Reader\Loader\xml_stream_loader; use function VeeWee\Xml\Reader\Loader\xml_string_loader; final class Reader @@ -57,6 +58,15 @@ public static function fromXmlString(string $xml, callable ... $configurators): return self::configure(xml_string_loader($xml), ...$configurators); } + /** + * @param resource $stream + * @param list $configurators + */ + public static function fromXmlStream(mixed $stream, callable ... $configurators): self + { + return self::configure(xml_stream_loader($stream), ...$configurators); + } + /** * @param callable(NodeSequence): bool $matcher * diff --git a/src/Xml/Writer/Applicative/Applicative.php b/src/Xml/Writer/Applicative/Applicative.php new file mode 100644 index 00000000..760f445e --- /dev/null +++ b/src/Xml/Writer/Applicative/Applicative.php @@ -0,0 +1,11 @@ +flush($empty); + }; +} diff --git a/src/Xml/Writer/Configurator/open.php b/src/Xml/Writer/Configurator/open.php deleted file mode 100644 index dc9d0403..00000000 --- a/src/Xml/Writer/Configurator/open.php +++ /dev/null @@ -1,28 +0,0 @@ - disallow_issues( - static function () use ($writer, $opener): XMLWriter { - disallow_libxml_false_returns( - $opener($writer), - 'Could not open the writer stream.' - ); - - return $writer; - } - ); -} diff --git a/src/Xml/Writer/Opener/Opener.php b/src/Xml/Writer/Opener/Opener.php index ea90f5cf..85596200 100644 --- a/src/Xml/Writer/Opener/Opener.php +++ b/src/Xml/Writer/Opener/Opener.php @@ -8,5 +8,5 @@ interface Opener { - public function __invoke(XMLWriter $writer): bool; + public function __invoke(): XMLWriter; } diff --git a/src/Xml/Writer/Opener/memory_opener.php b/src/Xml/Writer/Opener/memory_opener.php index c964ec8b..0f96126a 100644 --- a/src/Xml/Writer/Opener/memory_opener.php +++ b/src/Xml/Writer/Opener/memory_opener.php @@ -8,11 +8,9 @@ use XMLWriter; /** - * @return Closure(XMLWriter): bool XMLWriter + * @return Closure(): XMLWriter */ function memory_opener(): Closure { - return static function (XMLWriter $writer): bool { - return $writer->openMemory(); - }; + return static fn () => XMLWriter::toMemory(); } diff --git a/src/Xml/Writer/Opener/xml_file_opener.php b/src/Xml/Writer/Opener/xml_file_opener.php index 066e264f..9d0400e2 100644 --- a/src/Xml/Writer/Opener/xml_file_opener.php +++ b/src/Xml/Writer/Opener/xml_file_opener.php @@ -12,16 +12,16 @@ /** * @param non-empty-string $file * - * @return Closure(XMLWriter): bool XMLWriter + * @return Closure(): XMLWriter */ function xml_file_opener(string $file): Closure { - return static function (XMLWriter $writer) use ($file) : bool { + return static function () use ($file) : XMLWriter { // Try to create the file first. // If the file exists, it will truncated. (Default behaviour of XMLWriter as well) // If it cannot be created, it will throw exceptions. write($file, '', WriteMode::Truncate); - return $writer->openUri($file); + return XMLWriter::toUri($file); }; } diff --git a/src/Xml/Writer/Opener/xml_stream_opener.php b/src/Xml/Writer/Opener/xml_stream_opener.php new file mode 100644 index 00000000..4fcf9bea --- /dev/null +++ b/src/Xml/Writer/Opener/xml_stream_opener.php @@ -0,0 +1,22 @@ + $configurators */ - public static function configure(callable ... $configurators): self + public static function configure(callable $opener, callable ... $configurators): self { - return self::fromUnsafeWriter(new XMLWriter(), ...$configurators); + return self::fromUnsafeWriter($opener(), ...$configurators); } /** @@ -45,8 +47,20 @@ public static function fromUnsafeWriter(XMLWriter $writer, callable ... $configu */ public static function forFile(string $file, callable ... $configurators): self { - return self::configure( - open(xml_file_opener($file)), + return self::open( + xml_file_opener($file), + ...$configurators + ); + } + + /** + * @param resource $stream + * @param list<(callable(XMLWriter): XMLWriter)> $configurators + */ + public static function forStream(mixed $stream, callable ... $configurators): self + { + return self::open( + xml_stream_opener($stream), ...$configurators ); } @@ -56,8 +70,8 @@ public static function forFile(string $file, callable ... $configurators): self */ public static function inMemory(callable ... $configurators): self { - return self::configure( - open(memory_opener()), + return self::open( + memory_opener(), ...$configurators ); } diff --git a/src/bootstrap.php b/src/bootstrap.php index f44991d6..392fd8e9 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -125,6 +125,7 @@ 'Xml\Reader\Configurator\substitute_entities' => __DIR__.'/Xml/Reader/Configurator/substitute_entities.php', 'Xml\Reader\Configurator\xsd_schema' => __DIR__.'/Xml/Reader/Configurator/xsd_schema.php', 'Xml\Reader\Loader\xml_file_loader' => __DIR__.'/Xml/Reader/Loader/xml_file_loader.php', + 'Xml\Reader\Loader\xml_stream_loader' => __DIR__.'/Xml/Reader/Loader/xml_stream_loader.php', 'Xml\Reader\Loader\xml_string_loader' => __DIR__.'/Xml/Reader/Loader/xml_string_loader.php', 'Xml\Reader\Matcher\all' => __DIR__.'/Xml/Reader/Matcher/all.php', 'Xml\Reader\Matcher\any' => __DIR__.'/Xml/Reader/Matcher/any.php', @@ -142,6 +143,7 @@ 'Xml\Reader\Matcher\nested' => __DIR__.'/Xml/Reader/Matcher/nested.php', 'Xml\Reader\Matcher\not' => __DIR__.'/Xml/Reader/Matcher/not.php', 'Xml\Reader\Matcher\sequence' => __DIR__.'/Xml/Reader/Matcher/sequence.php', + 'Xml\Writer\Applicative\flush' => __DIR__.'/Xml/Writer/Applicative/flush.php', 'Xml\Writer\Builder\attribute' => __DIR__.'/Xml/Writer/Builder/attribute.php', 'Xml\Writer\Builder\attributes' => __DIR__.'/Xml/Writer/Builder/attributes.php', 'Xml\Writer\Builder\cdata' => __DIR__.'/Xml/Writer/Builder/cdata.php', @@ -159,10 +161,10 @@ 'Xml\Writer\Builder\raw' => __DIR__.'/Xml/Writer/Builder/raw.php', 'Xml\Writer\Builder\value' => __DIR__.'/Xml/Writer/Builder/value.php', 'Xml\Writer\Configurator\indentation' => __DIR__.'/Xml/Writer/Configurator/indentation.php', - 'Xml\Writer\Configurator\open' => __DIR__.'/Xml/Writer/Configurator/open.php', 'Xml\Writer\Mapper\memory_output' => __DIR__.'/Xml/Writer/Mapper/memory_output.php', 'Xml\Writer\Opener\memory_opener' => __DIR__.'/Xml/Writer/Opener/memory_opener.php', 'Xml\Writer\Opener\xml_file_opener' => __DIR__.'/Xml/Writer/Opener/xml_file_opener.php', + 'Xml\Writer\Opener\xml_stream_opener' => __DIR__.'/Xml/Writer/Opener/xml_stream_opener.php', 'Xml\Xsd\Schema\Manipulator\base_path' => __DIR__.'/Xml/Xsd/Schema/Manipulator/base_path.php', 'Xml\Xsd\Schema\Manipulator\overwrite_with_local_files' => __DIR__.'/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php', 'Xml\Xslt\Configurator\all_functions' => __DIR__.'/Xml/Xslt/Configurator/all_functions.php', diff --git a/stubs/XMLReader.phpstub b/stubs/XMLReader.phpstub index 7e008bc8..2c742f11 100644 --- a/stubs/XMLReader.phpstub +++ b/stubs/XMLReader.phpstub @@ -1,26 +1,225 @@