Skip to content

Commit

Permalink
__construct() now works
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdm committed Sep 15, 2013
1 parent 7fef039 commit 7d64bf3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 34 deletions.
36 changes: 18 additions & 18 deletions config.m4
Original file line number Diff line number Diff line change
@@ -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
68 changes: 54 additions & 14 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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[] */
};
/* }}} */
Expand All @@ -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,
Expand All @@ -78,26 +110,34 @@ 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;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(mosquitto)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
mosquitto_lib_cleanup();
return SUCCESS;
}
/* }}} */
Expand Down
10 changes: 8 additions & 2 deletions php_mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ extern zend_module_entry mosquitto_module_entry;

#include <mosquitto.h>

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);
Expand Down

0 comments on commit 7d64bf3

Please sign in to comment.