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