Skip to content

Commit

Permalink
Adding exceptions, general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Mar 22, 2012
1 parent ff83f50 commit 9549b64
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions config.m4
Expand Up @@ -54,6 +54,7 @@ if test "$PHP_MUSTACHE" != "no"; then
PHP_MUSTACHE_ADD_SOURCES([ PHP_MUSTACHE_ADD_SOURCES([
php_mustache.cpp php_mustache.cpp
mustache_mustache.cpp mustache_mustache.cpp
mustache_exceptions.cpp
mustache_data.cpp mustache_data.cpp
mustache_template.cpp mustache_template.cpp
mustache/mustache.cpp mustache/mustache.cpp
Expand Down
2 changes: 2 additions & 0 deletions mustache.php
Expand Up @@ -131,3 +131,5 @@ public function __construct($data) {}
*/ */
public function toValue() {} public function toValue() {}
} }

class MustacheException extends Exception {}
15 changes: 11 additions & 4 deletions mustache_data.cpp
@@ -1,8 +1,15 @@


#include "php_mustache.hpp"
#include "mustache_data.hpp" #include "mustache_data.hpp"






// Class Entries --------------------------------------------------------------

PHPAPI zend_class_entry * MustacheData_ce_ptr = NULL;



// Method Entries -------------------------------------------------------------- // Method Entries --------------------------------------------------------------


static zend_function_entry MustacheData_methods[] = { static zend_function_entry MustacheData_methods[] = {
Expand All @@ -28,7 +35,7 @@ static void MustacheData_obj_free(void *object TSRMLS_DC)


efree(object); efree(object);
} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }


Expand All @@ -50,7 +57,7 @@ static zend_object_value MustacheData_obj_create(zend_class_entry *class_type TS
retval.handlers = &MustacheData_obj_handlers; retval.handlers = &MustacheData_obj_handlers;


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }


return retval; return retval;
Expand Down Expand Up @@ -108,7 +115,7 @@ PHP_METHOD(MustacheData, __construct)
mustache_data_from_zval(payload->data, data TSRMLS_CC); mustache_data_from_zval(payload->data, data TSRMLS_CC);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} __construct */ /* }}} __construct */
Expand Down Expand Up @@ -145,7 +152,7 @@ PHP_METHOD(MustacheData, toValue)
zval_copy_ctor(return_value); zval_copy_ctor(return_value);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} toValue */ /* }}} toValue */
5 changes: 3 additions & 2 deletions mustache_data.hpp
Expand Up @@ -2,10 +2,11 @@
#ifndef PHP_MUSTACHE_DATA_HPP #ifndef PHP_MUSTACHE_DATA_HPP
#define PHP_MUSTACHE_DATA_HPP #define PHP_MUSTACHE_DATA_HPP


#include "php_mustache.hpp"


#include "php.h"


static zend_class_entry * MustacheData_ce_ptr = NULL;
extern "C" PHPAPI zend_class_entry * MustacheData_ce_ptr;




