File tree Expand file tree Collapse file tree 4 files changed +116
-1
lines changed Expand file tree Collapse file tree 4 files changed +116
-1
lines changed Original file line number Diff line number Diff line change @@ -1696,18 +1696,26 @@ zend_object_iterator_funcs php_phongo_cursor_iterator_funcs = {
1696
1696
1697
1697
zend_object_iterator * php_phongo_cursor_get_iterator (zend_class_entry * ce , zval * object , int by_ref TSRMLS_DC ) /* {{{ */
1698
1698
{
1699
+ php_phongo_cursor_t * cursor = zend_object_store_get_object (object TSRMLS_CC );
1699
1700
php_phongo_cursor_iterator * cursor_it = NULL ;
1700
1701
1701
1702
if (by_ref ) {
1702
1703
zend_error (E_ERROR , "An iterator cannot be used with foreach by reference" );
1703
1704
}
1704
1705
1706
+ if (cursor -> got_iterator ) {
1707
+ phongo_throw_exception (PHONGO_ERROR_LOGIC TSRMLS_CC , "Cursors cannot yield multiple iterators" );
1708
+ return NULL ;
1709
+ }
1710
+
1711
+ cursor -> got_iterator = 1 ;
1712
+
1705
1713
cursor_it = ecalloc (1 , sizeof (php_phongo_cursor_iterator ));
1706
1714
1707
1715
Z_ADDREF_P (object );
1708
1716
cursor_it -> intern .data = (void * )object ;
1709
1717
cursor_it -> intern .funcs = & php_phongo_cursor_iterator_funcs ;
1710
- cursor_it -> cursor = ( php_phongo_cursor_t * ) zend_object_store_get_object ( object TSRMLS_CC ) ;
1718
+ cursor_it -> cursor = cursor ;
1711
1719
/* cursor_it->current should already be allocated to zero */
1712
1720
1713
1721
php_phongo_cursor_free_current (cursor_it -> cursor );
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ typedef struct {
40
40
mongoc_client_t * client ;
41
41
int server_id ;
42
42
php_phongo_bson_state visitor_data ;
43
+ int got_iterator ;
43
44
} php_phongo_cursor_t ;
44
45
45
46
typedef struct {
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ MongoDB\Driver\Cursor get_iterator handler does not yield multiple iterators (foreach)
3
+ --SKIPIF--
4
+ <?php require __DIR__ . "/../utils/basic-skipif.inc " ; CLEANUP (STANDALONE ) ?>
5
+ --FILE--
6
+ <?php
7
+ require_once __DIR__ . "/../utils/basic.inc " ;
8
+
9
+ $ manager = new MongoDB \Driver \Manager (STANDALONE );
10
+
11
+ $ bulkWrite = new MongoDB \Driver \BulkWrite ;
12
+
13
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
14
+ $ bulkWrite ->insert (array ('_id ' => $ i ));
15
+ }
16
+
17
+ $ writeResult = $ manager ->executeBulkWrite (NS , $ bulkWrite );
18
+ printf ("Inserted: %d \n" , $ writeResult ->getInsertedCount ());
19
+
20
+ $ cursor = $ manager ->executeQuery (NS , new MongoDB \Driver \Query (array ()));
21
+
22
+ echo "\nFirst foreach statement: \n" ;
23
+
24
+ foreach ($ cursor as $ document ) {
25
+ var_dump ($ document );
26
+ }
27
+
28
+ echo "\nSecond foreach statement: \n" ;
29
+
30
+ try {
31
+ foreach ($ cursor as $ document ) {
32
+ echo "FAILED: get_iterator should not yield multiple iterators \n" ;
33
+ }
34
+ } catch (MongoDB \Driver \Exception \LogicException $ e ) {
35
+ printf ("LogicException: %s \n" , $ e ->getMessage ());
36
+ }
37
+
38
+ ?>
39
+ ===DONE===
40
+ <?php exit (0 ); ?>
41
+ --EXPECT--
42
+ Inserted: 3
43
+
44
+ First foreach statement:
45
+ array(1) {
46
+ ["_id"]=>
47
+ int(0)
48
+ }
49
+ array(1) {
50
+ ["_id"]=>
51
+ int(1)
52
+ }
53
+ array(1) {
54
+ ["_id"]=>
55
+ int(2)
56
+ }
57
+
58
+ Second foreach statement:
59
+ LogicException: Cursors cannot yield multiple iterators
60
+ ===DONE===
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ MongoDB\Driver\Cursor get_iterator handler does not yield multiple iterators (IteratorIterator)
3
+ --SKIPIF--
4
+ <?php require __DIR__ . "/../utils/basic-skipif.inc " ; CLEANUP (STANDALONE ) ?>
5
+ --FILE--
6
+ <?php
7
+ require_once __DIR__ . "/../utils/basic.inc " ;
8
+
9
+ $ manager = new MongoDB \Driver \Manager (STANDALONE );
10
+
11
+ $ bulkWrite = new MongoDB \Driver \BulkWrite ;
12
+
13
+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
14
+ $ bulkWrite ->insert (array ('_id ' => $ i ));
15
+ }
16
+
17
+ $ writeResult = $ manager ->executeBulkWrite (NS , $ bulkWrite );
18
+ printf ("Inserted: %d \n" , $ writeResult ->getInsertedCount ());
19
+
20
+ $ cursor = $ manager ->executeQuery (NS , new MongoDB \Driver \Query (array ()));
21
+
22
+ echo "\nFirst IteratorIterator wrapping: \n" ;
23
+
24
+ var_dump (new IteratorIterator ($ cursor ));
25
+
26
+ echo "\nSecond IteratorIterator wrapping: \n" ;
27
+
28
+ try {
29
+ var_dump (new IteratorIterator ($ cursor ));
30
+ } catch (MongoDB \Driver \Exception \LogicException $ e ) {
31
+ printf ("LogicException: %s \n" , $ e ->getMessage ());
32
+ }
33
+
34
+ ?>
35
+ ===DONE===
36
+ <?php exit (0 ); ?>
37
+ --EXPECTF--
38
+ Inserted: 3
39
+
40
+ First IteratorIterator wrapping:
41
+ object(IteratorIterator)#%d (0) {
42
+ }
43
+
44
+ Second IteratorIterator wrapping:
45
+ LogicException: Cursors cannot yield multiple iterators
46
+ ===DONE===
You can’t perform that action at this time.
0 commit comments