rfc:xmlreader_writer_streams

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:xmlreader_writer_streams [2024/04/21 17:50] nielsdosrfc:xmlreader_writer_streams [2024/05/27 19:42] (current) nielsdos
Line 1: Line 1:
-====== PHP RFC: Add openStream() to XML{Reader,Writer} ====== +====== PHP RFC: Add stream open functions to XML{Reader,Writer} ====== 
-  * Version: 0.9+  * Version: 0.11.0
   * Date: 2024-04-21   * Date: 2024-04-21
   * Author: Niels Dossche <nielsdos@php.net>   * Author: Niels Dossche <nielsdos@php.net>
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: https://wiki.php.net/rfc/xmlreader_writer_streams   * First Published at: https://wiki.php.net/rfc/xmlreader_writer_streams
  
 ===== Introduction ===== ===== Introduction =====
  
-The <php>XMLReader</php> and <php>XMLWriter</php> classes deal with XML in a stream-oriented manner. The former implements an XML "pull parser". This means that instead of keeping the data in memory or building a document tree, the document is streamed and the developer can instruct <php>XMLReader</php> to parse chunks at the current cursor and either process or skip the data. The advantage is that you can process and filter large documents. It is most often used as a lower-level building block for more complex handling of large XML documents. Similarly, <php>XMLWriter</php> writes an XML document to a stream or memory by using functions like <php>startElement</php> and <php>writeElement</php>.+The <php>XMLReader</php> and <php>XMLWriter</php> classes deal with XML in a stream-oriented manner. The former implements an XML "pull parser". This means that instead of keeping the data in memory or building a document tree, the document is streamed and the developer can instruct <php>XMLReader</php> to parse chunks at the current cursor and either process or skip the data. The advantage is that developers can process and filter large documents while requiring few resources. It is most often used as a lower-level building block for more complex handling of large XML documents. Similarly, <php>XMLWriter</php> writes an XML document to a stream or memory by using functions like <php>startElement</php> and <php>writeElement</php>.
  
-There is however a strange limitation to these classes: they cannot operate on an already-open stream! This is bizarre as the APIs (both internally and user-facing) are stream-oriented. Streams that are already open are common when working with HTTP requests for example, data passed from a framework, or just XML data embedded in an existing stream. The lack of an API that works with already-opened streams causes developers to rely on workarounds, e.g. reading the stream entirely to memory and then using the <php>XMLReader</php> APIs, or writing an XML file using <php>XMLWriter</php> and then having to re-read it. That's just wasteful and needlessly difficult. This RFC aims to fix that problem and aims to fix some other inconsistency as well.+There is however a strange limitation to these classes: they cannot operate on an already-open stream! This is bizarre as the APIs (both internally and user-facing) are stream-oriented. Streams that are already open are common when working, for example, with HTTP requests, data passed from a framework, or just XML data embedded in an existing stream. The lack of an API that works with already-opened streams causes developers to rely on workarounds, e.g. reading the stream entirely to memory and then using the <php>XMLReader</php> APIs, or writing an XML file using <php>XMLWriter</php> and then having to pass that into an already-open stream. That's just wasteful and needlessly difficult. This RFC aims to fix that problem and aims to fix some other inconsistency as well.
  
 ===== Proposal ===== ===== Proposal =====
  
-I propose to add new function called <php>openStream()</php> to both <php>XMLReader</php> and <php>XMLWriter</php>. Here is how they would look like:+==== Main Proposal ==== 
 + 
 +I propose to add new functions, one to <php>XMLReader</php> and one to <php>XMLWriter</php>, to create an instance from a stream. Here is how they would look like:
  
 <PHP> <PHP>
 class XMLReader { class XMLReader {
     /** @param resource $stream */     /** @param resource $stream */
-    public static function openStream($stream, ?string $baseUri = null, ?string $encoding = null, int $flags = 0): XMLReader {}+    public static function fromStream($stream, ?string $encoding = null, int $flags = 0, ?string $documentUri = null): static {}
 } }
  
 class XMLWriter { class XMLWriter {
     /** @param resource $stream */     /** @param resource $stream */
-    public function openStream($stream): void {}+    public static function toStream($stream): static {}
 } }
 </PHP> </PHP>
  
