Skip to content

Commit 2f18e42

Browse files
authored
PHPC-2699: Add typed, read-only properties for value objects and events (#1979)
* Add properties to ReadPreference class * Add properties to ReadConcern class * Add properties to WriteConcern class * Add properties to WriteError class * Add properties to WriteResult class * Add properties to CommandException and BulkWriteException classes * Add properties to CommandFailedEvent class * Add properties to CommandStartedEvent class * Add properties to CommandSucceededEvent class * Add properties to ServerChangedEvent class * Add properties to ServerClosedEvent class * Add properties to ServerHeartbeatFailedEvent class * Add properties to ServerHeartbeatStartedEvent class * Add properties to ServerHeartbeatSucceededEvent class * Add properties to ServerOpeningEvent class * Add properties to TopologyChangedEvent class * Add properties to TopologyClosedEvent class * Add properties to TopologyOpeningEvent class
1 parent 4309a99 commit 2f18e42

158 files changed

Lines changed: 3368 additions & 1300 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/MongoDB/Exception/BulkWriteException.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
class BulkWriteException extends ServerException
1111
{
12-
/** @var \MongoDB\Driver\WriteResult */
13-
protected $writeResult;
12+
public readonly \MongoDB\Driver\WriteResult $writeResult;
1413

1514
final public function getWriteResult(): \MongoDB\Driver\WriteResult {}
1615
}

src/MongoDB/Exception/BulkWriteException_arginfo.h

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

src/MongoDB/Exception/CommandException.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
class CommandException extends ServerException
1111
{
12-
/** @var object */
13-
protected $resultDocument;
12+
public readonly object $resultDocument;
1413

1514
final public function getResultDocument(): object {}
1615
}

src/MongoDB/Exception/CommandException_arginfo.h

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

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 76 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <php.h>
2020
#include <Zend/zend_interfaces.h>
21+
#include <Zend/zend_exceptions.h>
2122

2223
#include "phongo.h"
2324
#include "phongo_bson_encode.h"
@@ -160,6 +161,50 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getServerConnect
160161
RETURN_LONG(intern->server_connection_id);
161162
}
162163

164+
static void phongo_commandfailedevent_update_properties(phongo_commandfailedevent_t* intern)
165+
{
166+
char operation_id[24], request_id[24];
167+
phongo_bson_state reply_state;
168+
169+
PHONGO_BSON_INIT_STATE(reply_state);
170+
171+
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("host"), intern->host.host);
172+
zend_update_property_long(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("port"), intern->host.port);
173+
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("commandName"), intern->command_name);
174+
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("databaseName"), intern->database_name);
175+
zend_update_property_long(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("durationMicros"), intern->duration_micros);
176+
zend_update_property(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("error"), &intern->z_error);
177+
178+
if (phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
179+
zend_update_property(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("reply"), &reply_state.zchild);
180+
}
181+
zval_ptr_dtor(&reply_state.zchild);
182+
183+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
184+
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("operationId"), operation_id);
185+
186+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
187+
zend_update_property_string(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("requestId"), request_id);
188+
189+
if (intern->has_service_id) {
190+
zval service_id;
191+
192+
if (phongo_objectid_new(&service_id, &intern->service_id)) {
193+
zend_update_property(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serviceId"), &service_id);
194+
zval_ptr_dtor(&service_id);
195+
}
196+
} else {
197+
zend_update_property_null(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serviceId"));
198+
}
199+
200+
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
201+
if (intern->server_connection_id == -1) {
202+
zend_update_property_null(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serverConnectionId"));
203+
} else {
204+
zend_update_property_long(phongo_commandfailedevent_ce, &intern->std, ZEND_STRL("serverConnectionId"), intern->server_connection_id);
205+
}
206+
}
207+
163208
/* MongoDB\Driver\Monitoring\CommandFailedEvent object handlers */
164209
static zend_object_handlers phongo_handler_commandfailedevent;
165210

@@ -195,71 +240,46 @@ static zend_object* phongo_commandfailedevent_create_object(zend_class_entry* cl
195240
return &intern->std;
196241
}
197242

198-
static HashTable* phongo_commandfailedevent_get_debug_info(zend_object* object, int* is_temp)
243+
void phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS)
199244
{
200-
PHONGO_INTERN_FROM_Z_OBJ(commandfailedevent, object);
201-
202-
zval retval = ZVAL_STATIC_INIT;
203-
char operation_id[24], request_id[24];
204-
phongo_bson_state reply_state;
205-
206-
PHONGO_BSON_INIT_STATE(reply_state);
207-
208-
*is_temp = 1;
209-
array_init_size(&retval, 10);
210-
211-
ADD_ASSOC_STRING(&retval, "host", intern->host.host);
212-
ADD_ASSOC_LONG_EX(&retval, "port", intern->host.port);
213-
ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
214-
ADD_ASSOC_INT64(&retval, "durationMicros", intern->duration_micros);
245+
phongo_commandfailedevent_ce = register_class_MongoDB_Driver_Monitoring_CommandFailedEvent();
246+
phongo_commandfailedevent_ce->create_object = phongo_commandfailedevent_create_object;
215247

216-
ADD_ASSOC_ZVAL_EX(&retval, "error", &intern->z_error);
217-
Z_ADDREF(intern->z_error);
248+
memcpy(&phongo_handler_commandfailedevent, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
249+
phongo_handler_commandfailedevent.free_obj = phongo_commandfailedevent_free_object;
250+
phongo_handler_commandfailedevent.offset = XtOffsetOf(phongo_commandfailedevent_t, std);
251+
}
218252

219-
if (!phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
220-
zval_ptr_dtor(&reply_state.zchild);
221-
goto done;
222-
}
253+
void phongo_commandfailedevent_init(zval* return_value, const mongoc_apm_command_failed_t* event)
254+
{
255+
PHONGO_INTERN_INIT_EX(commandfailedevent, return_value);
223256

224-
ADD_ASSOC_ZVAL(&retval, "reply", &reply_state.zchild);
257+
bson_error_t tmp_error = { 0 };
225258

226-
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
227-
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
259+
memcpy(&intern->host, mongoc_apm_command_failed_get_host(event), sizeof(mongoc_host_list_t));
228260

229-
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
230-
ADD_ASSOC_STRING(&retval, "requestId", request_id);
261+
intern->command_name = estrdup(mongoc_apm_command_failed_get_command_name(event));
262+
intern->database_name = estrdup(mongoc_apm_command_failed_get_database_name(event));
263+
intern->server_id = mongoc_apm_command_failed_get_server_id(event);
264+
intern->operation_id = mongoc_apm_command_failed_get_operation_id(event);
265+
intern->request_id = mongoc_apm_command_failed_get_request_id(event);
266+
intern->duration_micros = mongoc_apm_command_failed_get_duration(event);
267+
intern->reply = bson_copy(mongoc_apm_command_failed_get_reply(event));
268+
intern->server_connection_id = mongoc_apm_command_failed_get_server_connection_id_int64(event);
269+
intern->has_service_id = mongoc_apm_command_failed_get_service_id(event) != NULL;
231270

232271
if (intern->has_service_id) {
233-
zval service_id;
234-
235-
if (!phongo_objectid_new(&service_id, &intern->service_id)) {
236-
/* Exception should already have been thrown */
237-
goto done;
238-
}
239-
240-
ADD_ASSOC_ZVAL_EX(&retval, "serviceId", &service_id);
241-
} else {
242-
ADD_ASSOC_NULL_EX(&retval, "serviceId");
272+
bson_oid_copy(mongoc_apm_command_failed_get_service_id(event), &intern->service_id);
243273
}
244274

245-
/* TODO: Use MONGOC_NO_SERVER_CONNECTION_ID once it is added to libmongoc's public API (CDRIVER-4176) */
246-
if (intern->server_connection_id == -1) {
247-
ADD_ASSOC_NULL_EX(&retval, "serverConnectionId");
248-
} else {
249-
ADD_ASSOC_LONG_EX(&retval, "serverConnectionId", intern->server_connection_id);
250-
}
275+
/* We need to process and convert the error right here, otherwise
276+
* debug_info will turn into a recursive loop, and with the wrong trace
277+
* locations */
278+
mongoc_apm_command_failed_get_error(event, &tmp_error);
251279

252-
done:
253-
return Z_ARRVAL(retval);
254-
}
280+
object_init_ex(&intern->z_error, phongo_exception_from_mongoc_domain(tmp_error.domain, tmp_error.code));
281+
zend_update_property_string(zend_ce_exception, Z_OBJ_P(&intern->z_error), ZEND_STRL("message"), tmp_error.message);
282+
zend_update_property_long(zend_ce_exception, Z_OBJ_P(&intern->z_error), ZEND_STRL("code"), tmp_error.code);
255283

256-
void phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS)
257-
{
258-
phongo_commandfailedevent_ce = register_class_MongoDB_Driver_Monitoring_CommandFailedEvent();
259-
phongo_commandfailedevent_ce->create_object = phongo_commandfailedevent_create_object;
260-
261-
memcpy(&phongo_handler_commandfailedevent, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
262-
phongo_handler_commandfailedevent.get_debug_info = phongo_commandfailedevent_get_debug_info;
263-
phongo_handler_commandfailedevent.free_obj = phongo_commandfailedevent_free_object;
264-
phongo_handler_commandfailedevent.offset = XtOffsetOf(phongo_commandfailedevent_t, std);
265-
}
284+
phongo_commandfailedevent_update_properties(intern);
285+
}

src/MongoDB/Monitoring/CommandFailedEvent.stub.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
/** @not-serializable */
1111
final class CommandFailedEvent
1212
{
13+
public readonly string $host;
14+
public readonly int $port;
15+
public readonly string $commandName;
16+
public readonly string $databaseName;
17+
public readonly int $durationMicros;
18+
public readonly \Exception $error;
19+
public readonly object $reply;
20+
public readonly string $operationId;
21+
public readonly string $requestId;
22+
public readonly ?\MongoDB\BSON\ObjectId $serviceId;
23+
public readonly ?int $serverConnectionId;
24+
1325
final private function __construct() {}
1426

1527
final public function getCommandName(): string {}

src/MongoDB/Monitoring/CommandFailedEvent_arginfo.h

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

0 commit comments

Comments
 (0)