Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleanup, removing length parameter to std::string

  • Loading branch information...
commit fe9b9e77772d0b4457f5bdefee4b7b1c08c7c44d 1 parent aa5797b
@jbboehr authored
View
5 .gitignore
@@ -1,3 +1,5 @@
+.*project
+*~
.deps
.dirstamp
.libs
@@ -12,6 +14,7 @@ gmon*
nbproject
build
modules
+test.php
tests/*.diff
tests/*.exp
tests/*.php
@@ -38,4 +41,4 @@ missing
mkinstalldirs
run-tests.php
tmp-php.ini
-specs
+specs
2  mustache
@@ -1 +1 @@
-Subproject commit aa80e4ae0134a230ffb0b64d8ea1e9c86c595e13
+Subproject commit d9a83ba41b37dd1546a6ae930605dd19ba897c2e
View
118 mustache_data.cpp
@@ -19,36 +19,39 @@ static zend_object_handlers MustacheData_obj_handlers;
static void MustacheData_obj_free(void *object TSRMLS_DC)
{
- php_obj_MustacheData * payload = (php_obj_MustacheData *) object;
-
- mustache::Data * data = payload->data;
+ try {
+ php_obj_MustacheData * payload = (php_obj_MustacheData *) object;
+
+ if( payload->data != NULL ) {
+ delete payload->data;
+ }
- if( data != NULL ) {
- delete data;
+ efree(object);
+ } catch(...) {
+ mustache_exception_handler();
}
-
- efree(object);
}
static zend_object_value MustacheData_obj_create(zend_class_entry *class_type TSRMLS_DC)
{
- php_obj_MustacheData * payload;
- zval *tmp;
zend_object_value retval;
+
+ try {
+ php_obj_MustacheData * payload;
+ zval * tmp;
- payload = (php_obj_MustacheData *) emalloc(sizeof(php_obj_MustacheData));
- memset(payload, 0, sizeof(php_obj_MustacheData));
- payload->obj.ce = class_type;
+ payload = (php_obj_MustacheData *) emalloc(sizeof(php_obj_MustacheData));
+ memset(payload, 0, sizeof(php_obj_MustacheData));
+ payload->obj.ce = class_type;
- try {
- payload->data = new mustache::Data;
- } catch( std::bad_alloc& e ) {
payload->data = NULL;
- php_error(E_ERROR, "Failed to allocate memory when initializing MustacheData");
- }
- retval.handle = zend_objects_store_put(payload, NULL, (zend_objects_free_object_storage_t) MustacheData_obj_free, NULL TSRMLS_CC);
- retval.handlers = &MustacheData_obj_handlers;
+ retval.handle = zend_objects_store_put(payload, NULL, (zend_objects_free_object_storage_t) MustacheData_obj_free, NULL TSRMLS_CC);
+ retval.handlers = &MustacheData_obj_handlers;
+
+ } catch(...) {
+ mustache_exception_handler();
+ }
return retval;
}
@@ -78,36 +81,34 @@ PHP_MINIT_FUNCTION(mustache_data)
*/
PHP_METHOD(MustacheData, __construct)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_MustacheData * payload;
-
- zval * data;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|z",
- &_this_zval, MustacheData_ce_ptr, &data) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_MustacheData *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Custom parameters
+ zval * data;
+
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz",
+ &_this_zval, MustacheData_ce_ptr, &data) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_MustacheData * payload =
+ (php_obj_MustacheData *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
// Check if argument was given
if( data == NULL ) {
- return;
+ throw PhpInvalidParameterException();
}
// Convert data
+ payload->data = new mustache::Data();
mustache_data_from_zval(payload->data, data TSRMLS_CC);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} __construct */
@@ -116,24 +117,19 @@ PHP_METHOD(MustacheData, __construct)
*/
PHP_METHOD(MustacheData, toValue)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_MustacheData * payload;
-
- zval * datacpy = NULL;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, MustacheData_ce_ptr) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
+ try {
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, MustacheData_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
- payload = (php_obj_MustacheData *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_MustacheData * payload =
+ (php_obj_MustacheData *) zend_object_store_get_object(_this_zval TSRMLS_CC);
- // Main
- try {
// Check if data was initialized
if( payload->data == NULL ) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "MustacheData was not initialized properly");
@@ -142,16 +138,14 @@ PHP_METHOD(MustacheData, toValue)
}
// Reverse template data
- datacpy = mustache_data_to_zval(payload->data TSRMLS_CC);
+ zval * datacpy = mustache_data_to_zval(payload->data TSRMLS_CC);
// Copy data into return value
*return_value = *datacpy;
zval_copy_ctor(return_value);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} toValue */
View
571 mustache_mustache.cpp
@@ -30,36 +30,42 @@ static zend_object_handlers Mustache_obj_handlers;
static void Mustache_obj_free(void *object TSRMLS_DC)
{
- php_obj_Mustache *payload = (php_obj_Mustache *)object;
-
- mustache::Mustache * mustache = payload->mustache;
-
- delete mustache;
+ try {
+ php_obj_Mustache * payload = (php_obj_Mustache *)object;
- efree(object);
+ if( payload->mustache != NULL ) {
+ delete payload->mustache;
+ }
+
+ efree(object);
+
+ } catch(...) {
+ mustache_exception_handler();
+ }
}
static zend_object_value Mustache_obj_create(zend_class_entry *class_type TSRMLS_DC)
{
- php_obj_Mustache * payload;
- zval *tmp;
zend_object_value retval;
+
+ try {
+ php_obj_Mustache * payload;
+ zval * tmp;
- payload = (php_obj_Mustache *)emalloc(sizeof(php_obj_Mustache));
- memset(payload, 0, sizeof(php_obj_Mustache));
- payload->obj.ce = class_type;
+ payload = (php_obj_Mustache *) emalloc(sizeof(php_obj_Mustache));
+ memset(payload, 0, sizeof(php_obj_Mustache));
+ payload->obj.ce = class_type;
- try {
payload->mustache = new mustache::Mustache;
- } catch( std::bad_alloc& e ) {
- payload->mustache = NULL;
- php_error(E_ERROR, "Failed to allocate memory when initializing Mustache");
+
+ retval.handle = zend_objects_store_put(payload, NULL,
+ (zend_objects_free_object_storage_t) Mustache_obj_free, NULL TSRMLS_CC);
+ retval.handlers = &Mustache_obj_handlers;
+
+ } catch(...) {
+ mustache_exception_handler();
}
- retval.handle = zend_objects_store_put(payload, NULL,
- (zend_objects_free_object_storage_t) Mustache_obj_free, NULL TSRMLS_CC);
- retval.handlers = &Mustache_obj_handlers;
-
return retval;
}
@@ -69,15 +75,20 @@ static zend_object_value Mustache_obj_create(zend_class_entry *class_type TSRMLS
PHP_MINIT_FUNCTION(mustache_mustache)
{
- zend_class_entry ce;
-
- INIT_CLASS_ENTRY(ce, "Mustache", Mustache_methods);
- ce.create_object = Mustache_obj_create;
- Mustache_ce_ptr = zend_register_internal_class(&ce);
- memcpy(&Mustache_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
- Mustache_obj_handlers.clone_obj = NULL;
-
- return SUCCESS;
+ try {
+ zend_class_entry ce;
+
+ INIT_CLASS_ENTRY(ce, "Mustache", Mustache_methods);
+ ce.create_object = Mustache_obj_create;
+ Mustache_ce_ptr = zend_register_internal_class(&ce);
+ memcpy(&Mustache_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+ Mustache_obj_handlers.clone_obj = NULL;
+
+ return SUCCESS;
+ } catch(...) {
+ mustache_exception_handler();
+ return FAILURE;
+ }
}
@@ -88,19 +99,23 @@ PHP_MINIT_FUNCTION(mustache_mustache)
*/
PHP_METHOD(Mustache, __construct)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
+ try {
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, Mustache_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, Mustache_ce_ptr) == FAILURE) {
- return;
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
+ } catch(...) {
+ mustache_exception_handler();
}
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
}
/* }}} __construct */
@@ -108,31 +123,29 @@ PHP_METHOD(Mustache, __construct)
*/
PHP_METHOD(Mustache, getEscapeByDefault)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, Mustache_ce_ptr) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, Mustache_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
+ // Main
if( payload->mustache->getEscapeByDefault() ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} getEscapeByDefault */
@@ -141,28 +154,26 @@ PHP_METHOD(Mustache, getEscapeByDefault)
*/
PHP_METHOD(Mustache, getStartSequence)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, Mustache_ce_ptr) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, Mustache_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
+ // Main
const std::string & str = payload->mustache->getStartSequence();
RETURN_STRING(str.c_str(), 1);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} getStartSequence */
@@ -171,28 +182,26 @@ PHP_METHOD(Mustache, getStartSequence)
*/
PHP_METHOD(Mustache, getStopSequence)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, Mustache_ce_ptr) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, Mustache_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
+ // Main
const std::string & str = payload->mustache->getStopSequence();
RETURN_STRING(str.c_str(), 1);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} getStopSequence */
@@ -201,30 +210,29 @@ PHP_METHOD(Mustache, getStopSequence)
*/
PHP_METHOD(Mustache, setEscapeByDefault)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
+ try {
+ // Custom parameters
+ long flag;
- long flag;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
- &_this_zval, Mustache_ce_ptr, &flag) == FAILURE) {
- return;
- }
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
+ &_this_zval, Mustache_ce_ptr, &flag) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
- // Main
- try {
+ // Main
payload->mustache->setEscapeByDefault((bool) flag != 0);
RETURN_TRUE;
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} setEscapeByDefault */
@@ -233,31 +241,30 @@ PHP_METHOD(Mustache, setEscapeByDefault)
*/
PHP_METHOD(Mustache, setStartSequence)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- char * str;
- long str_len;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
- &_this_zval, Mustache_ce_ptr, &str, &str_len) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
- payload->mustache->setStartSequence(str, str_len);
+ // Custom parameters
+ char * str;
+ long str_len;
+
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
+ &_this_zval, Mustache_ce_ptr, &str, &str_len) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
+ // Main
+ payload->mustache->setStartSequence(str/*, str_len*/);
RETURN_TRUE;
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} setStartSequence */
@@ -266,31 +273,30 @@ PHP_METHOD(Mustache, setStartSequence)
*/
PHP_METHOD(Mustache, setStopSequence)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- char * str;
- long str_len;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
- &_this_zval, Mustache_ce_ptr, &str, &str_len) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
- payload->mustache->setStopSequence(str, str_len);
+ // Custom parameters
+ char * str;
+ long str_len;
+
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
+ &_this_zval, Mustache_ce_ptr, &str, &str_len) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
+ // Main
+ payload->mustache->setStopSequence(str/*, str_len*/);
RETURN_TRUE;
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} setStartSequence */
@@ -299,64 +305,65 @@ PHP_METHOD(Mustache, setStopSequence)
*/
PHP_METHOD(Mustache, compile)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- char * template_str;
- long template_len;
- std::string templateStr;
- mustache::Node * root;
- std::string className;
- zend_class_entry * MustacheTemplate_ce_ptr;
- php_obj_MustacheTemplate * intern;
+ try {
+ // Custom parameters
+ zval * tmpl;
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
- &_this_zval, Mustache_ce_ptr, &template_str, &template_len) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz",
+ &_this_zval, Mustache_ce_ptr, &tmpl) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
- try {
- templateStr.assign(template_str, (size_t) template_len);
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
- // Get MustacheTemplate class entry
- className.assign("MustacheTemplate");
- MustacheTemplate_ce_ptr = mustache_get_class_entry((char *)className.c_str(), className.length() TSRMLS_CC);
-
- if( MustacheTemplate_ce_ptr == NULL ) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class %s does not exist%s", className.c_str());
+ // Check template parameter
+ mustache::Node * templateNodePtr;
+ if( !mustache_parse_template_param(tmpl, payload->mustache, &templateNodePtr TSRMLS_CC) ) {
+ delete templateNodePtr;
RETURN_FALSE;
return;
}
- // Initialize new object
- object_init_ex(return_value, MustacheTemplate_ce_ptr TSRMLS_CC);
- intern = (php_obj_MustacheTemplate *) zend_objects_get_address(return_value TSRMLS_CC);
-
- // Get object's internal node pointer
- if( intern->node == NULL ) {
- intern->node = root = new mustache::Node;
- } else {
- root = intern->node;
- }
-
- // Tokenize
- payload->mustache->tokenize(&templateStr, root);
-
+ // Handle return value
+ if( Z_TYPE_P(tmpl) == IS_STRING ) {
+ // Get MustacheTemplate class entry
+ std::string className("MustacheTemplate");
+ zend_class_entry * MustacheTemplate_ce_ptr =
+ mustache_get_class_entry((char *)className.c_str(), className.length() TSRMLS_CC);
+
+ if( MustacheTemplate_ce_ptr == NULL ) {
+ delete templateNodePtr;
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class %s does not exist%s", className.c_str());
+ RETURN_FALSE;
+ return;
+ }
+
+ // Initialize new object
+ object_init_ex(return_value, MustacheTemplate_ce_ptr TSRMLS_CC);
+ php_obj_MustacheTemplate * intern =
+ (php_obj_MustacheTemplate *) zend_objects_get_address(return_value TSRMLS_CC);
+
+ intern->node = templateNodePtr;
+
// Ref - not sure if this is required
// Z_SET_REFCOUNT_P(return_value, 1);
// Z_SET_ISREF_P(return_value);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+
+ } else if( Z_TYPE_P(tmpl) == IS_OBJECT ) {
+ // Handle return value for object parameter
+ // @todo return the object itself?
+ RETURN_TRUE;
+ }
+
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} compile */
@@ -365,41 +372,36 @@ PHP_METHOD(Mustache, compile)
*/
PHP_METHOD(Mustache, tokenize)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- char * template_str;
- long template_len;
- std::string templateStr;
+ try {
+ // Custom parameters
+ char * template_str;
+ long template_len;
- mustache::Node root;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
- &_this_zval, Mustache_ce_ptr, &template_str, &template_len) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
+ &_this_zval, Mustache_ce_ptr, &template_str, &template_len) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
- try {
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
// Assign template to string
- templateStr.assign(template_str, (size_t) template_len);
+ std::string templateStr(template_str/*, template_len*/);
// Tokenize template
+ mustache::Node root;
payload->mustache->tokenize(&templateStr, &root);
// Convert to PHP array
mustache_node_to_zval(&root, return_value TSRMLS_CC);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} tokenize */
@@ -408,60 +410,54 @@ PHP_METHOD(Mustache, tokenize)
*/
PHP_METHOD(Mustache, render)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- zval * tmpl = NULL;
- zval * data = NULL;
- zval * partials = NULL;
-
- mustache::Node templateNode;
- mustache::Node * templateNodePtr = NULL;
- mustache::Data templateData;
- mustache::Data * templateDataPtr = NULL;
- mustache::Node::Partials templatePartials;
- std::string output;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ozz|a/",
- &_this_zval, Mustache_ce_ptr, &tmpl, &data, &partials) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Custom parameters
+ zval * tmpl;
+ zval * data;
+ zval * partials;
+
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ozz|a/",
+ &_this_zval, Mustache_ce_ptr, &tmpl, &data, &partials) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
// Prepare template tree
- templateNodePtr = &templateNode;
+ mustache::Node templateNode;
+ mustache::Node * templateNodePtr = &templateNode;
if( !mustache_parse_template_param(tmpl, payload->mustache, &templateNodePtr TSRMLS_CC) ) {
RETURN_FALSE;
return;
}
// Prepare template data
- templateDataPtr = &templateData;
+ mustache::Data templateData;
+ mustache::Data * templateDataPtr = &templateData;
if( !mustache_parse_data_param(data, payload->mustache, &templateDataPtr TSRMLS_CC) ) {
RETURN_FALSE;
return;
}
// Tokenize partials
+ mustache::Node::Partials templatePartials;
mustache_parse_partials_param(partials, payload->mustache, &templatePartials);
// Render template
+ std::string output;
payload->mustache->render(templateNodePtr, templateDataPtr, &templatePartials, &output);
// Output
RETURN_STRING(output.c_str(), 1); // Yes reallocate
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} render */
@@ -470,40 +466,35 @@ PHP_METHOD(Mustache, render)
*/
PHP_METHOD(Mustache, debugDataStructure)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_Mustache * payload;
-
- zval * data = NULL;
- zval * datacpy = NULL;
-
- mustache::Data templateData;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa/",
- &_this_zval, Mustache_ce_ptr, &data) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Custom parameters
+ zval * data;
+
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa/",
+ &_this_zval, Mustache_ce_ptr, &data) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_Mustache * payload =
+ (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
// Prepare template data
+ mustache::Data templateData;
mustache_data_from_zval(&templateData, data TSRMLS_CC);
// Reverse template data
- datacpy = mustache_data_to_zval(&templateData TSRMLS_CC);
+ zval * datacpy = mustache_data_to_zval(&templateData TSRMLS_CC);
*return_value = *datacpy;
zval_copy_ctor(return_value);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} debugDataStructure */
View
273 mustache_template.cpp
@@ -9,7 +9,6 @@
static zend_function_entry MustacheTemplate_methods[] = {
PHP_ME(MustacheTemplate, __construct, MustacheTemplate____construct_args, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
- PHP_ME(MustacheTemplate, render, MustacheTemplate__render_args, ZEND_ACC_PUBLIC)
PHP_ME(MustacheTemplate, toArray, MustacheTemplate__toArray_args, ZEND_ACC_PUBLIC)
PHP_ME(MustacheTemplate, __toString, MustacheTemplate____toString_args, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
@@ -23,49 +22,45 @@ static zend_object_handlers MustacheTemplate_obj_handlers;
static void MustacheTemplate_obj_free(void *object TSRMLS_DC)
{
- php_obj_MustacheTemplate *payload = (php_obj_MustacheTemplate *)object;
-
- mustache::Mustache * mustache = payload->mustache;
- std::string * tmpl = payload->tmpl;
- mustache::Node * node = payload->node;
+ try {
+ php_obj_MustacheTemplate * payload = (php_obj_MustacheTemplate *) object;
+
+ if( payload->tmpl != NULL ) {
+ delete payload->tmpl;
+ }
+ if( payload->node != NULL ) {
+ delete payload->node;
+ }
- if( node != NULL ) {
- delete node;
- }
- if( tmpl != NULL ) {
- delete tmpl;
- }
- if( mustache != NULL ) {
- delete mustache;
+ efree(object);
+
+ } catch(...) {
+ mustache_exception_handler();
}
-
- efree(object);
}
static zend_object_value MustacheTemplate_obj_create(zend_class_entry *class_type TSRMLS_DC)
{
- php_obj_MustacheTemplate * payload;
- zval *tmp;
zend_object_value retval;
-
- payload = (php_obj_MustacheTemplate *)emalloc(sizeof(php_obj_MustacheTemplate));
- memset(payload, 0, sizeof(php_obj_MustacheTemplate));
- payload->obj.ce = class_type;
-
+
try {
- payload->mustache = new mustache::Mustache;
- payload->tmpl = new std::string;
- payload->node = new mustache::Node;
- } catch( std::bad_alloc& e ) {
- payload->mustache = NULL;
- payload->tmpl = NULL;
- payload->node = NULL;
- php_error(E_ERROR, "Failed to allocate memory when initializing MustacheTemplate");
- }
+ php_obj_MustacheTemplate * payload;
+ zval * tmp;
- retval.handle = zend_objects_store_put(payload, NULL, (zend_objects_free_object_storage_t) MustacheTemplate_obj_free, NULL TSRMLS_CC);
- retval.handlers = &MustacheTemplate_obj_handlers;
+ payload = (php_obj_MustacheTemplate *) emalloc(sizeof(php_obj_MustacheTemplate));
+ memset(payload, 0, sizeof(php_obj_MustacheTemplate));
+ payload->obj.ce = class_type;
+
+ payload->tmpl = NULL; //new std::string();
+ payload->node = NULL; //new mustache::Node();
+ retval.handle = zend_objects_store_put(payload, NULL, (zend_objects_free_object_storage_t) MustacheTemplate_obj_free, NULL TSRMLS_CC);
+ retval.handlers = &MustacheTemplate_obj_handlers;
+
+ } catch(...) {
+ mustache_exception_handler();
+ }
+
return retval;
}
@@ -75,15 +70,20 @@ static zend_object_value MustacheTemplate_obj_create(zend_class_entry *class_typ
PHP_MINIT_FUNCTION(mustache_template)
{
- zend_class_entry ce;
-
- INIT_CLASS_ENTRY(ce, "MustacheTemplate", MustacheTemplate_methods);
- ce.create_object = MustacheTemplate_obj_create;
- MustacheTemplate_ce_ptr = zend_register_internal_class(&ce);
- memcpy(&MustacheTemplate_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
- MustacheTemplate_obj_handlers.clone_obj = NULL;
-
- return SUCCESS;
+ try {
+ zend_class_entry ce;
+
+ INIT_CLASS_ENTRY(ce, "MustacheTemplate", MustacheTemplate_methods);
+ ce.create_object = MustacheTemplate_obj_create;
+ MustacheTemplate_ce_ptr = zend_register_internal_class(&ce);
+ memcpy(&MustacheTemplate_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+ MustacheTemplate_obj_handlers.clone_obj = NULL;
+
+ return SUCCESS;
+ } catch(...) {
+ mustache_exception_handler();
+ return FAILURE;
+ }
}
@@ -94,137 +94,66 @@ PHP_MINIT_FUNCTION(mustache_template)
*/
PHP_METHOD(MustacheTemplate, __construct)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_MustacheTemplate * payload;
-
- char * template_str;
- long template_len;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|s",
- &_this_zval, MustacheTemplate_ce_ptr, &template_str, &template_len) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Custom parameters
+ char * template_str;
+ long template_len;
+
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
+ &_this_zval, MustacheTemplate_ce_ptr, &template_str, &template_len) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_MustacheTemplate * payload =
+ (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
// Check if data was null
if( template_len <= 0 || template_str == NULL ) {
- return;
+ throw PhpInvalidParameterException();
}
// Store template
- payload->tmpl->assign(template_str, (size_t) template_len);
-
- // Tokenize template
- payload->mustache->tokenize(payload->tmpl, payload->node);
+ payload->tmpl = new std::string(template_str/*, template_len*/);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} __construct */
-/* {{{ proto string render(array data, array partials)
+/* {{{ proto array toArray()
*/
-PHP_METHOD(MustacheTemplate, render)
+PHP_METHOD(MustacheTemplate, toArray)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_MustacheTemplate * payload;
-
- zval * data = NULL;
- zval * partials = NULL;
-
- mustache::Data templateData;
- mustache::Data * templateDataPtr = NULL;
- mustache::Node::Partials templatePartials;
- std::string output;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|za/",
- &_this_zval, MustacheTemplate_ce_ptr, &data, &partials) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
- // Check payload
- if( payload->mustache == NULL || payload->node == NULL ) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "MustacheTemplate was not initialized properly");
- RETURN_FALSE;
- return;
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, MustacheTemplate_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
}
- // Prepare template data
- templateDataPtr = &templateData;
- if( !mustache_parse_data_param(data, payload->mustache, &templateDataPtr TSRMLS_CC) ) {
- RETURN_FALSE;
- return;
- }
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_MustacheTemplate * payload =
+ (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
- // Tokenize partials
- mustache_parse_partials_param(partials, payload->mustache, &templatePartials);
-
- // Render template
- payload->mustache->render(payload->node, templateDataPtr, &templatePartials, &output);
-
- // Output
- RETURN_STRING(output.c_str(), 1); // Yes reallocate
-
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
- }
-}
-/* }}} render */
-
-/* {{{ proto array toArray()
- */
-PHP_METHOD(MustacheTemplate, toArray)
-{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_MustacheTemplate * payload;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, MustacheTemplate_ce_ptr) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
- try {
// Check payload
- if( payload->mustache == NULL || payload->node == NULL ) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "MustacheTemplate was not initialized properly");
- RETURN_FALSE;
- return;
+ if( payload->node == NULL ) {
+ throw InvalidParameterException("MustacheTemplate was not initialized properly");
}
// Convert to PHP array
mustache_node_to_zval(payload->node, return_value TSRMLS_CC);
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
/* }}} toArray */
@@ -233,36 +162,30 @@ PHP_METHOD(MustacheTemplate, toArray)
*/
PHP_METHOD(MustacheTemplate, __toString)
{
- zend_class_entry * _this_ce;
- zval * _this_zval;
- php_obj_MustacheTemplate * payload;
-
- if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
- &_this_zval, MustacheTemplate_ce_ptr) == FAILURE) {
- return;
- }
-
- _this_zval = getThis();
- _this_ce = Z_OBJCE_P(_this_zval);
-
- payload = (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
-
- // Main
try {
+ // Check parameters
+ zval * _this_zval;
+ if( zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
+ &_this_zval, MustacheTemplate_ce_ptr) == FAILURE) {
+ throw PhpInvalidParameterException();
+ }
+
+ // Class parameters
+ _this_zval = getThis();
+ zend_class_entry * _this_ce = Z_OBJCE_P(_this_zval);
+ php_obj_MustacheTemplate * payload =
+ (php_obj_MustacheTemplate *) zend_object_store_get_object(_this_zval TSRMLS_CC);
+
// Check payload
- if( payload->mustache == NULL || payload->node == NULL ) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "MustacheTemplate was not initialized properly");
- RETURN_FALSE;
- return;
+ if( payload->tmpl == NULL ) {
+ throw InvalidParameterException("MustacheTemplate was not initialized properly");
}
// Return
RETURN_STRING(payload->tmpl->c_str(), 1); // Yes reallocate
- } catch( mustache::Exception& e ) {
- mustache_error_handler(e.what(), &e, return_value TSRMLS_CC);
- } catch( std::bad_alloc& e ) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch(...) {
+ mustache_exception_handler();
}
}
-/* }}} toArray */
+/* }}} toArray */
View
11 mustache_template.hpp
@@ -11,7 +11,6 @@ static zend_class_entry * MustacheTemplate_ce_ptr = NULL;
typedef struct _php_obj_MustacheTemplate {
zend_object obj;
std::string * tmpl;
- mustache::Mustache * mustache;
mustache::Node * node;
} php_obj_MustacheTemplate;
@@ -23,17 +22,11 @@ PHP_METHOD(MustacheTemplate, __construct);
ZEND_BEGIN_ARG_INFO_EX(MustacheTemplate____construct_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, vars)
ZEND_END_ARG_INFO()
-
-PHP_METHOD(MustacheTemplate, render);
-ZEND_BEGIN_ARG_INFO_EX(MustacheTemplate__render_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 2)
- ZEND_ARG_INFO(0, vars)
- ZEND_ARG_INFO(0, partials)
-ZEND_END_ARG_INFO()
-
+
PHP_METHOD(MustacheTemplate, toArray);
ZEND_BEGIN_ARG_INFO_EX(MustacheTemplate__toArray_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
-
+
PHP_METHOD(MustacheTemplate, __toString);
ZEND_BEGIN_ARG_INFO_EX(MustacheTemplate____toString_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
View
65 php_mustache.cpp
@@ -129,7 +129,7 @@ void mustache_data_from_zval(mustache::Data * node, zval * current TSRMLS_DC)
case IS_STRING:
convert_to_string(current);
node->type = mustache::Data::TypeString;
- node->val = new std::string(Z_STRVAL_P(current), (size_t) Z_STRLEN_P(current));
+ node->val = new std::string(Z_STRVAL_P(current)/*, (size_t) Z_STRLEN_P(current)*/);
break;
case IS_ARRAY: // START IS_ARRAY -----------------------------------------
node->type = mustache::Data::TypeNone;
@@ -279,36 +279,67 @@ void mustache_error_handler(const char * msg, mustache::Exception * e,
}
}
+void mustache_exception_handler()
+{
+#if PHP_MUSTACHE_THROW_EXCEPTIONS
+ throw;
+#else
+ try {
+ throw;
+ } catch( mustache::Exception& e ) {
+ php_error(E_WARNING, e.what());
+ } catch( InvalidParameterException& e ) {
+ php_error(E_WARNING, e.what());
+ } catch( PhpInvalidParameterException& e ) {
+ // The error message should be handled by PHP
+ } catch( std::bad_alloc& e ) {
+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "Memory allocation failed");
+ } catch( std::runtime_error& e ) {
+ php_error(E_ERROR, e.what());
+ } catch(...) {
+ php_error(E_ERROR, "An unknown error has occurred.");
+ }
+#endif
+}
+
bool mustache_parse_template_param(zval * tmpl, mustache::Mustache * mustache,
mustache::Node ** node TSRMLS_DC)
{
- std::string templateStr;
- std::string mtClassName("MustacheTemplate");
- zend_class_entry * tmp_ce;
- zend_class_entry * mtce;
- php_obj_MustacheTemplate * mtPayload;
-
// Prepare template string
if( Z_TYPE_P(tmpl) == IS_STRING ) {
-
- // Assign string
- templateStr.assign(Z_STRVAL_P(tmpl), (size_t) Z_STRLEN_P(tmpl));
-
// Tokenize template
- mustache->tokenize(&templateStr, *node);
-
+ char * tmpstr = Z_STRVAL_P(tmpl);
+ *node = new mustache::Node();
+ try {
+ std::string templateStr(tmpstr/*, (size_t) Z_STRLEN_P(tmpl)*/);
+ mustache->tokenize(&templateStr, *node);
+ } catch(...) {
+ delete *node; // Prevent leaks
+ throw;
+ }
return true;
} else if( Z_TYPE_P(tmpl) == IS_OBJECT ) {
-
// Use compiled template
- tmp_ce = Z_OBJCE_P(tmpl);
- mtce = mustache_get_class_entry((char *)mtClassName.c_str(), mtClassName.length() TSRMLS_CC);
+ std::string mtClassName("MustacheTemplate");
+ zend_class_entry * tmp_ce = Z_OBJCE_P(tmpl);
+ zend_class_entry * mtce = mustache_get_class_entry((char *)mtClassName.c_str(), mtClassName.length() TSRMLS_CC);
if( tmp_ce == NULL || mtce == NULL || tmp_ce != mtce ) {
php_error(E_WARNING, "Object not an instance of MustacheTemplate");
return false;
} else {
- mtPayload = (php_obj_MustacheTemplate *) zend_object_store_get_object(tmpl TSRMLS_CC);
+ php_obj_MustacheTemplate * mtPayload =
+ (php_obj_MustacheTemplate *) zend_object_store_get_object(tmpl TSRMLS_CC);
+ if( mtPayload->node == NULL ) {
+ if( mtPayload->tmpl == NULL ) {
+ php_error(E_WARNING, "Empty MustacheTemplate");
+ return false;
+ } else {
+ // This won't leak because the payload handles destruction
+ mtPayload->node = new mustache::Node();
+ mustache->tokenize(mtPayload->tmpl, mtPayload->node);
+ }
+ }
*node = mtPayload->node;
return true;
}
View
17 php_mustache.hpp
@@ -10,6 +10,10 @@ extern "C" {
#define PHP_MUSTACHE_BUILD "master"
#define PHP_MUSTACHE_AUTHORS "John Boehr <jbboehr@gmail.com> (lead)"
+#ifndef PHP_MUSTACHE_THROW_EXCEPTIONS
+#define PHP_MUSTACHE_THROW_EXCEPTIONS 0
+#endif
+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -33,7 +37,8 @@ extern "C" {
} // extern "C"
-#include <iostream>
+#include <exception>
+#include <stdexcept>
#include <string>
#include "mustache/mustache.hpp"
@@ -48,6 +53,15 @@ extern zend_module_entry mustache_module_entry;
PHP_MINIT_FUNCTION(mustache);
PHP_MINFO_FUNCTION(mustache);
+// Parameter exception
+class PhpInvalidParameterException : public std::exception {
+ public:
+};
+class InvalidParameterException : public std::runtime_error {
+ public:
+ InvalidParameterException(const std::string& desc) : std::runtime_error(desc) { }
+};
+
// Utils
void mustache_node_to_zval(mustache::Node * node, zval * current TSRMLS_DC);
@@ -56,6 +70,7 @@ zval * mustache_data_to_zval(mustache::Data * node TSRMLS_DC);
zend_class_entry * mustache_get_class_entry(char * name, int len TSRMLS_DC);
void mustache_error_handler(const char * msg, mustache::Exception * e,
zval * return_value TSRMLS_DC);
+void mustache_exception_handler();
bool mustache_parse_template_param(zval * tmpl, mustache::Mustache * mustache,
mustache::Node ** node TSRMLS_DC);
View
15 tests/MustacheTemplate__render.phpt
@@ -1,15 +0,0 @@
---TEST--
-MustacheTemplate::render() member function
---SKIPIF--
-<?php
-if( !extension_loaded('mustache') ) die('skip ');
- ?>
---FILE--
-<?php
-$tmpl = new MustacheTemplate('{{test}}');
-var_dump($tmpl->render(array(
- 'test' => 'okay'
-)));
-?>
---EXPECT--
-string(4) "okay"
View
4 tests/MustacheTemplate__toArray.phpt
@@ -6,10 +6,14 @@ if( !extension_loaded('mustache') ) die('skip ');
?>
--FILE--
<?php
+$m = new Mustache();
$tmpl = new MustacheTemplate('{{test}}');
+$r = $m->compile($tmpl);
+var_dump($r);
var_dump($tmpl->toArray());
?>
--EXPECT--
+bool(true)
array(3) {
["type"]=>
int(1)
Please sign in to comment.
Something went wrong with that request. Please try again.