Change XMLWriter resources to objects

While we generally prefer objects over resources for quite a while, the
procedural XMLWriter API still uses resources, although there is
already an object-oriented API which uses objects.  This dichotomy
makes no sense, slightly complicates the implementation, and doesn't
allow a stepwise migration to the object-oriented API, which might be
desired.  Thus we completely drop the XMLWriter resources in favor of
XMLWriter objects.

We consider the minor BC break acceptable for a major version, since
only explicit type checks (`is_resource()`, `gettype()` etc.) need to
be adapted.
This commit is contained in:
Christoph M. Becker 2019-09-14 18:31:59 +02:00
parent 873b54840f
commit d61bc63c4d
6 changed files with 190 additions and 395 deletions

4
NEWS
View File

@ -48,4 +48,8 @@ PHP NEWS
- XML:
. Fixed bug #76874 (xml_parser_free() should never leak memory). (Nikita)
- XMLWriter:
. Changed functions to accept/return XMKWriter objects instead of resources.
(cmb)
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

View File

@ -234,6 +234,10 @@ PHP 8.0 UPGRADE NOTES
instead the XmlParser instance is automatically destroyed if it is no longer
referenced.
- XMLWriter:
. The XMLWriter functions now accept and return, respectively, XMLWriter
objects instead of resources.
- Zlib:
. gzgetss() has been removed.

File diff suppressed because it is too large Load Diff

View File

@ -34,16 +34,10 @@ extern zend_module_entry xmlwriter_module_entry;
#include <libxml/xmlwriter.h>
#include <libxml/uri.h>
/* Resource struct, not the object :) */
typedef struct _xmlwriter_object {
xmlTextWriterPtr ptr;
xmlBufferPtr output;
} xmlwriter_object;
/* Extends zend object */
typedef struct _ze_xmlwriter_object {
xmlwriter_object *xmlwriter_ptr;
xmlTextWriterPtr ptr;
xmlBufferPtr output;
zend_object std;
} ze_xmlwriter_object;

View File

