forked from AOP-PHP/AOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aop_joinpoint.c
278 lines (254 loc) · 10.8 KB
/
aop_joinpoint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
/+----------------------------------------------------------------------+
| AOP |
+----------------------------------------------------------------------+
| Copyright (c) 2012 Julien Salleyron, Gérald Croës |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Julien Salleyron <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/php_string.h"
#include "ext/pcre/php_pcre.h"
#include "aop.h"
#include "aop_joinpoint.h"
PHP_METHOD(AopJoinpoint, getPropertyName){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!(obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY)) {
zend_error(E_ERROR, "getPropertyName is only available when the JoinPoint is a property operation (read or write)");
}
if (obj->member != NULL) {
RETURN_ZVAL(obj->member, 1, 0);
return;
}
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getPropertyValue){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
zval **to_return;
#if ZEND_MODULE_API_NO >= 20100525
zend_literal *key = NULL;
#endif
if (!(obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY)) {
zend_error(E_ERROR, "getPropertyValue is only available when the JoinPoint is a property operation (read or write)");
}
if (obj->object!=NULL && obj->member!=NULL) {
to_return = zend_std_get_property_ptr_ptr_overload(obj->object, obj->member AOP_KEY_C TSRMLS_CC);
}
RETURN_ZVAL (*to_return, 1, 0);
}
PHP_METHOD(AopJoinpoint, getArguments){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY) {
zend_error(E_ERROR, "getArguments is only available when the JoinPoint is a function or method call");
}
if (obj->args == NULL) {
obj->args = get_current_args(obj->ex TSRMLS_CC);
}
if (obj->args != NULL) {
RETURN_ZVAL(obj->args, 1, 0);
}
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, setArguments){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
zval *params;
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY) {
zend_error(E_ERROR, "setArguments is only available when the JoinPoint is a function or ia method call");
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", ¶ms) == FAILURE) {
zend_error(E_ERROR, "setArguments expects an array as its first argument");
return;
}
if (obj->args != NULL) {
zval_ptr_dtor(&obj->args);
}
obj->args = params;
obj->args_overloaded = 1;
Z_ADDREF_P(params);
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getKindOfAdvice){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
RETURN_LONG(obj->kind_of_advice);
}
PHP_METHOD(AopJoinpoint, getPointcut){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
RETURN_STRING(obj->current_pointcut->selector, 1);
}
PHP_METHOD(AopJoinpoint, getReturnedValue){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY) {
zend_error(E_ERROR, "getReturnedValue is not available when the JoinPoint is a property operation (read or write)");
}
if (obj->current_pointcut->kind_of_advice & AOP_KIND_BEFORE) {
zend_error(E_ERROR, "getReturnedValue is not available when the advice was added with aop_add_before");
}
if ((*obj->to_return_ptr_ptr) != NULL) {
zval_ptr_dtor (return_value_ptr);
(*return_value_ptr) = (*obj->to_return_ptr_ptr);
Z_ADDREF_P(*obj->to_return_ptr_ptr);
}
// RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getAssignedValue){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!(obj->kind_of_advice & AOP_KIND_WRITE)) {
zend_error(E_ERROR, "getAssignedValue is only available when the JoinPoint is a property write operation");
}
if (obj->value != NULL) {
zval_ptr_dtor (return_value_ptr);
*return_value_ptr = obj->value;
Z_ADDREF_P(obj->value);
} else {
RETURN_NULL();
}
}
PHP_METHOD(AopJoinpoint, setAssignedValue){
zval *ret;
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->kind_of_advice & AOP_KIND_READ) {
zend_error(E_ERROR, "setAssignedValue is not available when the JoinPoint is a property read operation");
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &ret) == FAILURE) {
zend_error(E_ERROR, "Error");
return;
}
if (obj->value!=NULL) {
zval_ptr_dtor(&obj->value);
}
obj->value = ret;
Z_ADDREF_P(ret);
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, setReturnedValue){
zval *ret;
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->kind_of_advice & AOP_KIND_WRITE) {
zend_error(E_ERROR, "setReturnedValue is not available when the JoinPoint is a property write operation");
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &ret) == FAILURE) {
zend_error(E_ERROR, "Error");
return;
}
if (!(obj->kind_of_advice & AOP_KIND_PROPERTY) && *obj->to_return_ptr_ptr!=NULL) {
zval_ptr_dtor(obj->to_return_ptr_ptr);
}
obj->value = ret;
Z_ADDREF_P(ret);
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getObject) {
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->object != NULL) {
RETURN_ZVAL(obj->object, 1, 0);
}
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getClassName){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY) {
if (obj->object != NULL) {
zend_class_entry *ce = Z_OBJCE_P(obj->object);
RETURN_STRING(ce->name, 1);
}
} else {
zend_class_entry *ce;
zend_execute_data *data = obj->ex;
zend_function *curr_func;
if (data == NULL) {
RETURN_NULL();
}
curr_func = data->function_state.function;
ce = curr_func->common.scope;
if (ce != NULL) {
RETURN_STRING(ce->name, 1);
}
}
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getFunctionName){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
zend_execute_data *data = obj->ex;
zend_function *curr_func;
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY || obj->current_pointcut->kind_of_advice & AOP_KIND_METHOD) {
zend_error(E_ERROR, "getMethodName is only available when the JoinPoint is a function call");
}
if (data == NULL) {
RETURN_NULL();
}
curr_func = data->function_state.function;
RETURN_STRING(curr_func->common.function_name, 1);
}
PHP_METHOD(AopJoinpoint, getException){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!(obj->current_pointcut->kind_of_advice & AOP_KIND_CATCH)){
zend_error(E_ERROR, "getException is only available when the advice was added with aop_add_after or aop_add_after_throwing");
}
if (obj->exception != NULL) {
RETURN_ZVAL(obj->exception, 1, 0);
}
RETURN_NULL();
}
PHP_METHOD(AopJoinpoint, getMethodName){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
zend_execute_data *data = obj->ex;
zend_function *curr_func;
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY || obj->current_pointcut->kind_of_advice & AOP_KIND_FUNCTION) {
zend_error(E_ERROR, "getMethodName is only available when the JoinPoint is a method call");
}
if (data == NULL) {
RETURN_NULL();
}
curr_func = data->function_state.function;
RETURN_STRING(curr_func->common.function_name, 1);
}
PHP_METHOD(AopJoinpoint, process){
AopJoinpoint_object *obj = (AopJoinpoint_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj || !obj->current_pointcut || !obj->current_pointcut->kind_of_advice) {
zend_error(E_ERROR, "Error");
}
if (!(obj->current_pointcut->kind_of_advice & AOP_KIND_AROUND)) {
zend_error(E_ERROR, "process is only available when the advice was added with aop_add_around");
}
if (obj->current_pointcut->kind_of_advice & AOP_KIND_PROPERTY) {
if (obj->kind_of_advice & AOP_KIND_WRITE) {
#if ZEND_MODULE_API_NO >= 20100525
// zend_literal *key = obj->key;
// NULL for no segfault (use by zend_get_property_info_quick)
zend_literal *key = NULL;
#endif
_test_write_pointcut_and_execute(obj->pos, obj->advice, obj->object, obj->member, obj->value, obj->scope AOP_KEY_C);
} else {
#if ZEND_MODULE_API_NO >= 20100525
// zend_literal *key = obj->key;
// NULL for no segfault (use by zend_get_property_info_quick)
zend_literal *key = NULL;
#endif
obj->value = _test_read_pointcut_and_execute(obj->pos, obj->advice, obj->object, obj->member, obj->type, obj->scope AOP_KEY_C);
}
} else {
_test_func_pointcut_and_execute(obj->pos, obj->advice, obj->ex, obj->object, obj->scope, obj->called_scope, obj->args_overloaded, obj->args, obj->to_return_ptr_ptr);
obj->value = (*obj->to_return_ptr_ptr);
if (!EG(exception)) {
if ((*obj->to_return_ptr_ptr) != NULL) {
RETURN_ZVAL((*obj->to_return_ptr_ptr), 1, 0);
} else {
RETURN_NULL();
}
} else {
RETURN_NULL();
}
}
}