39e9116cbac20e9a95ba3df1d8ddaa33ff3b25ce
[m6w6/ext-http] / php_http_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 STATUS php_http_new(zend_object_value *ov, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
16 {
17 if (!ce) {
18 ce = parent_ce;
19 } else if (parent_ce && !instanceof_function(ce, parent_ce TSRMLS_CC)) {
20 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "Class %s does not extend %s", ce->name, parent_ce->name);
21 return FAILURE;
22 }
23
24 *ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
25 return SUCCESS;
26 }
27
28 PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *object TSRMLS_DC)
29 {
30 zval *zeh, *lzeh;
31 long eh;
32
33 zeh = zend_read_property(Z_OBJCE_P(object), object, ZEND_STRL("errorHandling"), 0 TSRMLS_CC);
34 if (Z_TYPE_P(zeh) != IS_NULL) {
35 lzeh = php_http_ztyp(IS_LONG, zeh);
36 eh = Z_LVAL_P(lzeh);
37 zval_ptr_dtor(&lzeh);
38 return eh;
39 }
40 zeh = zend_read_static_property(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
41 if (Z_TYPE_P(zeh) != IS_NULL) {
42 lzeh = php_http_ztyp(IS_LONG, zeh);
43 eh = Z_LVAL_P(lzeh);
44 zval_ptr_dtor(&lzeh);
45 return eh;
46 }
47 return EH_NORMAL;
48 }
49
50 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpObject, method, 0, req_args)
51 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpObject, method, 0)
52 #define PHP_HTTP_OBJECT_ME(method, visibility) PHP_ME(HttpObject, method, PHP_HTTP_ARGS(HttpObject, method), visibility)
53
54 PHP_HTTP_BEGIN_ARGS(factory, 1)
55 PHP_HTTP_ARG_VAL(class_name, 0)
56 PHP_HTTP_ARG_VAL(ctor_args, 0)
57 PHP_HTTP_END_ARGS;
58
59 PHP_HTTP_BEGIN_ARGS(setErrorHandling, 1)
60 PHP_HTTP_ARG_VAL(eh, 0)
61 PHP_HTTP_END_ARGS;
62
63 PHP_HTTP_EMPTY_ARGS(getErrorHandling);
64
65 PHP_HTTP_BEGIN_ARGS(setDefaultErrorHandling, 1)
66 PHP_HTTP_ARG_VAL(eh, 0)
67 PHP_HTTP_END_ARGS;
68
69 PHP_HTTP_EMPTY_ARGS(getDefaultErrorHandling);
70
71 zend_class_entry *php_http_object_class_entry;
72 zend_function_entry php_http_object_method_entry[] = {
73 PHP_HTTP_OBJECT_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
74
75 PHP_HTTP_OBJECT_ME(setErrorHandling, ZEND_ACC_PUBLIC)
76 PHP_HTTP_OBJECT_ME(getErrorHandling, ZEND_ACC_PUBLIC)
77 PHP_HTTP_OBJECT_ME(setDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
78 PHP_HTTP_OBJECT_ME(getDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
79
80 EMPTY_FUNCTION_ENTRY
81 };
82
83 zend_object_value php_http_object_new(zend_class_entry *ce TSRMLS_DC)
84 {
85 return php_http_object_new_ex(ce, NULL, NULL TSRMLS_CC);
86 }
87
88 zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, php_http_object_t **ptr TSRMLS_DC)
89 {
90 zend_object_value ov;
91 php_http_object_t *o;
92
93 o = ecalloc(1, sizeof(php_http_object_t));
94 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
95 object_properties_init((zend_object *) o, ce);
96
97 if (ptr) {
98 *ptr = o;
99 }
100
101 ov.handle = zend_objects_store_put(o, NULL, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
102 ov.handlers = zend_get_std_object_handlers();
103
104 return ov;
105 }
106
107 PHP_METHOD(HttpObject, factory)
108 {
109 zval *ctor_args = NULL;
110 zend_class_entry *class_entry = NULL;
111 zval *object_ctor;
112 zend_fcall_info fci;
113 zend_fcall_info_cache fcc;
114
115 with_error_handling(EH_THROW, php_http_exception_class_entry) {
116 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "C|a/!", &class_entry, &ctor_args)) {
117 object_init_ex(return_value, class_entry);
118
119 MAKE_STD_ZVAL(object_ctor);
120 array_init(object_ctor);
121
122 Z_ADDREF_P(return_value);
123 add_next_index_zval(object_ctor, return_value);
124 add_next_index_stringl(object_ctor, ZEND_STRL("__construct"), 1);
125
126 zend_fcall_info_init(object_ctor, 0, &fci, &fcc, NULL, NULL TSRMLS_CC);
127 zend_fcall_info_call(&fci, &fcc, NULL, ctor_args TSRMLS_CC);
128
129 zval_ptr_dtor(&object_ctor);
130 }
131 } end_error_handling();
132 }
133
134 PHP_METHOD(HttpObject, getErrorHandling)
135 {
136 RETURN_PROP(php_http_object_class_entry, "errorHandling");
137 }
138
139 PHP_METHOD(HttpObject, setErrorHandling)
140 {
141 long eh;
142
143 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
144 switch (eh) {
145 case EH_NORMAL:
146 case EH_SUPPRESS:
147 case EH_THROW:
148 zend_update_property_long(php_http_object_class_entry, getThis(), ZEND_STRL("errorHandling"), eh TSRMLS_CC);
149 break;
150
151 default:
152 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
153 break;
154 }
155 }
156
157 RETURN_ZVAL(getThis(), 1, 0);
158 }
159
160 PHP_METHOD(HttpObject, getDefaultErrorHandling)
161 {
162 RETURN_SPROP(php_http_object_class_entry, "defaultErrorHandling");
163 }
164
165 PHP_METHOD(HttpObject, setDefaultErrorHandling)
166 {
167 long eh;
168
169 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
170 switch (eh) {
171 case EH_NORMAL:
172 case EH_SUPPRESS:
173 case EH_THROW:
174 zend_update_static_property_long(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), eh TSRMLS_CC);
175 break;
176
177 default:
178 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
179 break;
180 }
181 }
182 }
183
184 PHP_MINIT_FUNCTION(http_object)
185 {
186 PHP_HTTP_REGISTER_CLASS(http, Object, http_object, NULL, ZEND_ACC_ABSTRACT);
187 php_http_object_class_entry->create_object = php_http_object_new;
188
189 zend_declare_property_null(php_http_object_class_entry, ZEND_STRL("defaultErrorHandling"), (ZEND_ACC_STATIC|ZEND_ACC_PROTECTED) TSRMLS_CC);
190 zend_declare_property_null(php_http_object_class_entry, ZEND_STRL("errorHandling"), ZEND_ACC_PROTECTED TSRMLS_CC);
191
192 zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_NORMAL"), EH_NORMAL TSRMLS_CC);
193 zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_SUPPRESS"), EH_SUPPRESS TSRMLS_CC);
194 zend_declare_class_constant_long(php_http_object_class_entry, ZEND_STRL("EH_THROW"), EH_THROW TSRMLS_CC);
195
196 return SUCCESS;
197 }
198
199
200 /*
201 * Local variables:
202 * tab-width: 4
203 * c-basic-offset: 4
204 * End:
205 * vim600: noet sw=4 ts=4 fdm=marker
206 * vim<600: noet sw=4 ts=4
207 */
208