1 /*
2  * Copyright 2016-2017 MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <php.h>
18 #include <Zend/zend_interfaces.h>
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "phongo_compat.h"
25 #include "php_phongo.h"
26 
27 zend_class_entry* php_phongo_commandsucceededevent_ce;
28 
29 /* {{{ proto string CommandSucceededEvent::getCommandName()
30    Returns the command name for this event */
PHP_METHOD(CommandSucceededEvent,getCommandName)31 PHP_METHOD(CommandSucceededEvent, getCommandName)
32 {
33 	zend_error_handling                 error_handling;
34 	php_phongo_commandsucceededevent_t* intern;
35 
36 	intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
37 
38 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
39 	if (zend_parse_parameters_none() == FAILURE) {
40 		zend_restore_error_handling(&error_handling);
41 		return;
42 	}
43 	zend_restore_error_handling(&error_handling);
44 
45 	RETVAL_STRING(intern->command_name);
46 } /* }}} */
47 
48 /* {{{ proto int CommandSucceededEvent::getDurationMicros()
49    Returns the event's duration in microseconds */
PHP_METHOD(CommandSucceededEvent,getDurationMicros)50 PHP_METHOD(CommandSucceededEvent, getDurationMicros)
51 {
52 	zend_error_handling                 error_handling;
53 	php_phongo_commandsucceededevent_t* intern;
54 
55 	intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
56 
57 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
58 	if (zend_parse_parameters_none() == FAILURE) {
59 		zend_restore_error_handling(&error_handling);
60 		return;
61 	}
62 	zend_restore_error_handling(&error_handling);
63 
64 	RETURN_LONG(intern->duration_micros);
65 } /* }}} */
66 
67 /* {{{ proto string CommandSucceededEvent::getOperationId()
68    Returns the event's operation ID */
PHP_METHOD(CommandSucceededEvent,getOperationId)69 PHP_METHOD(CommandSucceededEvent, getOperationId)
70 {
71 	zend_error_handling                 error_handling;
72 	php_phongo_commandsucceededevent_t* intern;
73 	char                                int_as_string[20];
74 
75 	intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
76 
77 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
78 	if (zend_parse_parameters_none() == FAILURE) {
79 		zend_restore_error_handling(&error_handling);
80 		return;
81 	}
82 	zend_restore_error_handling(&error_handling);
83 
84 	sprintf(int_as_string, "%" PRIu64, intern->operation_id);
85 	RETVAL_STRING(int_as_string);
86 } /* }}} */
87 
88 /* {{{ proto stdClass CommandSucceededEvent::getReply()
89    Returns the reply document associated with the event */
PHP_METHOD(CommandSucceededEvent,getReply)90 PHP_METHOD(CommandSucceededEvent, getReply)
91 {
92 	zend_error_handling                 error_handling;
93 	php_phongo_commandsucceededevent_t* intern;
94 	php_phongo_bson_state               state;
95 
96 	PHONGO_BSON_INIT_STATE(state);
97 
98 	intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
99 
100 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
101 	if (zend_parse_parameters_none() == FAILURE) {
102 		zend_restore_error_handling(&error_handling);
103 		return;
104 	}
105 	zend_restore_error_handling(&error_handling);
106 
107 	if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) {
108 		zval_ptr_dtor(&state.zchild);
109 		return;
110 	}
111 
112 	RETURN_ZVAL(&state.zchild, 0, 1);
113 } /* }}} */
114 
115 /* {{{ proto string CommandsucceededEvent::getRequestId()
116    Returns the event's request ID */
PHP_METHOD(CommandSucceededEvent,getRequestId)117 PHP_METHOD(CommandSucceededEvent, getRequestId)
118 {
119 	zend_error_handling                 error_handling;
120 	php_phongo_commandsucceededevent_t* intern;
121 	char                                int_as_string[20];
122 
123 	intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
124 
125 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
126 	if (zend_parse_parameters_none() == FAILURE) {
127 		zend_restore_error_handling(&error_handling);
128 		return;
129 	}
130 	zend_restore_error_handling(&error_handling);
131 
132 	sprintf(int_as_string, "%" PRIu64, intern->request_id);
133 	RETVAL_STRING(int_as_string);
134 } /* }}} */
135 
136 /* {{{ proto MongoDB\Driver\Server CommandSucceededEvent::getServer()
137    Returns the Server from which the event originated */
PHP_METHOD(CommandSucceededEvent,getServer)138 PHP_METHOD(CommandSucceededEvent, getServer)
139 {
140 	zend_error_handling                 error_handling;
141 	php_phongo_commandsucceededevent_t* intern;
142 
143 	intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
144 
145 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
146 	if (zend_parse_parameters_none() == FAILURE) {
147 		zend_restore_error_handling(&error_handling);
148 		return;
149 	}
150 	zend_restore_error_handling(&error_handling);
151 
152 	phongo_server_init(return_value, intern->client, intern->server_id);
153 } /* }}} */
154 
155 /**
156  * Event thrown when a command has succeeded to execute.
157  *
158  * This class is only constructed internally.
159  */
160 
161 /* {{{ MongoDB\Driver\Monitoring\CommandSucceededEvent function entries */
162 ZEND_BEGIN_ARG_INFO_EX(ai_CommandSucceededEvent_void, 0, 0, 0)
163 ZEND_END_ARG_INFO()
164 
165 static zend_function_entry php_phongo_commandsucceededevent_me[] = {
166 	/* clang-format off */
167 	ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_CommandSucceededEvent_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
168 	PHP_ME(CommandSucceededEvent, getCommandName, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
169 	PHP_ME(CommandSucceededEvent, getDurationMicros, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
170 	PHP_ME(CommandSucceededEvent, getOperationId, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
171 	PHP_ME(CommandSucceededEvent, getReply, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
172 	PHP_ME(CommandSucceededEvent, getRequestId, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
173 	PHP_ME(CommandSucceededEvent, getServer, ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
174 	ZEND_NAMED_ME(__wakeup, PHP_FN(MongoDB_disabled___wakeup), ai_CommandSucceededEvent_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
175 	PHP_FE_END
176 	/* clang-format on */
177 };
178 /* }}} */
179 
180 /* {{{ MongoDB\Driver\Monitoring\CommandSucceededEvent object handlers */
181 static zend_object_handlers php_phongo_handler_commandsucceededevent;
182 
php_phongo_commandsucceededevent_free_object(zend_object * object)183 static void php_phongo_commandsucceededevent_free_object(zend_object* object) /* {{{ */
184 {
185 	php_phongo_commandsucceededevent_t* intern = Z_OBJ_COMMANDSUCCEEDEDEVENT(object);
186 
187 	zend_object_std_dtor(&intern->std);
188 
189 	if (intern->reply) {
190 		bson_destroy(intern->reply);
191 	}
192 	if (intern->command_name) {
193 		efree(intern->command_name);
194 	}
195 } /* }}} */
196 
php_phongo_commandsucceededevent_create_object(zend_class_entry * class_type)197 static zend_object* php_phongo_commandsucceededevent_create_object(zend_class_entry* class_type) /* {{{ */
198 {
199 	php_phongo_commandsucceededevent_t* intern = NULL;
200 
201 	intern = PHONGO_ALLOC_OBJECT_T(php_phongo_commandsucceededevent_t, class_type);
202 
203 	zend_object_std_init(&intern->std, class_type);
204 	object_properties_init(&intern->std, class_type);
205 
206 	intern->std.handlers = &php_phongo_handler_commandsucceededevent;
207 
208 	return &intern->std;
209 } /* }}} */
210 
php_phongo_commandsucceededevent_get_debug_info(phongo_compat_object_handler_type * object,int * is_temp)211 static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */
212 {
213 	php_phongo_commandsucceededevent_t* intern;
214 	zval                                retval = ZVAL_STATIC_INIT;
215 	char                                operation_id[20], request_id[20];
216 	php_phongo_bson_state               reply_state;
217 
218 	PHONGO_BSON_INIT_STATE(reply_state);
219 
220 	intern   = Z_OBJ_COMMANDSUCCEEDEDEVENT(PHONGO_COMPAT_GET_OBJ(object));
221 	*is_temp = 1;
222 	array_init_size(&retval, 6);
223 
224 	ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
225 	ADD_ASSOC_INT64(&retval, "durationMicros", (int64_t) intern->duration_micros);
226 
227 	sprintf(operation_id, "%" PRIu64, intern->operation_id);
228 	ADD_ASSOC_STRING(&retval, "operationId", operation_id);
229 
230 	if (php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) {
231 		zval_ptr_dtor(&reply_state.zchild);
232 		goto done;
233 	}
234 
235 	ADD_ASSOC_ZVAL(&retval, "reply", &reply_state.zchild);
236 
237 	sprintf(request_id, "%" PRIu64, intern->request_id);
238 	ADD_ASSOC_STRING(&retval, "requestId", request_id);
239 
240 	{
241 		zval server;
242 
243 		phongo_server_init(&server, intern->client, intern->server_id);
244 		ADD_ASSOC_ZVAL_EX(&retval, "server", &server);
245 	}
246 
247 done:
248 	return Z_ARRVAL(retval);
249 } /* }}} */
250 /* }}} */
251 
php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS)252 void php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS) /* {{{ */
253 {
254 	zend_class_entry ce;
255 	(void) type;
256 	(void) module_number;
257 
258 	INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver\\Monitoring", "CommandSucceededEvent", php_phongo_commandsucceededevent_me);
259 	php_phongo_commandsucceededevent_ce                = zend_register_internal_class(&ce);
260 	php_phongo_commandsucceededevent_ce->create_object = php_phongo_commandsucceededevent_create_object;
261 	PHONGO_CE_FINAL(php_phongo_commandsucceededevent_ce);
262 	PHONGO_CE_DISABLE_SERIALIZATION(php_phongo_commandsucceededevent_ce);
263 
264 	memcpy(&php_phongo_handler_commandsucceededevent, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
265 	php_phongo_handler_commandsucceededevent.get_debug_info = php_phongo_commandsucceededevent_get_debug_info;
266 	php_phongo_handler_commandsucceededevent.free_obj       = php_phongo_commandsucceededevent_free_object;
267 	php_phongo_handler_commandsucceededevent.offset         = XtOffsetOf(php_phongo_commandsucceededevent_t, std);
268 
269 	return;
270 } /* }}} */
271 
272 /*
273  * Local variables:
274  * tab-width: 4
275  * c-basic-offset: 4
276  * End:
277  * vim600: noet sw=4 ts=4 fdm=marker
278  * vim<600: noet sw=4 ts=4
279  */
280