]> granicus.if.org Git - php/commitdiff
Add class SimpleXMLIterator
authorMarcus Boerger <helly@php.net>
Sun, 18 Jan 2004 15:33:38 +0000 (15:33 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 18 Jan 2004 15:33:38 +0000 (15:33 +0000)
ext/spl/config.m4
ext/spl/php_spl.c
ext/spl/spl_directory.h
ext/spl/spl_sxe.c [new file with mode: 0755]
ext/spl/spl_sxe.h [new file with mode: 0755]
ext/spl/tests/sxe_001.phpt [new file with mode: 0755]
ext/spl/tests/sxe_002.phpt [new file with mode: 0755]

index 95bffc35c18636d70b04fb7e7163f287b6b48fc8..1cff311ccedca7a749e369ab11602d583d88e6c7 100755 (executable)
@@ -6,5 +6,5 @@ PHP_ARG_ENABLE(spl, enable SPL suppport,
 
 if test "$PHP_SPL" != "no"; then
     AC_DEFINE(HAVE_SPL, 1, [Whether you want SPL (Standard Php Library) support]) 
-    PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c, $ext_shared)
+    PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_sxe.c, $ext_shared)
 fi
index 45d87e3d7773e66623f56341a238fdcd0c9ae205..ef0b533cd0d1806992bbeaf86200f3bf29ad3058 100755 (executable)
@@ -29,6 +29,7 @@
 #include "spl_array.h"
 #include "spl_directory.h"
 #include "spl_iterators.h"
+#include "spl_sxe.h"
 
 #ifdef COMPILE_DL_SPL
 ZEND_GET_MODULE(spl)
@@ -85,6 +86,7 @@ PHP_MINIT_FUNCTION(spl)
        PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
        PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
        PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
+       PHP_MINIT(spl_sxe)(INIT_FUNC_ARGS_PASSTHRU);
 
        return SUCCESS;
 }
@@ -175,9 +177,13 @@ PHP_FUNCTION(spl_classes)
        SPL_ADD_CLASS(FilterIterator);
        SPL_ADD_CLASS(LimitIterator);
        SPL_ADD_CLASS(ParentIterator);
+       SPL_ADD_CLASS(RecursiveDirectoryIterator);
        SPL_ADD_CLASS(RecursiveIterator);
        SPL_ADD_CLASS(RecursiveIteratorIterator);
        SPL_ADD_CLASS(SeekableIterator);
+       if (spl_ce_SimpleXMLIterator) {
+       SPL_ADD_CLASS(SimpleXMLIterator);
+       }
 }
 /* }}} */
 
index a1320e972f6ead0fba21d9149d2c8c387f140185..05141deef4b11ac17445fb151ca85201bf1658f4 100755 (executable)
@@ -23,6 +23,7 @@
 #include "php_spl.h"
 
 extern zend_class_entry *spl_ce_DirectoryIterator;
+extern zend_class_entry *spl_ce_RecursiveDirectoryIterator;
 
 PHP_MINIT_FUNCTION(spl_directory);
 