typedef struct _php_obj_MustacheData { typedef struct _php_obj_MustacheData {
Expand Down
34 changes: 20 additions & 14 deletions mustache_mustache.cpp
Expand Up @@ -5,6 +5,12 @@






// Class Entries --------------------------------------------------------------

PHPAPI zend_class_entry * Mustache_ce_ptr = NULL;



// Method Entries -------------------------------------------------------------- // Method Entries --------------------------------------------------------------


static zend_function_entry Mustache_methods[] = { static zend_function_entry Mustache_methods[] = {
Expand Down Expand Up @@ -40,7 +46,7 @@ static void Mustache_obj_free(void *object TSRMLS_DC)
efree(object); efree(object);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }


Expand All @@ -63,7 +69,7 @@ static zend_object_value Mustache_obj_create(zend_class_entry *class_type TSRMLS
retval.handlers = &Mustache_obj_handlers; retval.handlers = &Mustache_obj_handlers;


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }


return retval; return retval;
Expand All @@ -86,7 +92,7 @@ PHP_MINIT_FUNCTION(mustache_mustache)


return SUCCESS; return SUCCESS;
} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
return FAILURE; return FAILURE;
} }
} }
Expand Down Expand Up @@ -114,7 +120,7 @@ PHP_METHOD(Mustache, __construct)
(php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC); (php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} __construct */ /* }}} __construct */
Expand Down Expand Up @@ -145,7 +151,7 @@ PHP_METHOD(Mustache, getEscapeByDefault)
} }


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} getEscapeByDefault */ /* }}} getEscapeByDefault */
Expand Down Expand Up @@ -173,7 +179,7 @@ PHP_METHOD(Mustache, getStartSequence)
RETURN_STRING(str.c_str(), 1); RETURN_STRING(str.c_str(), 1);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} getStartSequence */ /* }}} getStartSequence */
Expand Down Expand Up @@ -201,7 +207,7 @@ PHP_METHOD(Mustache, getStopSequence)
RETURN_STRING(str.c_str(), 1); RETURN_STRING(str.c_str(), 1);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} getStopSequence */ /* }}} getStopSequence */
Expand Down Expand Up @@ -232,7 +238,7 @@ PHP_METHOD(Mustache, setEscapeByDefault)
RETURN_TRUE; RETURN_TRUE;


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} setEscapeByDefault */ /* }}} setEscapeByDefault */
Expand Down Expand Up @@ -264,7 +270,7 @@ PHP_METHOD(Mustache, setStartSequence)
RETURN_TRUE; RETURN_TRUE;


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} setStartSequence */ /* }}} setStartSequence */
Expand Down Expand Up @@ -296,7 +302,7 @@ PHP_METHOD(Mustache, setStopSequence)
RETURN_TRUE; RETURN_TRUE;


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} setStartSequence */ /* }}} setStartSequence */
Expand Down Expand Up @@ -363,7 +369,7 @@ PHP_METHOD(Mustache, compile)
} }


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} compile */ /* }}} compile */
Expand Down Expand Up @@ -401,7 +407,7 @@ PHP_METHOD(Mustache, tokenize)
mustache_node_to_zval(&root, return_value TSRMLS_CC); mustache_node_to_zval(&root, return_value TSRMLS_CC);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} tokenize */ /* }}} tokenize */
Expand Down Expand Up @@ -457,7 +463,7 @@ PHP_METHOD(Mustache, render)
RETURN_STRING(output.c_str(), 1); // Yes reallocate RETURN_STRING(output.c_str(), 1); // Yes reallocate


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} render */ /* }}} render */
Expand Down Expand Up @@ -494,7 +500,7 @@ PHP_METHOD(Mustache, debugDataStructure)
zval_copy_ctor(return_value); zval_copy_ctor(return_value);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} debugDataStructure */ /* }}} debugDataStructure */
2 changes: 1 addition & 1 deletion mustache_mustache.hpp
Expand Up @@ -5,7 +5,7 @@
#include "php_mustache.hpp" #include "php_mustache.hpp"




static zend_class_entry * Mustache_ce_ptr = NULL; extern "C" PHPAPI zend_class_entry * Mustache_ce_ptr;




typedef struct _php_obj_Mustache { typedef struct _php_obj_Mustache {
Expand Down
20 changes: 13 additions & 7 deletions mustache_template.cpp
@@ -1,7 +1,13 @@


#include "php_mustache.hpp"
#include "mustache_data.hpp"
#include "mustache_template.hpp" #include "mustache_template.hpp"


#include "mustache_data.hpp"

// Class Entries --------------------------------------------------------------

PHPAPI zend_class_entry * MustacheTemplate_ce_ptr = NULL;






Expand Down Expand Up @@ -35,7 +41,7 @@ static void MustacheTemplate_obj_free(void *object TSRMLS_DC)
efree(object); efree(object);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }


Expand All @@ -58,7 +64,7 @@ static zend_object_value MustacheTemplate_obj_create(zend_class_entry *class_typ
retval.handlers = &MustacheTemplate_obj_handlers; retval.handlers = &MustacheTemplate_obj_handlers;


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }


return retval; return retval;
Expand All @@ -81,7 +87,7 @@ PHP_MINIT_FUNCTION(mustache_template)


return SUCCESS; return SUCCESS;
} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
return FAILURE; return FAILURE;
} }
} }
Expand Down Expand Up @@ -121,7 +127,7 @@ PHP_METHOD(MustacheTemplate, __construct)
payload->tmpl = new std::string(template_str/*, template_len*/); payload->tmpl = new std::string(template_str/*, template_len*/);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} __construct */ /* }}} __construct */
Expand Down Expand Up @@ -153,7 +159,7 @@ PHP_METHOD(MustacheTemplate, toArray)
mustache_node_to_zval(payload->node, return_value TSRMLS_CC); mustache_node_to_zval(payload->node, return_value TSRMLS_CC);


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} toArray */ /* }}} toArray */
Expand Down Expand Up @@ -185,7 +191,7 @@ PHP_METHOD(MustacheTemplate, __toString)
RETURN_STRING(payload->tmpl->c_str(), 1); // Yes reallocate RETURN_STRING(payload->tmpl->c_str(), 1); // Yes reallocate


} catch(...) { } catch(...) {
mustache_exception_handler(); mustache_exception_handler(TSRMLS_C);
} }
} }
/* }}} toArray */ /* }}} toArray */
5 changes: 3 additions & 2 deletions mustache_template.hpp
Expand Up @@ -2,10 +2,11 @@
#ifndef PHP_MUSTACHE_TEMPLATE_HPP #ifndef PHP_MUSTACHE_TEMPLATE_HPP
#define PHP_MUSTACHE_TEMPLATE_HPP #define PHP_MUSTACHE_TEMPLATE_HPP