@ -1,133 +1,91 @@
<?php
/** @return resource|false */
/** @return XMLWriter|false */
function xmlwriter_open_uri(string $uri) {}
/** @return resource|false */
/** @return XMLWriter|false */
function xmlwriter_open_memory() {}
/** @param resource $xmlwriter */
function xmlwriter_set_indent($xmlwriter, bool $indent): bool {}
function xmlwriter_set_indent(XMLWriter $xmlwriter, bool $indent): bool {}
/** @param resource $xmlwriter */
function xmlwriter_set_indent_string($xmlwriter, string $indentString): bool {}
function xmlwriter_set_indent_string(XMLWriter $xmlwriter, string $indentString): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_comment($xmlwriter): bool {}
function xmlwriter_start_comment(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_comment($xmlwriter): bool {}
function xmlwriter_end_comment(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_attribute($xmlwriter, string $name): bool {}
function xmlwriter_start_attribute(XMLWriter $xmlwriter, string $name): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_attribute($xmlwriter): bool {}
function xmlwriter_end_attribute(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_attribute($xmlwriter, string $name, string $value): bool {}
function xmlwriter_write_attribute(XMLWriter $xmlwriter, string $name, string $value): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_attribute_ns($xmlwriter, string $prefix, string $name, ?string $uri): bool {}
function xmlwriter_start_attribute_ns(XMLWriter $xmlwriter, string $prefix, string $name, ?string $uri): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_attribute_ns($xmlwriter, string $prefix, string $name, ?string $uri, string $content): bool {}
function xmlwriter_write_attribute_ns(XMLWriter $xmlwriter, string $prefix, string $name, ?string $uri, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_element($xmlwriter, string $name): bool {}
function xmlwriter_start_element(XMLWriter $xmlwriter, string $name): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_element($xmlwriter): bool {}
function xmlwriter_end_element(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_full_end_element($xmlwriter): bool {}
function xmlwriter_full_end_element(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_element_ns($xmlwriter, ?string $prefix, string $name, ?string $uri): bool {}
function xmlwriter_start_element_ns(XMLWriter $xmlwriter, ?string $prefix, string $name, ?string $uri): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_element($xmlwriter, string $name, ?string $content = null): bool {}
function xmlwriter_write_element(XMLWriter $xmlwriter, string $name, ?string $content = null): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_element_ns($xmlwriter, ?string $prefix, string $name, ?string $uri, ?string $content = null): bool {}
function xmlwriter_write_element_ns(XMLWriter $xmlwriter, ?string $prefix, string $name, ?string $uri, ?string $content = null): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_pi($xmlwriter, string $target): bool {}
function xmlwriter_start_pi(XMLWriter $xmlwriter, string $target): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_pi($xmlwriter): bool {}
function xmlwriter_end_pi(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_pi($xmlwriter, string $target, string $content): bool {}
function xmlwriter_write_pi(XMLWriter $xmlwriter, string $target, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_cdata($xmlwriter): bool {}
function xmlwriter_start_cdata(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_cdata($xmlwriter): bool {}
function xmlwriter_end_cdata(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_cdata($xmlwriter, string $content): bool {}
function xmlwriter_write_cdata(XMLWriter $xmlwriter, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_text($xmlwriter, string $content): bool {}
function xmlwriter_text(XMLWriter $xmlwriter, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_raw($xmlwriter, string $content): bool {}
function xmlwriter_write_raw(XMLWriter $xmlwriter, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_document($xmlwriter, ?string $version = '1.0', ?string $encoding = null, ?string $standalone = null) {}
function xmlwriter_start_document(XMLWriter $xmlwriter, ?string $version = '1.0', ?string $encoding = null, ?string $standalone = null) {}
/** @param resource $xmlwriter */
function xmlwriter_end_document($xmlwriter): bool {}
function xmlwriter_end_document(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_comment($xmlwriter, string $content): bool {}
function xmlwriter_write_comment(XMLWriter $xmlwriter, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_dtd($xmlwriter, string $qualifiedName, ?string $publicId = null, ?string $systemId = null): bool {}
function xmlwriter_start_dtd(XMLWriter $xmlwriter, string $qualifiedName, ?string $publicId = null, ?string $systemId = null): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_dtd($xmlwriter): bool {}
function xmlwriter_end_dtd(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_dtd($xmlwriter, string $name, ?string $publicId = null, ?string $systemId = null, ?string $subset = null): bool {}
function xmlwriter_write_dtd(XMLWriter $xmlwriter, string $name, ?string $publicId = null, ?string $systemId = null, ?string $subset = null): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_dtd_element($xmlwriter, string $qualifiedName): bool {}
function xmlwriter_start_dtd_element(XMLWriter $xmlwriter, string $qualifiedName): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_dtd_element($xmlwriter): bool {}
function xmlwriter_end_dtd_element(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_dtd_element($xmlwriter, string $name, string $content): bool {}
function xmlwriter_write_dtd_element(XMLWriter $xmlwriter, string $name, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_dtd_attlist($xmlwriter, string $name): bool {}
function xmlwriter_start_dtd_attlist(XMLWriter $xmlwriter, string $name): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_dtd_attlist($xmlwriter): bool {}
function xmlwriter_end_dtd_attlist(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_dtd_attlist($xmlwriter, string $name, string $content): bool {}
function xmlwriter_write_dtd_attlist(XMLWriter $xmlwriter, string $name, string $content): bool {}
/** @param resource $xmlwriter */
function xmlwriter_start_dtd_entity($xmlwriter, string $name, bool $isparam): bool {}
function xmlwriter_start_dtd_entity(XMLWriter $xmlwriter, string $name, bool $isparam): bool {}
/** @param resource $xmlwriter */
function xmlwriter_end_dtd_entity($xmlwriter): bool {}
function xmlwriter_end_dtd_entity(XMLWriter $xmlwriter): bool {}
/** @param resource $xmlwriter */
function xmlwriter_write_dtd_entity($xmlwriter, string $name, string $content, bool $isparam, string $publicId = UNKNOWN, string $systemId = UNKNOWN, string $ndataid = UNKNOWN): bool {}
function xmlwriter_write_dtd_entity(XMLWriter $xmlwriter, string $name, string $content, bool $isparam, string $publicId = UNKNOWN, string $systemId = UNKNOWN, string $ndataid = UNKNOWN): bool {}
/** @param resource $xmlwriter */
function xmlwriter_output_memory($xmlwriter, bool $flush = true): string {}
function xmlwriter_output_memory(XMLWriter $xmlwriter, bool $flush = true): string {}
/**
* @param resource $xmlwriter
* @return string|int
*/
function xmlwriter_flush($xmlwriter, bool $empty = true) {}
/** @return string|int */
function xmlwriter_flush(XMLWriter $xmlwriter, bool $empty = true) {}
class XMLWriter
{

View File

@ -8,43 +8,43 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_open_memory, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_set_indent, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, indent, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_set_indent_string, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, indentString, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_comment, 0, 1, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_comment arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_attribute, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_attribute arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_attribute, 0, 3, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_attribute_ns, 0, 4, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_attribute_ns, 0, 5, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 1)
@ -58,20 +58,20 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_full_end_element arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_element_ns, 0, 4, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_element, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_element_ns, 0, 4, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, uri, IS_STRING, 1)
@ -79,14 +79,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_element_ns, 0, 4
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_pi, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, target, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_pi arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_pi, 0, 3, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, target, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
ZEND_END_ARG_INFO()
@ -96,7 +96,7 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_cdata arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_cdata, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
ZEND_END_ARG_INFO()
@ -105,7 +105,7 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_write_raw arginfo_xmlwriter_write_cdata
ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_document, 0, 0, 1)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, version, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, standalone, IS_STRING, 1)
@ -116,7 +116,7 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_write_comment arginfo_xmlwriter_write_cdata
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_dtd, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, qualifiedName, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, publicId, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, systemId, IS_STRING, 1)
@ -125,7 +125,7 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_dtd arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_dtd, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, publicId, IS_STRING, 1)
ZEND_ARG_TYPE_INFO(0, systemId, IS_STRING, 1)
@ -133,14 +133,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_dtd, 0, 2, _IS_B
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_dtd_element, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, qualifiedName, IS_STRING, 0)
ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_dtd_element arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_dtd_element, 0, 3, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
ZEND_END_ARG_INFO()
@ -152,7 +152,7 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_write_dtd_attlist arginfo_xmlwriter_write_dtd_element
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_start_dtd_entity, 0, 3, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, isparam, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
@ -160,7 +160,7 @@ ZEND_END_ARG_INFO()
#define arginfo_xmlwriter_end_dtd_entity arginfo_xmlwriter_start_comment
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_dtd_entity, 0, 4, _IS_BOOL, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, content, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, isparam, _IS_BOOL, 0)
@ -170,12 +170,12 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_write_dtd_entity, 0, 4
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_xmlwriter_output_memory, 0, 1, IS_STRING, 0)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, flush, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_flush, 0, 0, 1)
ZEND_ARG_INFO(0, xmlwriter)
ZEND_ARG_OBJ_INFO(0, xmlwriter, XMLWriter, 0)
ZEND_ARG_TYPE_INFO(0, empty, _IS_BOOL, 0)
ZEND_END_ARG_INFO()