back to dev
[m6w6/ext-http] / php_http_object.c
index c278e6bc08b9b75c700b198bf386173154ffcf49..ba6d28c82b8b7fed832cc9327d73bca8782a3d6d 100644 (file)
@@ -6,86 +6,12 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
 #include "php_http_api.h"
 
-PHP_HTTP_API 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)
-{
-       if (!ce) {
-               ce = parent_ce;
-       } else if (parent_ce && !instanceof_function(ce, parent_ce TSRMLS_CC)) {
-               php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "Class %s does not extend %s", ce->name, parent_ce->name);
-               return FAILURE;
-       }
-
-       *ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
-       return SUCCESS;
-}
-
-PHP_HTTP_API zend_error_handling_t php_http_object_get_error_handling(zval *object TSRMLS_DC)
-{
-       zval *zeh, *lzeh;
-       long eh;
-
-       zeh = zend_read_property(Z_OBJCE_P(object), object, ZEND_STRL("errorHandling"), 0 TSRMLS_CC);
-       if (Z_TYPE_P(zeh) != IS_NULL) {
-               lzeh = php_http_ztyp(IS_LONG, zeh);
-               eh = Z_LVAL_P(lzeh);
-               zval_ptr_dtor(&lzeh);
-               return eh;
-       }
-       zeh = zend_read_static_property(php_http_object_get_class_entry(), ZEND_STRL("defaultErrorHandling"), 0 TSRMLS_CC);
-       if (Z_TYPE_P(zeh) != IS_NULL) {
-               lzeh = php_http_ztyp(IS_LONG, zeh);
-               eh = Z_LVAL_P(lzeh);
-               zval_ptr_dtor(&lzeh);
-               return eh;
-       }
-       return EH_NORMAL;
-}
-
-#define PHP_HTTP_BEGIN_ARGS(method, req_args)                  PHP_HTTP_BEGIN_ARGS_EX(HttpObject, method, 0, req_args)
-#define PHP_HTTP_EMPTY_ARGS(method)                                            PHP_HTTP_EMPTY_ARGS_EX(HttpObject, method, 0)
-#define PHP_HTTP_OBJECT_ME(method, visibility)                 PHP_ME(HttpObject, method, PHP_HTTP_ARGS(HttpObject, method), visibility)
-
-PHP_HTTP_BEGIN_ARGS(setErrorHandling, 1)
-       PHP_HTTP_ARG_VAL(eh, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_EMPTY_ARGS(getErrorHandling);
-
-PHP_HTTP_BEGIN_ARGS(setDefaultErrorHandling, 1)
-       PHP_HTTP_ARG_VAL(eh, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_EMPTY_ARGS(getDefaultErrorHandling);
-
-PHP_HTTP_BEGIN_ARGS(triggerError, 3)
-       PHP_HTTP_ARG_VAL(error_type, 0)
-       PHP_HTTP_ARG_VAL(error_code, 0)
-       PHP_HTTP_ARG_VAL(error_message, 0)
-PHP_HTTP_END_ARGS;
-
-static zend_class_entry *php_http_object_class_entry;
-
-zend_class_entry *php_http_object_get_class_entry(void)
-{
-       return php_http_object_class_entry;
-}
-
-static zend_function_entry php_http_object_method_entry[] = {
-       PHP_HTTP_OBJECT_ME(setErrorHandling, ZEND_ACC_PUBLIC)
-       PHP_HTTP_OBJECT_ME(getErrorHandling, ZEND_ACC_PUBLIC)
-       PHP_HTTP_OBJECT_ME(setDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
-       PHP_HTTP_OBJECT_ME(getDefaultErrorHandling, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
-       PHP_HTTP_OBJECT_ME(triggerError, ZEND_ACC_PUBLIC)
-
-       EMPTY_FUNCTION_ENTRY
-};
-
 zend_object_value php_http_object_new(zend_class_entry *ce TSRMLS_DC)
 {
        return php_http_object_new_ex(ce, NULL, NULL TSRMLS_CC);
@@ -93,7 +19,6 @@ zend_object_value php_http_object_new(zend_class_entry *ce TSRMLS_DC)
 
 zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, php_http_object_t **ptr TSRMLS_DC)
 {
-       zend_object_value ov;
        php_http_object_t *o;
 
        o = ecalloc(1, sizeof(php_http_object_t));
@@ -104,88 +29,106 @@ zend_object_value php_http_object_new_ex(zend_class_entry *ce, void *nothing, ph
                *ptr = o;
        }
 
-       ov.handle = zend_objects_store_put(o, NULL, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
-       ov.handlers = zend_get_std_object_handlers();
+       o->zv.handle = zend_objects_store_put(o, NULL, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
+       o->zv.handlers = zend_get_std_object_handlers();
 
-       return ov;
+       return o->zv;
 }
 
-PHP_METHOD(HttpObject, getErrorHandling)
+STATUS php_http_new(zend_object_value *ovp, zend_class_entry *ce, php_http_new_t create, zend_class_entry *parent_ce, void *intern_ptr, void **obj_ptr TSRMLS_DC)
 {
-       RETURN_PROP(php_http_object_get_class_entry(), "errorHandling");
-}
+       zend_object_value ov;
 
-PHP_METHOD(HttpObject, setErrorHandling)
-{
-       long eh;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
-               switch (eh) {
-                       case EH_NORMAL:
-                       case EH_SUPPRESS:
-                       case EH_THROW:
-                               zend_update_property_long(php_http_object_get_class_entry(), getThis(), ZEND_STRL("errorHandling"), eh TSRMLS_CC);
-                               break;
-
-                       default:
-                               php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
-                               break;
-               }
+       if (!ce) {
+               ce = parent_ce;
+       } else if (parent_ce && !instanceof_function(ce, parent_ce TSRMLS_CC)) {
+               php_http_throw(unexpected_val, "Class %s does not extend %s", ce->name, parent_ce->name);
+               return FAILURE;
        }
 
-       RETURN_ZVAL(getThis(), 1, 0);
+       ov = create(ce, intern_ptr, obj_ptr TSRMLS_CC);
+       if (ovp) {
+               *ovp = ov;
+       }
+       return SUCCESS;
 }
 
-PHP_METHOD(HttpObject, getDefaultErrorHandling)
+static inline zend_function *get_object_method(zval *zobject, zval *zmeth TSRMLS_DC)
 {
-       RETURN_SPROP(php_http_object_get_class_entry(), "defaultErrorHandling");
+#if PHP_VERSION_ID >= 50400
+       return Z_OBJ_HT_P(zobject)->get_method(&zobject, Z_STRVAL_P(zmeth), Z_STRLEN_P(zmeth), NULL TSRMLS_CC);
+#else
+       return Z_OBJ_HT_P(zobject)->get_method(&zobject, Z_STRVAL_P(zmeth), Z_STRLEN_P(zmeth) TSRMLS_CC);
+#endif
 }
 
-PHP_METHOD(HttpObject, setDefaultErrorHandling)
+php_http_object_method_t *php_http_object_method_init(php_http_object_method_t *cb, zval *zobject, const char *method_str, size_t method_len TSRMLS_DC)
 {
-       long eh;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &eh)) {
-               switch (eh) {
-                       case EH_NORMAL:
-                       case EH_SUPPRESS:
-                       case EH_THROW:
-                               zend_update_static_property_long(php_http_object_get_class_entry(), ZEND_STRL("defaultErrorHandling"), eh TSRMLS_CC);
-                               break;
-
-                       default:
-                               php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "unknown error handling code (%ld)", eh);
-                               break;
-               }
+       zval *zfn;
+
+       if (!cb) {
+               cb = ecalloc(1, sizeof(*cb));
+       } else {
+               memset(cb, 0, sizeof(*cb));
        }
+
+       MAKE_STD_ZVAL(zfn);
+       ZVAL_STRINGL(zfn, method_str, method_len, 1);
+
+       cb->fci.size = sizeof(cb->fci);
+       cb->fci.function_name = zfn;
+       cb->fcc.initialized = 1;
+       cb->fcc.calling_scope = cb->fcc.called_scope = Z_OBJCE_P(zobject);
+       cb->fcc.function_handler = get_object_method(zobject, cb->fci.function_name TSRMLS_CC);
+
+       return cb;
 }
 
-PHP_METHOD(HttpObject, triggerError)
+void php_http_object_method_dtor(php_http_object_method_t *cb)
 {
-       long eh, code;
-       char *msg_str;
-       int msg_len;
+       if (cb->fci.function_name) {
+               zval_ptr_dtor(&cb->fci.function_name);
+               cb->fci.function_name = NULL;
+       }
+}
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &eh, &code, &msg_str, &msg_len)) {
-               php_http_error(eh TSRMLS_CC, code, "%.*s", msg_len, msg_str);
+void php_http_object_method_free(php_http_object_method_t **cb)
+{
+       if (*cb) {
+               php_http_object_method_dtor(*cb);
+               efree(*cb);
+               *cb = NULL;
        }
 }
 
-PHP_MINIT_FUNCTION(http_object)
+STATUS php_http_object_method_call(php_http_object_method_t *cb, zval *zobject, zval **retval_ptr, int argc, zval ***args TSRMLS_DC)
 {
-       PHP_HTTP_REGISTER_CLASS(http, Object, http_object, NULL, ZEND_ACC_ABSTRACT);
-       php_http_object_get_class_entry()->create_object = php_http_object_new;
+       STATUS rv;
+       zval *retval = NULL;
 
-       zend_declare_property_null(php_http_object_get_class_entry(), ZEND_STRL("defaultErrorHandling"), (ZEND_ACC_STATIC|ZEND_ACC_PROTECTED) TSRMLS_CC);
-       zend_declare_property_null(php_http_object_get_class_entry(), ZEND_STRL("errorHandling"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       Z_ADDREF_P(zobject);
+       cb->fci.object_ptr = zobject;
+       cb->fcc.object_ptr = zobject;
 
-       zend_declare_class_constant_long(php_http_object_get_class_entry(), ZEND_STRL("EH_NORMAL"), EH_NORMAL TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_object_get_class_entry(), ZEND_STRL("EH_SUPPRESS"), EH_SUPPRESS TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_object_get_class_entry(), ZEND_STRL("EH_THROW"), EH_THROW TSRMLS_CC);
+       cb->fci.retval_ptr_ptr = retval_ptr ? retval_ptr : &retval;
 
-       return SUCCESS;
-}
+       cb->fci.param_count = argc;
+       cb->fci.params = args;
 
+       if (cb->fcc.called_scope != Z_OBJCE_P(zobject)) {
+               cb->fcc.called_scope = Z_OBJCE_P(zobject);
+               cb->fcc.function_handler = get_object_method(zobject, cb->fci.function_name TSRMLS_CC);
+       }
+
+       rv = zend_call_function(&cb->fci, &cb->fcc TSRMLS_CC);
+
+       zval_ptr_dtor(&zobject);
+       if (!retval_ptr && retval) {
+               zval_ptr_dtor(&retval);
+       }
+
+       return rv;
+}
 
 /*
  * Local variables: