Skip to content

Commit 2d6b7d2

Browse files
committed
PHPC-2412: Deprecate CursorId class
1 parent 2cd4b43 commit 2d6b7d2

17 files changed

+126
-21
lines changed

php_phongo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ PHP_MINIT_FUNCTION(mongodb) /* {{{ */
253253
php_phongo_bulkwrite_init_ce(INIT_FUNC_ARGS_PASSTHRU);
254254
php_phongo_clientencryption_init_ce(INIT_FUNC_ARGS_PASSTHRU);
255255
php_phongo_command_init_ce(INIT_FUNC_ARGS_PASSTHRU);
256-
php_phongo_cursor_init_ce(INIT_FUNC_ARGS_PASSTHRU);
257256
php_phongo_cursorid_init_ce(INIT_FUNC_ARGS_PASSTHRU);
257+
php_phongo_cursor_init_ce(INIT_FUNC_ARGS_PASSTHRU);
258258
php_phongo_manager_init_ce(INIT_FUNC_ARGS_PASSTHRU);
259259
php_phongo_query_init_ce(INIT_FUNC_ARGS_PASSTHRU);
260260
php_phongo_readconcern_init_ce(INIT_FUNC_ARGS_PASSTHRU);

src/MongoDB/Cursor.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,22 @@ static PHP_METHOD(MongoDB_Driver_Cursor, toArray)
143143
static PHP_METHOD(MongoDB_Driver_Cursor, getId)
144144
{
145145
php_phongo_cursor_t* intern;
146+
bool asInt64;
146147

147148
intern = Z_CURSOR_OBJ_P(getThis());
148149

149-
PHONGO_PARSE_PARAMETERS_NONE();
150+
PHONGO_PARSE_PARAMETERS_START(0, 1)
151+
Z_PARAM_OPTIONAL
152+
Z_PARAM_BOOL(asInt64)
153+
PHONGO_PARSE_PARAMETERS_END();
150154

151-
php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor));
155+
if (asInt64) {
156+
phongo_int64_new(return_value, mongoc_cursor_get_id(intern->cursor));
157+
} else {
158+
php_error_docref(NULL, E_DEPRECATED, "The method \"MongoDB\\Driver\\Cursor::getId\" will no longer return a \"MongoDB\\Driver\\CursorId\" instance in the future. Pass \"true\" as argument to change to the new behavior and receive a \"MongoDB\\BSON\\Int64\" instance instead.");
159+
160+
php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor));
161+
}
152162
}
153163

154164
/* Returns the Server object to which this cursor is attached */

src/MongoDB/Cursor.stub.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public function current(): array|object|null {}
1818
public function current() {}
1919
#endif
2020

21-
final public function getId(): CursorId {}
21+
#if PHP_VERSION_ID >= 80000
22+
/** @tentative-return-type */
23+
final public function getId(bool $asInt64 = false): CursorId|\MongoDB\BSON\Int64 {}
24+
#else
25+
/** @return CursorId|\MongoDB\BSON\Int64 */
26+
final public function getId(bool $asInt64 = false) {}
27+
#endif
2228

2329
final public function getServer(): Server {}
2430

src/MongoDB/CursorId.stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace MongoDB\Driver;
99

10+
/** @deprecated deprecated without replacement */
1011
final class CursorId implements \Serializable
1112
{
1213
final private function __construct() {}

src/MongoDB/CursorId_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MongoDB/CursorInterface.stub.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
interface CursorInterface extends \Traversable
1111
{
12+
#if PHP_VERSION_ID >= 80000
1213
/** @tentative-return-type */
13-
public function getId(): CursorId;
14+
public function getId(): CursorId|\MongoDB\BSON\Int64;
15+
#else
16+
/** @return CursorId|\MongoDB\BSON\Int64 */
17+
public function getId();
18+
#endif
1419

1520
/** @tentative-return-type */
1621
public function getServer(): Server;

src/MongoDB/CursorInterface_arginfo.h

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MongoDB/Cursor_arginfo.h

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cursor/cursor-session-001.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $iterator = new IteratorIterator($cursor);
2626
$iterator->rewind();
2727
$iterator->next();
2828

29-
printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
29+
printf("Cursor ID is zero: %s\n", (int) $cursor->getId(true) === 0 ? 'yes' : 'no');
3030
var_dump($cursor);
3131

3232
$iterator->next();
@@ -35,7 +35,7 @@ $iterator->next();
3535
* is exhausted. While this is primarily done to ensure implicit sessions for
3636
* command cursors are returned to the pool ASAP, it also applies to explicit
3737
* sessions. */
38-
printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
38+
printf("\nCursor ID is zero: %s\n", (int) $cursor->getId(true) === 0 ? 'yes' : 'no');
3939
var_dump($cursor);
4040

4141
?>

tests/cursor/cursor-session-002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ $iterator->next();
2828
/* Implicit sessions for query cursors are never exposed to PHPC, as they are
2929
* handled internally by libmongoc. Cursor debug ouput should never report such
3030
* sessions. */
31-
printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
31+
printf("Cursor ID is zero: %s\n", (int) $cursor->getId(true) === 0 ? 'yes' : 'no');
3232
var_dump($cursor);
3333

3434
$iterator->next();
3535

36-
printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
36+
printf("\nCursor ID is zero: %s\n", (int) $cursor->getId(true) === 0 ? 'yes' : 'no');
3737
var_dump($cursor);
3838

3939
?>

0 commit comments

Comments
 (0)