diff --git a/ext/spl/spl_sxe.c b/ext/spl/spl_sxe.c
new file mode 100755 (executable)
index 0000000..0e22f80
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2003 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.0 of the PHP license,       |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.php.net/license/3_0.txt.                                  |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Authors: Marcus Boerger <helly@php.net>                              |
+   +----------------------------------------------------------------------+
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "php.h"
+#include "php_ini.h"
+#include "ext/standard/info.h"
+#include "zend_interfaces.h"
+
+#include "php_spl.h"
+#include "spl_functions.h"
+#include "spl_engine.h"
+#include "spl_iterators.h"
+#include "spl_sxe.h"
+
+zend_class_entry *spl_ce_SimpleXMLIterator = NULL;
+
+#if HAVE_LIBXML && HAVE_SIMPLEXML
+
+#include "ext/simplexml/simplexml.h"
+
+SPL_METHOD(SimpleXMLIterator, rewind) /* {{{ */
+{
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       php_sxe_reset_iterator(sxe TSRMLS_CC);
+}
+/* }}} */
+
+SPL_METHOD(SimpleXMLIterator, hasMore) /* {{{ */
+{
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       RETURN_BOOL(sxe->iter.data);
+}
+/* }}} */
+
+SPL_METHOD(SimpleXMLIterator, current) /* {{{ */
+{
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       RETURN_ZVAL(sxe->iter.data, 1, 0);
+}
+/* }}} */
+
+SPL_METHOD(SimpleXMLIterator, key) /* {{{ */
+{
+       xmlNodePtr curnode;
+
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       if (sxe->node != NULL) {
+               curnode = (xmlNodePtr)((php_libxml_node_ptr *)sxe->node)->node;
+               RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name), 1);
+       }
+    
+    RETURN_FALSE;
+}
+/* }}} */
+
+SPL_METHOD(SimpleXMLIterator, next) /* {{{ */
+{
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       php_sxe_move_forward_iterator(sxe TSRMLS_CC);
+}
+/* }}} */
+
+/* {{{ hasChildren()
+ */ 
+SPL_METHOD(SimpleXMLIterator, hasChildren)
+{
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+       php_sxe_object *child = php_sxe_fetch_object(sxe->iter.data TSRMLS_CC);
+       xmlNodePtr      node;
+
+       GET_NODE(child, node);
+       if (node) {
+               node = node->children;
+       }
+       while (node && node->type != XML_ELEMENT_NODE) {
+               node = node->next;
+       }
+       RETURN_BOOL(node ? 1 : 0);
+}
+/* }}} */
+
+/* {{{ getChildren()
+ */ 
+SPL_METHOD(SimpleXMLIterator, getChildren)
+{
+       php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       return_value->type = IS_OBJECT;
+       return_value->value.obj = zend_objects_store_clone_obj(sxe->iter.data TSRMLS_CC);
+}
+
+static zend_function_entry spl_funcs_SimpleXMLIterator[] = {
+       SPL_ME(SimpleXMLIterator, rewind,                 NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, hasMore,                NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, current,                NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, key,                    NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, next,                   NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, hasChildren,            NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(SimpleXMLIterator, getChildren,            NULL, ZEND_ACC_PUBLIC)
+       {NULL, NULL, NULL}
+};
+/* }}} */
+
+#define SimpleXML_Element sxe_get_element_class_entry()
+
+ZEND_API PHP_MINIT_FUNCTION(spl_sxe) /* {{{ */
+{
+       zend_class_entry *spl_ce_SimpleXML_Element = sxe_get_element_class_entry();
+
+       if (!sxe_get_element_class_entry()) {
+               return SUCCESS; /* SimpleXML must be initialized before */
+       }
+
+       REGISTER_SPL_SUB_CLASS_EX(SimpleXMLIterator, SimpleXML_Element, sxe_object_new, spl_funcs_SimpleXMLIterator);
+       REGISTER_SPL_IMPLEMENTS(SimpleXMLIterator, RecursiveIterator);
+
+       return SUCCESS;
+}
+/* }}} */
+
+#else /* HAVE_LIBXML && HAVE_SIMPLEXML */
+
+ZEND_API PHP_MINIT_FUNCTION(spl_sxe) /* {{{ */
+{
+       return SUCCESS;
+}
+
+#endif /* HAVE_LIBXML && HAVE_SIMPLEXML */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: fdm=marker
+ * vim: noet sw=4 ts=4
+ */
diff --git a/ext/spl/spl_sxe.h b/ext/spl/spl_sxe.h
new file mode 100755 (executable)
index 0000000..5310b46
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+   +----------------------------------------------------------------------+
+   | PHP Version 5                                                        |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1997-2003 The PHP Group                                |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 3.0 of the PHP license,       |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.php.net/license/3_0.txt.                                  |
+   | If you did not receive a copy of the PHP license and are unable to   |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@php.net so we can mail you a copy immediately.               |
+   +----------------------------------------------------------------------+
+   | Authors: Marcus Boerger <helly@php.net>                              |
+   +----------------------------------------------------------------------+
+ */
+
+#ifndef SPL_SXE_H
+#define SPL_SXE_H
+
+#include "php.h"
+#include "php_spl.h"
+
+extern zend_class_entry *spl_ce_SimpleXMLIterator;
+
+ZEND_API PHP_MINIT_FUNCTION(spl_sxe);
+
+#endif /* SPL_SXE_H */
+
+/*
+ * Local Variables:
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ * vim600: fdm=marker
+ * vim: noet sw=4 ts=4
+ */
diff --git a/ext/spl/tests/sxe_001.phpt b/ext/spl/tests/sxe_001.phpt
new file mode 100755 (executable)
index 0000000..88f83f2
--- /dev/null
@@ -0,0 +1,60 @@
+--TEST--
+SPL: SimpleXMLIterator
+--SKIPIF--
+<?php if (!extension_loaded("spl")) print "skip"; ?>
+<?php if (!extension_loaded("simplexml")) print "skip SimpleXML not present"; ?>
+--FILE--
+<?php 
+
+$xml =<<<EOF
+<?xml version='1.0'?>
+<!DOCTYPE sxe SYSTEM "notfound.dtd">
+<sxe id="elem1">
+ <elem1 attr1='first'>
+  <!-- comment -->
+  <elem2>
+   <elem3>
+    <elem4>
+     <?test processing instruction ?>
+    </elem4>
+   </elem3>
+  </elem2>
+ </elem1>
+</sxe>
+EOF;
+
+$sxe = simplexml_load_string($xml, 'SimpleXMLIterator');
+
+print_r($sxe);
+
+?>
+===DONE===
+--EXPECT--
+SimpleXMLIterator Object
+(
+    [elem1] => SimpleXMLIterator Object
+        (
+            [comment] => SimpleXMLIterator Object
+                (
+                )
+
+            [elem2] => SimpleXMLIterator Object
+                (
+                    [elem3] => SimpleXMLIterator Object
+                        (
+                            [elem4] => SimpleXMLIterator Object
+                                (
+                                    [test] => SimpleXMLIterator Object
+                                        (
+                                        )
+
+                                )
+
+                        )
+
+                )
+
+        )
+
+)
+===DONE===
diff --git a/ext/spl/tests/sxe_002.phpt b/ext/spl/tests/sxe_002.phpt
new file mode 100755 (executable)
index 0000000..b230e8c
--- /dev/null
@@ -0,0 +1,75 @@
+--TEST--
+SPL: SimpleXMLIterator and recursion
+--SKIPIF--
+<?php 
+       if (!extension_loaded('simplexml')) print 'skip';
+       if (!class_exists('RecursiveIteratorIterator')) print 'skip RecursiveIteratorIterator not available';
+?>
+--FILE--
+<?php 
+
+$xml =<<<EOF
+<?xml version='1.0'?>
+<!DOCTYPE sxe SYSTEM "notfound.dtd">
+<sxe id="elem1">
+ Plain text.
+ <elem1 attr1='first'>
+  Bla bla 1.
+  <!-- comment -->
+  <elem2>
+   Here we have some text data.
+   <elem3>
+    And here some more.
+    <elem4>
+     Wow once again.
+    </elem4>
+   </elem3>
+  </elem2>
+ </elem1>
+ <elem11 attr2='second'>
+  Bla bla 2.
+  <elem111>
+   Foo Bar
+  </elem111>
+ </elem11>
+</sxe>
+EOF;
+
+$sxe = simplexml_load_string($xml, 'SimpleXMLIterator');
+
+foreach(new RecursiveIteratorIterator($sxe, 1) as $name => $data) {
+       var_dump($name);
+       var_dump(get_class($data));
+       var_dump(trim($data));
+}
+
+echo "===DUMP===\n";
+
+var_dump(get_class($sxe));
+var_dump(trim($sxe->elem1));
+
+?>
+===DONE===
+--EXPECT--
+string(5) "elem1"
+string(17) "SimpleXMLIterator"
+string(10) "Bla bla 1."
+string(5) "elem2"
+string(17) "SimpleXMLIterator"
+string(28) "Here we have some text data."
+string(5) "elem3"
+string(17) "SimpleXMLIterator"
+string(19) "And here some more."
+string(5) "elem4"
+string(17) "SimpleXMLIterator"
+string(15) "Wow once again."
+string(6) "elem11"
+string(17) "SimpleXMLIterator"
+string(10) "Bla bla 2."
+string(7) "elem111"
+string(17) "SimpleXMLIterator"
+string(7) "Foo Bar"
+===DUMP===
+string(17) "SimpleXMLIterator"
+string(10) "Bla bla 1."
+===DONE===