From 7d64bf3846defb41acea7d6093d2153a680fd90f Mon Sep 17 00:00:00 2001 From: Michael Maclean Date: Sun, 15 Sep 2013 23:12:30 +0100 Subject: [PATCH] __construct() now works --- config.m4 | 36 +++++++++++++------------- mosquitto.c | 68 +++++++++++++++++++++++++++++++++++++++---------- php_mosquitto.h | 10 ++++++-- 3 files changed, 80 insertions(+), 34 deletions(-) diff --git a/config.m4 b/config.m4 index cc83328..48a46df 100644 --- a/config.m4 +++ b/config.m4 @@ -1,47 +1,47 @@ dnl $Id$ -dnl config.m4 for extension libmosquitto +dnl config.m4 for extension mosquitto dnl Comments in this file start with the string 'dnl'. dnl Remove where necessary. This file will not work dnl without editing. -PHP_ARG_WITH(libmosquitto, for libmosquitto support, -[ --with-libmosquitto Include libmosquitto support]) +PHP_ARG_WITH(mosquitto, for mosquitto support, +[ --with-mosquitto Include mosquitto support]) -if test "$PHP_LIBMOSQUITTO" != "no"; then +if test "$PHP_MOSQUITTO" != "no"; then dnl Write more examples of tests here... - # --with-libmosquitto -> check with-path + # --with-mosquitto -> check with-path SEARCH_PATH="/usr/local /usr" # you might want to change this SEARCH_FOR="/include/mosquitto.h" # you most likely want to change this - if test -r $PHP_LIBMOSQUITTO/$SEARCH_FOR; then # path given as parameter - LIBMOSQUITTO_DIR=$PHP_LIBMOSQUITTO + if test -r $PHP_MOSQUITTO/$SEARCH_FOR; then # path given as parameter + MOSQUITTO_DIR=$PHP_MOSQUITTO else # search default path list - AC_MSG_CHECKING([for libmosquitto files in default path]) + AC_MSG_CHECKING([for mosquitto files in default path]) for i in $SEARCH_PATH ; do if test -r $i/$SEARCH_FOR; then - LIBMOSQUITTO_DIR=$i - AC_MSG_CHECKING($LIBMOSQUITTO_DIR) + MOSQUITTO_DIR=$i + AC_MSG_CHECKING($MOSQUITTO_DIR) AC_MSG_RESULT(found in $i) fi done fi - if test -z "$LIBMOSQUITTO_DIR"; then + if test -z "$MOSQUITTO_DIR"; then AC_MSG_RESULT([not found]) - AC_MSG_ERROR([Please reinstall the libmosquitto distribution]) + AC_MSG_ERROR([Please reinstall the mosquitto distribution]) fi - # --with-libmosquitto -> add include path - PHP_ADD_INCLUDE($LIBMOSQUITTO_DIR/include) + # --with-mosquitto -> add include path + PHP_ADD_INCLUDE($MOSQUITTO_DIR/include) - # --with-libmosquitto -> check for lib and symbol presence + # --with-mosquitto -> check for lib and symbol presence LIBNAME=mosquitto # you may want to change this - PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $LIBMOSQUITTO_DIR/lib, LIBMOSQUITTO_SHARED_LIBADD) + PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MOSQUITTO_DIR/lib, MOSQUITTO_SHARED_LIBADD) - PHP_SUBST(LIBMOSQUITTO_SHARED_LIBADD) + PHP_SUBST(MOSQUITTO_SHARED_LIBADD) - PHP_NEW_EXTENSION(libmosquitto, libmosquitto.c, $ext_shared) + PHP_NEW_EXTENSION(mosquitto, mosquitto.c, $ext_shared) fi diff --git a/mosquitto.c b/mosquitto.c index 9d642eb..aab10c5 100644 --- a/mosquitto.c +++ b/mosquitto.c @@ -4,11 +4,14 @@ #include "php.h" #include "php_ini.h" +#include "zend_exceptions.h" #include "ext/standard/info.h" #include "php_mosquitto.h" zend_class_entry *mosquitto_ce_client; +zend_class_entry *mosquitto_ce_exception; zend_object_handlers mosquitto_std_object_handlers; +zend_error_handling mosquitto_original_error_handling; PHP_FUNCTION(mosquitto_version) { @@ -19,13 +22,37 @@ PHP_FUNCTION(mosquitto_version) RETURN_LONG(mosquitto_lib_version(NULL, NULL, NULL)); } +/* {{{ */ +PHP_METHOD(Mosquitto_Client, __construct) +{ + mosquitto_client_object *object; + char *id = NULL; + int id_len = 0; + zend_bool clean_session = 0; + + PHP_MOSQUITTO_ERROR_HANDLING(); + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &id, &id_len, &clean_session) == FAILURE) { + PHP_MOSQUITTO_RESTORE_ERRORS(); + return; + } + PHP_MOSQUITTO_RESTORE_ERRORS(); + + object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC); + object->client = mosquitto_new(id, clean_session, NULL); + + if (!object->client) { + zend_throw_exception(mosquitto_ce_exception, "Failed to create a Mosquitto client", 0 TSRMLS_CC); + } +} +/* }}} */ + /* Internal functions */ static void mosquitto_client_object_destroy(void *object TSRMLS_DC) { mosquitto_client_object *client = (mosquitto_client_object *) object; - zend_hash_destroy(context->std.properties); - FREE_HASHTABLE(context->std.properties); + zend_hash_destroy(client->std.properties); + FREE_HASHTABLE(client->std.properties); mosquitto_destroy(client->client); efree(object); } @@ -34,7 +61,6 @@ static zend_object_value mosquitto_client_object_new() { zend_object_value retval; mosquitto_client_object *client; - zval *temp; client = ecalloc(1, sizeof(mosquitto_client_object)); client->std.ce = mosquitto_ce_client; @@ -43,18 +69,24 @@ static zend_object_value mosquitto_client_object_new() { ALLOC_HASHTABLE(client->std.properties); zend_hash_init(client->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0); #if PHP_VERSION_ID < 50399 - zend_hash_copy(client->std.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &temp, sizeof(zval *)); + zend_hash_copy(client->std.properties, &mosquitto_ce_client->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &temp, sizeof(zval *)); #else - object_properties_init(&client->std, ce); + object_properties_init(&client->std, mosquitto_ce_client); #endif retval.handle = zend_objects_store_put(client, NULL, (zend_objects_free_object_storage_t) mosquitto_client_object_destroy, NULL TSRMLS_CC); retval.handlers = &mosquitto_std_object_handlers; return retval; } +/* {{{ mosquitto_client_methods */ +const zend_function_entry mosquitto_client_methods[] = { + PHP_ME(Mosquitto_Client, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + {NULL, NULL, NULL} +}; +/* }}} */ + /* {{{ mosquitto_functions[] */ const zend_function_entry mosquitto_functions[] = { - PHP_FE(mosquitto_version, NULL) PHP_FE_END /* Must be the last line in mosquitto_functions[] */ }; /* }}} */ @@ -65,7 +97,7 @@ zend_module_entry mosquitto_module_entry = { STANDARD_MODULE_HEADER, #endif "mosquitto", - mosquitto_functions, + NULL, PHP_MINIT(mosquitto), PHP_MSHUTDOWN(mosquitto), NULL, @@ -78,16 +110,26 @@ zend_module_entry mosquitto_module_entry = { }; /* }}} */ -#ifdef COMPILE_DL_mosquitto +#ifdef COMPILE_DL_MOSQUITTO ZEND_GET_MODULE(mosquitto) #endif /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(mosquitto) { - /* If you have INI entries, uncomment these lines - REGISTER_INI_ENTRIES(); - */ + memcpy(&mosquitto_std_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + mosquitto_std_object_handlers.clone_obj = NULL; + + zend_class_entry client_ce, exception_ce; + INIT_NS_CLASS_ENTRY(client_ce, "Mosquitto", "Client", mosquitto_client_methods); + mosquitto_ce_client = zend_register_internal_class_ex(&client_ce, NULL, NULL TSRMLS_CC); + mosquitto_ce_client->create_object = mosquitto_client_object_new; + + INIT_NS_CLASS_ENTRY(exception_ce, "Mosquitto", "Exception", NULL); + mosquitto_ce_exception = zend_register_internal_class_ex(&exception_ce, + zend_exception_get_default(TSRMLS_C), "Exception" TSRMLS_CC); + + mosquitto_lib_init(); return SUCCESS; } /* }}} */ @@ -95,9 +137,7 @@ PHP_MINIT_FUNCTION(mosquitto) /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(mosquitto) { - /* uncomment this line if you have INI entries - UNREGISTER_INI_ENTRIES(); - */ + mosquitto_lib_cleanup(); return SUCCESS; } /* }}} */ diff --git a/php_mosquitto.h b/php_mosquitto.h index e333dd2..2864496 100644 --- a/php_mosquitto.h +++ b/php_mosquitto.h @@ -18,10 +18,16 @@ extern zend_module_entry mosquitto_module_entry; #include -typedef struct _mosquitto_context_object { +#define PHP_MOSQUITTO_ERROR_HANDLING() \ + zend_replace_error_handling(EH_THROW, mosquitto_ce_exception, &mosquitto_original_error_handling TSRMLS_CC) + +#define PHP_MOSQUITTO_RESTORE_ERRORS() \ + zend_restore_error_handling(&mosquitto_original_error_handling TSRMLS_CC) + +typedef struct _mosquitto_client_object { zend_object std; struct mosquitto *client; -} mosquitto_context_object; +} mosquitto_client_object; PHP_MINIT_FUNCTION(mosquitto); PHP_MSHUTDOWN_FUNCTION(mosquitto);