Skip to content

Commit 9f5f19c

Browse files
committed
Merge pull request #840
2 parents 575d10a + afa752f commit 9f5f19c

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

php_phongo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,11 +2220,13 @@ static void php_phongo_command_failed(const mongoc_apm_command_failed_t* event)
22202220
p_event->operation_id = mongoc_apm_command_failed_get_operation_id(event);
22212221
p_event->request_id = mongoc_apm_command_failed_get_request_id(event);
22222222
p_event->duration_micros = mongoc_apm_command_failed_get_duration(event);
2223+
p_event->reply = bson_copy(mongoc_apm_command_failed_get_reply(event));
22232224

22242225
/* We need to process and convert the error right here, otherwise
22252226
* debug_info will turn into a recursive loop, and with the wrong trace
22262227
* locations */
22272228
mongoc_apm_command_failed_get_error(event, &tmp_error);
2229+
22282230
{
22292231
#if PHP_VERSION_ID < 70000
22302232
MAKE_STD_ZVAL(p_event->z_error);

php_phongo_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ typedef struct {
248248
uint64_t operation_id;
249249
uint64_t request_id;
250250
uint64_t duration_micros;
251+
bson_t* reply;
251252
PHONGO_STRUCT_ZVAL z_error;
252253
PHONGO_ZEND_OBJECT_POST
253254
} php_phongo_commandfailedevent_t;

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ PHP_METHOD(CommandFailedEvent, getOperationId)
100100
PHONGO_RETVAL_STRING(int_as_string);
101101
} /* }}} */
102102

103+
/* {{{ proto stdClass CommandFailedEvent::getReply()
104+
Returns the reply document associated with the event */
105+
PHP_METHOD(CommandFailedEvent, getReply)
106+
{
107+
php_phongo_commandfailedevent_t* intern;
108+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
109+
SUPPRESS_UNUSED_WARNING(return_value_ptr)
110+
SUPPRESS_UNUSED_WARNING(return_value_used)
111+
112+
intern = Z_COMMANDFAILEDEVENT_OBJ_P(getThis());
113+
114+
if (zend_parse_parameters_none() == FAILURE) {
115+
return;
116+
}
117+
118+
php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state);
119+
#if PHP_VERSION_ID >= 70000
120+
RETURN_ZVAL(&state.zchild, 0, 1);
121+
#else
122+
RETURN_ZVAL(state.zchild, 0, 1);
123+
#endif
124+
} /* }}} */
125+
103126
/* {{{ proto string CommandFailedEvent::getRequestId()
104127
Returns the event's request ID */
105128
PHP_METHOD(CommandFailedEvent, getRequestId)
@@ -153,6 +176,7 @@ static zend_function_entry php_phongo_commandfailedevent_me[] = {
153176
PHP_ME(CommandFailedEvent, getError, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
154177
PHP_ME(CommandFailedEvent, getDurationMicros, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
155178
PHP_ME(CommandFailedEvent, getOperationId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
179+
PHP_ME(CommandFailedEvent, getReply, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
156180
PHP_ME(CommandFailedEvent, getRequestId, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
157181
PHP_ME(CommandFailedEvent, getServer, ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
158182
ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_CommandFailedEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
@@ -174,6 +198,10 @@ static void php_phongo_commandfailedevent_free_object(phongo_free_object_arg* ob
174198
zval_ptr_dtor(&intern->z_error);
175199
}
176200

201+
if (intern->reply) {
202+
bson_destroy(intern->reply);
203+
}
204+
177205
if (intern->command_name) {
178206
efree(intern->command_name);
179207
}
@@ -212,6 +240,7 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(zval* object, int
212240
php_phongo_commandfailedevent_t* intern;
213241
zval retval = ZVAL_STATIC_INIT;
214242
char operation_id[20], request_id[20];
243+
php_phongo_bson_state reply_state = PHONGO_BSON_STATE_INITIALIZER;
215244

216245
intern = Z_COMMANDFAILEDEVENT_OBJ_P(object);
217246
*is_temp = 1;
@@ -231,6 +260,13 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(zval* object, int
231260
sprintf(operation_id, "%" PRIu64, intern->operation_id);
232261
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
233262

263+
php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state);
264+
#if PHP_VERSION_ID >= 70000
265+
ADD_ASSOC_ZVAL(&retval, "reply", &reply_state.zchild);
266+
#else
267+
ADD_ASSOC_ZVAL(&retval, "reply", reply_state.zchild);
268+
#endif
269+
234270
sprintf(request_id, "%" PRIu64, intern->request_id);
235271
ADD_ASSOC_STRING(&retval, "requestId", request_id);
236272

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
MongoDB\Driver\Monitoring\CommandFailedEvent
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_server_version('<', '3.4'); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = new MongoDB\Driver\Manager(URI);
12+
13+
class MySubscriber implements MongoDB\Driver\Monitoring\CommandSubscriber
14+
{
15+
public function commandStarted( \MongoDB\Driver\Monitoring\CommandStartedEvent $event )
16+
{
17+
echo "started: ", $event->getCommandName(), "\n";
18+
}
19+
20+
public function commandSucceeded( \MongoDB\Driver\Monitoring\CommandSucceededEvent $event )
21+
{
22+
var_dump($event);
23+
}
24+
25+
public function commandFailed( \MongoDB\Driver\Monitoring\CommandFailedEvent $event )
26+
{
27+
echo "failed: ", $event->getCommandName(), "\n";
28+
var_dump($event->getReply());
29+
}
30+
}
31+
32+
$subscriber = new MySubscriber;
33+
34+
MongoDB\Driver\Monitoring\addSubscriber( $subscriber );
35+
36+
$command = new MongoDB\Driver\Command([
37+
'findAndModify' => COLLECTION_NAME,
38+
'query' => ['_id' => 'foo'],
39+
'upsert' => true,
40+
'new' => true,
41+
]);
42+
43+
try {
44+
$manager->executeWriteCommand(DATABASE_NAME, $command);
45+
} catch (MongoDB\Driver\Exception\CommandException $e) {}
46+
47+
?>
48+
--EXPECTF--
49+
started: findAndModify
50+
failed: findAndModify
51+
object(stdClass)#%d (%d) {
52+
["ok"]=>
53+
float(0)
54+
["errmsg"]=>
55+
string(49) "Either an update or remove=true must be specified"
56+
["code"]=>
57+
int(9)
58+
["codeName"]=>
59+
string(13) "FailedToParse"%A
60+
}

tests/apm/overview.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ object(MongoDB\Driver\Monitoring\CommandFailedEvent)#%d (%d) {
9191
}
9292
["operationId"]=>
9393
string(%d) "%s"
94+
["reply"]=>
95+
object(stdClass)#%d (%d) {
96+
["ok"]=>
97+
float(0)
98+
["errmsg"]=>
99+
string(12) "ns not found"
100+
["code"]=>
101+
int(26)%A
102+
}
94103
["requestId"]=>
95104
string(%d) "%s"
96105
["server"]=>

0 commit comments

Comments
 (0)