@@ -53,45 +53,28 @@ static void php_phongo_cursor_free_current(php_phongo_cursor_t* cursor) /* {{{ *
53
53
} /* }}} */
54
54
55
55
/* {{{ MongoDB\Driver\Cursor iterator handlers */
56
- static void php_phongo_cursor_iterator_dtor ( zend_object_iterator * iter ) /* {{{ */
56
+ static int php_phongo_cursor_valid ( php_phongo_cursor_t * cursor ) /* {{{ */
57
57
{
58
- php_phongo_cursor_iterator * cursor_it = (php_phongo_cursor_iterator * ) iter ;
59
-
60
- if (!Z_ISUNDEF (cursor_it -> intern .data )) {
61
- zval_ptr_dtor (& cursor_it -> intern .data );
62
- }
63
- } /* }}} */
64
-
65
- static int php_phongo_cursor_iterator_valid (zend_object_iterator * iter ) /* {{{ */
66
- {
67
- php_phongo_cursor_t * cursor = ((php_phongo_cursor_iterator * ) iter )-> cursor ;
68
-
69
58
if (!Z_ISUNDEF (cursor -> visitor_data .zchild )) {
70
59
return SUCCESS ;
71
60
}
72
61
73
62
return FAILURE ;
74
63
} /* }}} */
75
64
76
- static void php_phongo_cursor_iterator_get_current_key ( zend_object_iterator * iter , zval * key ) /* {{{ */
65
+ static void php_phongo_cursor_get_current_key ( php_phongo_cursor_t * cursor , zval * key ) /* {{{ */
77
66
{
78
- php_phongo_cursor_t * cursor = ((php_phongo_cursor_iterator * ) iter )-> cursor ;
79
-
80
67
ZVAL_LONG (key , cursor -> current );
81
68
} /* }}} */
82
69
83
- static zval * php_phongo_cursor_iterator_get_current_data ( zend_object_iterator * iter ) /* {{{ */
70
+ static zval * php_phongo_cursor_get_current_data ( php_phongo_cursor_t * cursor ) /* {{{ */
84
71
{
85
- php_phongo_cursor_t * cursor = ((php_phongo_cursor_iterator * ) iter )-> cursor ;
86
-
87
72
return & cursor -> visitor_data .zchild ;
88
73
} /* }}} */
89
74
90
- static void php_phongo_cursor_iterator_move_forward ( zend_object_iterator * iter ) /* {{{ */
75
+ static void php_phongo_cursor_move_forward ( php_phongo_cursor_t * cursor ) /* {{{ */
91
76
{
92
- php_phongo_cursor_iterator * cursor_it = (php_phongo_cursor_iterator * ) iter ;
93
- php_phongo_cursor_t * cursor = cursor_it -> cursor ;
94
- const bson_t * doc ;
77
+ const bson_t * doc ;
95
78
96
79
php_phongo_cursor_free_current (cursor );
97
80
@@ -124,11 +107,9 @@ static void php_phongo_cursor_iterator_move_forward(zend_object_iterator* iter)
124
107
php_phongo_cursor_free_session_if_exhausted (cursor );
125
108
} /* }}} */
126
109
127
- static void php_phongo_cursor_iterator_rewind ( zend_object_iterator * iter ) /* {{{ */
110
+ static void php_phongo_cursor_rewind ( php_phongo_cursor_t * cursor ) /* {{{ */
128
111
{
129
- php_phongo_cursor_iterator * cursor_it = (php_phongo_cursor_iterator * ) iter ;
130
- php_phongo_cursor_t * cursor = cursor_it -> cursor ;
131
- const bson_t * doc ;
112
+ const bson_t * doc ;
132
113
133
114
/* If the cursor was never advanced (e.g. command cursor), do so now */
134
115
if (!cursor -> advanced ) {
@@ -159,45 +140,6 @@ static void php_phongo_cursor_iterator_rewind(zend_object_iterator* iter) /* {{{
159
140
160
141
php_phongo_cursor_free_session_if_exhausted (cursor );
161
142
} /* }}} */
162
-
163
- static zend_object_iterator_funcs php_phongo_cursor_iterator_funcs = {
164
- php_phongo_cursor_iterator_dtor ,
165
- php_phongo_cursor_iterator_valid ,
166
- php_phongo_cursor_iterator_get_current_data ,
167
- php_phongo_cursor_iterator_get_current_key ,
168
- php_phongo_cursor_iterator_move_forward ,
169
- php_phongo_cursor_iterator_rewind ,
170
- NULL /* invalidate_current is not used */
171
- };
172
-
173
- static zend_object_iterator * php_phongo_cursor_get_iterator (zend_class_entry * ce , zval * object , int by_ref ) /* {{{ */
174
- {
175
- php_phongo_cursor_iterator * cursor_it = NULL ;
176
- php_phongo_cursor_t * cursor = Z_CURSOR_OBJ_P (object );
177
-
178
- if (by_ref ) {
179
- zend_error (E_ERROR , "An iterator cannot be used with foreach by reference" );
180
- }
181
-
182
- if (cursor -> got_iterator ) {
183
- phongo_throw_exception (PHONGO_ERROR_LOGIC , "Cursors cannot yield multiple iterators" );
184
- return NULL ;
185
- }
186
-
187
- cursor -> got_iterator = true;
188
-
189
- cursor_it = ecalloc (1 , sizeof (php_phongo_cursor_iterator ));
190
- zend_iterator_init (& cursor_it -> intern );
191
-
192
- ZVAL_COPY (& cursor_it -> intern .data , object );
193
- cursor_it -> intern .funcs = & php_phongo_cursor_iterator_funcs ;
194
- cursor_it -> cursor = cursor ;
195
- /* cursor_it->current should already be allocated to zero */
196
-
197
- php_phongo_cursor_free_current (cursor_it -> cursor );
198
-
199
- return & cursor_it -> intern ;
200
- } /* }}} */
201
143
/* }}} */
202
144
203
145
/* {{{ proto void MongoDB\Driver\Cursor::setTypeMap(array $typemap)
@@ -354,10 +296,11 @@ static PHP_METHOD(Cursor, isDead)
354
296
RETURN_BOOL (!mongoc_cursor_more (intern -> cursor ));
355
297
} /* }}} */
356
298
357
- #if PHP_VERSION_ID >= 80000
358
- static PHP_METHOD (Cursor , getIterator )
299
+ PHP_METHOD (Cursor , current )
359
300
{
360
- zend_error_handling error_handling ;
301
+ zend_error_handling error_handling ;
302
+ php_phongo_cursor_t * intern = Z_CURSOR_OBJ_P (getThis ());
303
+ zval * data ;
361
304
362
305
zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling );
363
306
if (zend_parse_parameters_none () == FAILURE ) {
@@ -366,9 +309,72 @@ static PHP_METHOD(Cursor, getIterator)
366
309
}
367
310
zend_restore_error_handling (& error_handling );
368
311
369
- zend_create_internal_iterator_zval (return_value , getThis ());
312
+ data = php_phongo_cursor_get_current_data (intern );
313
+
314
+ if (data ) {
315
+ ZVAL_COPY_DEREF (return_value , data );
316
+ }
317
+ }
318
+
319
+ PHP_METHOD (Cursor , key )
320
+ {
321
+ zend_error_handling error_handling ;
322
+ php_phongo_cursor_t * intern = Z_CURSOR_OBJ_P (getThis ());
323
+
324
+ zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling );
325
+ if (zend_parse_parameters_none () == FAILURE ) {
326
+ zend_restore_error_handling (& error_handling );
327
+ return ;
328
+ }
329
+ zend_restore_error_handling (& error_handling );
330
+
331
+ php_phongo_cursor_get_current_key (intern , return_value );
332
+ }
333
+
334
+ PHP_METHOD (Cursor , next )
335
+ {
336
+ zend_error_handling error_handling ;
337
+ php_phongo_cursor_t * intern = Z_CURSOR_OBJ_P (getThis ());
338
+
339
+ zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling );
340
+ if (zend_parse_parameters_none () == FAILURE ) {
341
+ zend_restore_error_handling (& error_handling );
342
+ return ;
343
+ }
344
+ zend_restore_error_handling (& error_handling );
345
+
346
+ php_phongo_cursor_move_forward (intern );
347
+ }
348
+
349
+ PHP_METHOD (Cursor , valid )
350
+ {
351
+ zend_error_handling error_handling ;
352
+ php_phongo_cursor_t * intern = Z_CURSOR_OBJ_P (getThis ());
353
+
354
+ zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling );
355
+ if (zend_parse_parameters_none () == FAILURE ) {
356
+ zend_restore_error_handling (& error_handling );
357
+ return ;
358
+ }
359
+ zend_restore_error_handling (& error_handling );
360
+
361
+ RETURN_BOOL (php_phongo_cursor_valid (intern ) == SUCCESS );
362
+ }
363
+
364
+ PHP_METHOD (Cursor , rewind )
365
+ {
366
+ zend_error_handling error_handling ;
367
+ php_phongo_cursor_t * intern = Z_CURSOR_OBJ_P (getThis ());
368
+
369
+ zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling );
370
+ if (zend_parse_parameters_none () == FAILURE ) {
371
+ zend_restore_error_handling (& error_handling );
372
+ return ;
373
+ }
374
+ zend_restore_error_handling (& error_handling );
375
+
376
+ php_phongo_cursor_rewind (intern );
370
377
}
371
- #endif
372
378
373
379
/* {{{ MongoDB\Driver\Cursor function entries */
374
380
ZEND_BEGIN_ARG_INFO_EX (ai_Cursor_setTypeMap , 0 , 0 , 1 )
@@ -385,9 +391,13 @@ static zend_function_entry php_phongo_cursor_me[] = {
385
391
PHP_ME (Cursor , getId , ai_Cursor_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
386
392
PHP_ME (Cursor , getServer , ai_Cursor_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
387
393
PHP_ME (Cursor , isDead , ai_Cursor_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
388
- #if PHP_VERSION_ID >= 80000
389
- PHP_ME (Cursor , getIterator , ai_Cursor_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
390
- #endif
394
+
395
+ PHP_ME (Cursor , current , ai_Cursor_void , ZEND_ACC_PUBLIC )
396
+ PHP_ME (Cursor , key , ai_Cursor_void , ZEND_ACC_PUBLIC )
397
+ PHP_ME (Cursor , next , ai_Cursor_void , ZEND_ACC_PUBLIC )
398
+ PHP_ME (Cursor , valid , ai_Cursor_void , ZEND_ACC_PUBLIC )
399
+ PHP_ME (Cursor , rewind , ai_Cursor_void , ZEND_ACC_PUBLIC )
400
+
391
401
ZEND_NAMED_ME (__construct , PHP_FN (MongoDB_disabled___construct ), ai_Cursor_void , ZEND_ACC_PRIVATE | ZEND_ACC_FINAL )
392
402
ZEND_NAMED_ME (__wakeup , PHP_FN (MongoDB_disabled___wakeup ), ai_Cursor_void , ZEND_ACC_PUBLIC | ZEND_ACC_FINAL )
393
403
PHP_FE_END
@@ -540,11 +550,8 @@ void php_phongo_cursor_init_ce(INIT_FUNC_ARGS) /* {{{ */
540
550
php_phongo_cursor_ce -> create_object = php_phongo_cursor_create_object ;
541
551
PHONGO_CE_FINAL (php_phongo_cursor_ce );
542
552
PHONGO_CE_DISABLE_SERIALIZATION (php_phongo_cursor_ce );
543
- php_phongo_cursor_ce -> get_iterator = php_phongo_cursor_get_iterator ;
544
553
545
- #if PHP_VERSION_ID >= 80000
546
- zend_class_implements (php_phongo_cursor_ce , 1 , zend_ce_aggregate );
547
- #endif
554
+ zend_class_implements (php_phongo_cursor_ce , 1 , zend_ce_iterator );
548
555
zend_class_implements (php_phongo_cursor_ce , 1 , php_phongo_cursor_interface_ce );
549
556
550
557
memcpy (& php_phongo_handler_cursor , phongo_get_std_object_handlers (), sizeof (zend_object_handlers ));
0 commit comments