#include "php_mustache.hpp"


#include "php.h"


static zend_class_entry * MustacheTemplate_ce_ptr = NULL;
extern "C" PHPAPI zend_class_entry * MustacheTemplate_ce_ptr;




typedef struct _php_obj_MustacheTemplate { typedef struct _php_obj_MustacheTemplate {
Expand Down
15 changes: 11 additions & 4 deletions php_mustache.cpp
@@ -1,7 +1,7 @@


#include "php_mustache.hpp" #include "php_mustache.hpp"

#include "mustache_mustache.hpp" #include "mustache_mustache.hpp"
#include "mustache_exceptions.hpp"
#include "mustache_data.hpp" #include "mustache_data.hpp"
#include "mustache_template.hpp" #include "mustache_template.hpp"


Expand Down Expand Up @@ -33,6 +33,7 @@ PHP_MINIT_FUNCTION(mustache)
PHP_MINIT(mustache_mustache)(INIT_FUNC_ARGS_PASSTHRU); PHP_MINIT(mustache_mustache)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(mustache_data)(INIT_FUNC_ARGS_PASSTHRU); PHP_MINIT(mustache_data)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(mustache_template)(INIT_FUNC_ARGS_PASSTHRU); PHP_MINIT(mustache_template)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(mustache_exceptions)(INIT_FUNC_ARGS_PASSTHRU);


return SUCCESS; return SUCCESS;
} }
Expand Down Expand Up @@ -280,16 +281,22 @@ zend_class_entry * mustache_get_class_entry(char * name, int len TSRMLS_DC)
} }
} }


void mustache_exception_handler() void mustache_exception_handler(TSRMLS_D)
{ {
#if PHP_MUSTACHE_THROW_EXCEPTIONS #if PHP_MUSTACHE_THROW_EXCEPTIONS
throw; throw;
#else #else
try { try {
throw; throw;
} catch( mustache::TokenizerException& e ) {
zval * exception = zend_throw_exception_ex(MustacheParserException_ce_ptr,
0 TSRMLS_CC, (char *) e.what(), "MustacheParserException");
zend_update_property_long(MustacheParserException_ce_ptr, exception, "templateLineNo", strlen("templateLineNo"), e.lineNo TSRMLS_CC);
zend_update_property_long(MustacheParserException_ce_ptr, exception, "templateCharNo", strlen("templateCharNo"), e.charNo TSRMLS_CC);
} catch( mustache::Exception& e ) { } catch( mustache::Exception& e ) {
// @todo change this to an exception zend_throw_exception_ex(MustacheException_ce_ptr, 0 TSRMLS_CC,
php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what()); (char *) e.what(), "MustacheException");
//php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what());
} catch( InvalidParameterException& e ) { } catch( InvalidParameterException& e ) {
// @todo change this to an exception // @todo change this to an exception
php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what()); php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what());
Expand Down
6 changes: 4 additions & 2 deletions php_mustache.hpp
Expand Up @@ -23,8 +23,10 @@ extern "C" {
#include <php_ini.h> #include <php_ini.h>
#include <SAPI.h> #include <SAPI.h>
#include <ext/standard/info.h> #include <ext/standard/info.h>
#include "zend_interfaces.h"
#include <Zend/zend_exceptions.h>
#include <Zend/zend_extensions.h> #include <Zend/zend_extensions.h>

#ifdef PHP_WIN32 #ifdef PHP_WIN32
#define PHP_FFTW_API __declspec(dllexport) #define PHP_FFTW_API __declspec(dllexport)
#else #else
Expand Down Expand Up @@ -68,7 +70,7 @@ void mustache_node_to_zval(mustache::Node * node, zval * current TSRMLS_DC);
void mustache_data_from_zval(mustache::Data * node, zval * current TSRMLS_DC); void mustache_data_from_zval(mustache::Data * node, zval * current TSRMLS_DC);
zval * mustache_data_to_zval(mustache::Data * node TSRMLS_DC); zval * mustache_data_to_zval(mustache::Data * node TSRMLS_DC);
zend_class_entry * mustache_get_class_entry(char * name, int len TSRMLS_DC); zend_class_entry * mustache_get_class_entry(char * name, int len TSRMLS_DC);
void mustache_exception_handler(); void mustache_exception_handler(TSRMLS_D);


bool mustache_parse_template_param(zval * tmpl, mustache::Mustache * mustache, bool mustache_parse_template_param(zval * tmpl, mustache::Mustache * mustache,
mustache::Node ** node TSRMLS_DC); mustache::Node ** node TSRMLS_DC);
Expand Down

0 comments on commit 9549b64

Please sign in to comment.