-The signature for <php>XMLReader::openStream()</php> is heavily inspired by the existing function <php>public static XMLReader::open(string $uri, ?string $encoding = null, int $flags = 0): bool|XMLReader</php> that operates on files. +The signatures are heavily inspired by the existing function <php>public static XMLReader::open(string $uri, ?string $encoding = null, int $flags = 0): bool|XMLReader</php> that operate on files. 
-However, a major difference is that <php>XMLReader::openStream()</php> is static-only, whereas the other open functions of <php>XMLReader</php> can either be statically or non-statically called and change their return-value behaviour depending on that. As we seem to try to get away from such strange APIs, I decided to only make a static variation available. When you try to call this method non-statically, you'll get an Error to avoid confusion stating "XMLReader::openStream() can only be called statically".+However, a major difference is that <php>XMLReader::fromStream()</php> is static-only, whereas the other open functions of <php>XMLReader</php> can either be statically or non-statically called and change their return-value behaviour depending on that. The disadvantage of the existing static methods is that they can only return an instance of <php>XMLReader</php>, therefore when <php>XMLReader</php> is inherited by a user subclass we run into the problem that it doesn't return an object of the right type. We solve this by choosing <php>static</php> as return-type, and letting the method internally call the constructor of the static type (with no arguments). As we seem to move away from overloaded functions, I decided to only make a static method variation available.
  
-The signature for <php>XMLWriter::openStream()</php> should be self-explanatory. It is also modeled like the other open functions, but they are considerably simpler. You'll also notice the lack of an encoding argument, and that's because this is already handled by the <php>XMLWriter::startDocument()</php> function.+The <php>$documentUri</php> parameter is used mostly for when libxml outputs error messages, such that you can put an origin name in there. 
 + 
 +The signature for <php>XMLWriter::toStream()</php> should be self-explanatory. It is also modeled like the other open functions, but it is considerably simpler. You'll also notice the lack of an encoding argument, and that's because this is already handled by the <php>XMLWriter::startDocument()</php> function.
  
 While implementing this, I found some strange behaviour regarding the <php>?string $encoding</php> parameter of the existing functions <php>XMLReader::open()</php> and <php>XMLReader::XML()</php>. The first oddity is that they emit a warning instead of throwing a ValueError when the encoding contains NULL bytes. This is inconsistent with how other functions handle it. I propose to promote this warning to a ValueError instead. The second oddity is that invalid encoding names are ignored entirely. This means that it won't emit a warning or anything, but just silently not set the encoding. This can hide bugs. I propose to also throw a ValueError in this case stating "Argument #X ($encoding) must be a valid character encoding". While implementing this, I found some strange behaviour regarding the <php>?string $encoding</php> parameter of the existing functions <php>XMLReader::open()</php> and <php>XMLReader::XML()</php>. The first oddity is that they emit a warning instead of throwing a ValueError when the encoding contains NULL bytes. This is inconsistent with how other functions handle it. I propose to promote this warning to a ValueError instead. The second oddity is that invalid encoding names are ignored entirely. This means that it won't emit a warning or anything, but just silently not set the encoding. This can hide bugs. I propose to also throw a ValueError in this case stating "Argument #X ($encoding) must be a valid character encoding".
 +
 +An earlier version of this RFC proposed adding <php>openStream()</php> methods to both classes, but the naming was not ideal and the behaviour of being an instance method was not liked. Therefore, this was changed to static-only methods that return an instance of the respective class.
 +
 +==== Consistency ====
 +
 +We're adding new static named constructors to the <php>XMLWriter</php> and <php>XMLReader</php> classes. However, <php>XMLWriter</php> doesn't have static constructors yet and <php>XMLReader</php> has this hybrid static methods/instance methods we talked about earlier. Those existing methods also can't be used in subclasses because they return <php>XMLWriter</php> or <php>XMLReader</php> instead of <php>static</php>.
 +
 +The idea is to add the following static named constructors for consistency with the newly proposed methods, with the same arguments as their existing counterpart:
 +  - <php>XMLReader::fromUrl(string $url, ?string $encoding = null, int $flags = 0): static</php> as a new version of <php>XMLReader::open(...)</php>
 +  - <php>XMLReader::fromString(string $source, ?string $encoding = null, int $flags = 0): static</php> as a new version of <php>XMLReader::XML(...)</php>
 +  - <php>XMLWriter::toMemory(): static</php> as a new version of <php>XMLWriter::openMemory(...)</php>
 +  - <php>XMLWriter::toUrl(string $url): static</php> as a new version of <php>XMLWriter::openUri(...)</php>
 +
 +Note I used Url here instead of Uri because that's the more accurate term: it actually locates the resource instead of just identifying it.
 +
 +This does not aim to deprecate any existing methods. We will merely update the documentation to point users towards the new constructors.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 39: Line 59:
 There are three minor BC breaks. There are three minor BC breaks.
  
-The first one is the fact that we're adding the <php>openStream()</php> method. If a user extends the <php>XMLReader</php> or <php>XMLWriter</php> class, and their extension implements a method with the same name but an incompatible signature, a compile error will occur. I analyzed the top 2500 Composer packages, and only found one package that contains the method name openStream and it wasn'in a class that extends either classes. This means that the top 2500 packages don't suffer a BC break because of this. That doesn't mean there will be none, but it gives a good indication.+The first one is the fact that we're adding new methods. If a user extends the <php>XMLReader</php> or <php>XMLWriter</php> class, and their class implements a method with the same name but an incompatible signature, a compile error will occur. I analyzed the top 2500 Composer packages, and none used any of the proposed function names in subclasses of the XML classes. This means that the top 2500 packages don't suffer a BC break because of this. That doesn't mean there will be none, but it gives a good indication.
  
 The second BC break is caused by throwing a <php>ValueError</php> on invalid encodings instead of silently ignoring invalid encodings. If we don't signal the invalid encoding in any way to the user, this can subtly hide bugs. For example, this could hide typos or silently pass invalid user input to the respective functions. Forcing developers to handle this error explicitly will result in more robust code in the end. The second BC break is caused by throwing a <php>ValueError</php> on invalid encodings instead of silently ignoring invalid encodings. If we don't signal the invalid encoding in any way to the user, this can subtly hide bugs. For example, this could hide typos or silently pass invalid user input to the respective functions. Forcing developers to handle this error explicitly will result in more robust code in the end.
  
-The third BC break is the promotion of the NULL-byte warning to a <php>ValueError</php>. This makes the <php>XMLReader</php> and <php>XMLWriter</php> class more consistent with other extensions that throw instead of issuing a warning. The migration for developers should be quite simple: instead of silencing the warning and/or checking the return value of the function, they should use a try-catch construct to handle the error.+The third BC break is the promotion of the NUL-byte warning to a <php>ValueError</php>. This makes the <php>XMLReader</php> and <php>XMLWriter</php> class more consistent with other extensions that throw instead of issuing a warning. The migration for developers should be quite simple: instead of silencing the warning and/or checking the return value of the function, they should use a try-catch construct to handle the error. 
 + 
 +===== Example usages ===== 
 + 
 +==== Minimal XMLReader example ==== 
 + 
 +<PHP> 
 +// Could be any stream, but this is for simplicity sake 
 +$h = fopen("php://memory", "w+"); 
 +fwrite($h, "<root><!--my comment--><child/></root>"); 
 +fseek($h, 0); 
 + 
 +$reader = XMLReader::fromStream($h); 
 + 
 +while ($reader->read()) { 
 +    switch ($reader->nodeType) { 
 +        case XMLReader::ELEMENT: 
 +            echo "Element: ", $reader->name, "\n"; 
 +            break; 
 +        case XMLReader::COMMENT: 
 +            echo "Comment: ", $reader->value, "\n"; 
 +            break; 
 +    } 
 +
 +</PHP> 
 + 
 +==== Minimal XMLWriter example ==== 
 + 
 +<PHP> 
 +// Could be any stream, but this is for simplicity sake 
 +$h = fopen("php://output", "w"); 
 + 
 +$writer = XMLWriter::toStream($h); 
 + 
 +$writer->startElement("root"); 
 +$writer->writeAttribute("align", "left"); 
 +$writer->writeComment("hello"); 
 +$writer->endElement(); 
 +$amount = $writer->flush(); 
 +echo "\nAmount of bytes written: "; 
 +var_dump($amount); 
 +</PHP>
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 69: Line 130:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-One primary vote requiring 2/3rd majority to accept the RFC as a whole.+Two primary votes each requiring 2/3rd majority: one for the main proposal and one for the consistency proposal.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Links to any external patches and tests go here. 
  
-If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. +Implementation PR: https://github.com/php/php-src/pull/14030
- +
-Make it clear if the patch is intended to be the final patch, or is just a prototype. +
- +
-For changes affecting the core language, you should also provide a patch for the language specification.+
  
 ===== Implementation ===== ===== Implementation =====
Line 96: Line 152:
 None yet. None yet.
  
 +===== Changelog =====
 +
 +  - 0.11.0: Incorporate feedback about static methods
 +  - 0.10.1: Language fixes
 +  - 0.10.0: Static again
 +  - 0.9.2: Add example usages of the new APIs.
 +  - 0.9.1: Made XMLReader::openStream() non-static instead such that it works with overridden classes.
 +  - 0.9: Initial version under discussion
rfc/xmlreader_writer_streams.1713721835.txt.gz · Last modified: 2024/04/21 17:50 by nielsdos