Permalink
Browse files

Adding exceptions, general cleanup

  • Loading branch information...
1 parent ff83f50 commit 9549b64bcb70b13d82c5ac6dee2c108a06324c6e @jbboehr committed Mar 22, 2012
Showing with 69 additions and 36 deletions.
  1. +1 −0 config.m4
  2. +2 −0 mustache.php
  3. +11 −4 mustache_data.cpp
  4. +3 −2 mustache_data.hpp
  5. +20 −14 mustache_mustache.cpp
  6. +1 −1 mustache_mustache.hpp
  7. +13 −7 mustache_template.cpp
  8. +3 −2 mustache_template.hpp
  9. +11 −4 php_mustache.cpp
  10. +4 −2 php_mustache.hpp
View
1 config.m4
@@ -54,6 +54,7 @@ if test "$PHP_MUSTACHE" != "no"; then
PHP_MUSTACHE_ADD_SOURCES([
php_mustache.cpp
mustache_mustache.cpp
+ mustache_exceptions.cpp
mustache_data.cpp
mustache_template.cpp
mustache/mustache.cpp
View
2 mustache.php
@@ -131,3 +131,5 @@ public function __construct($data) {}
*/
public function toValue() {}
}
+
+class MustacheException extends Exception {}
View
15 mustache_data.cpp
@@ -1,8 +1,15 @@
+#include "php_mustache.hpp"
#include "mustache_data.hpp"
+// Class Entries --------------------------------------------------------------
+
+PHPAPI zend_class_entry * MustacheData_ce_ptr = NULL;
+
+
+
// Method Entries --------------------------------------------------------------
static zend_function_entry MustacheData_methods[] = {
@@ -28,7 +35,7 @@ static void MustacheData_obj_free(void *object TSRMLS_DC)
efree(object);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
@@ -50,7 +57,7 @@ static zend_object_value MustacheData_obj_create(zend_class_entry *class_type TS
retval.handlers = &MustacheData_obj_handlers;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
return retval;
@@ -108,7 +115,7 @@ PHP_METHOD(MustacheData, __construct)
mustache_data_from_zval(payload->data, data TSRMLS_CC);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} __construct */
@@ -145,7 +152,7 @@ PHP_METHOD(MustacheData, toValue)
zval_copy_ctor(return_value);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} toValue */
View
5 mustache_data.hpp
@@ -2,10 +2,11 @@
#ifndef 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 {
View
34 mustache_mustache.cpp
@@ -5,6 +5,12 @@
+// Class Entries --------------------------------------------------------------
+
+PHPAPI zend_class_entry * Mustache_ce_ptr = NULL;
+
+
+
// Method Entries --------------------------------------------------------------
static zend_function_entry Mustache_methods[] = {
@@ -40,7 +46,7 @@ static void Mustache_obj_free(void *object TSRMLS_DC)
efree(object);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
@@ -63,7 +69,7 @@ static zend_object_value Mustache_obj_create(zend_class_entry *class_type TSRMLS
retval.handlers = &Mustache_obj_handlers;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
return retval;
@@ -86,7 +92,7 @@ PHP_MINIT_FUNCTION(mustache_mustache)
return SUCCESS;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
return FAILURE;
}
}
@@ -114,7 +120,7 @@ PHP_METHOD(Mustache, __construct)
(php_obj_Mustache *) zend_object_store_get_object(_this_zval TSRMLS_CC);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} __construct */
@@ -145,7 +151,7 @@ PHP_METHOD(Mustache, getEscapeByDefault)
}
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} getEscapeByDefault */
@@ -173,7 +179,7 @@ PHP_METHOD(Mustache, getStartSequence)
RETURN_STRING(str.c_str(), 1);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} getStartSequence */
@@ -201,7 +207,7 @@ PHP_METHOD(Mustache, getStopSequence)
RETURN_STRING(str.c_str(), 1);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} getStopSequence */
@@ -232,7 +238,7 @@ PHP_METHOD(Mustache, setEscapeByDefault)
RETURN_TRUE;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} setEscapeByDefault */
@@ -264,7 +270,7 @@ PHP_METHOD(Mustache, setStartSequence)
RETURN_TRUE;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} setStartSequence */
@@ -296,7 +302,7 @@ PHP_METHOD(Mustache, setStopSequence)
RETURN_TRUE;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} setStartSequence */
@@ -363,7 +369,7 @@ PHP_METHOD(Mustache, compile)
}
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} compile */
@@ -401,7 +407,7 @@ PHP_METHOD(Mustache, tokenize)
mustache_node_to_zval(&root, return_value TSRMLS_CC);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} tokenize */
@@ -457,7 +463,7 @@ PHP_METHOD(Mustache, render)
RETURN_STRING(output.c_str(), 1); // Yes reallocate
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} render */
@@ -494,7 +500,7 @@ PHP_METHOD(Mustache, debugDataStructure)
zval_copy_ctor(return_value);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} debugDataStructure */
View
2 mustache_mustache.hpp
@@ -5,7 +5,7 @@
#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 {
View
20 mustache_template.cpp
@@ -1,7 +1,13 @@
+#include "php_mustache.hpp"
+#include "mustache_data.hpp"
#include "mustache_template.hpp"
-#include "mustache_data.hpp"
+
+
+// Class Entries --------------------------------------------------------------
+
+PHPAPI zend_class_entry * MustacheTemplate_ce_ptr = NULL;
@@ -35,7 +41,7 @@ static void MustacheTemplate_obj_free(void *object TSRMLS_DC)
efree(object);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
@@ -58,7 +64,7 @@ static zend_object_value MustacheTemplate_obj_create(zend_class_entry *class_typ
retval.handlers = &MustacheTemplate_obj_handlers;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
return retval;
@@ -81,7 +87,7 @@ PHP_MINIT_FUNCTION(mustache_template)
return SUCCESS;
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
return FAILURE;
}
}
@@ -121,7 +127,7 @@ PHP_METHOD(MustacheTemplate, __construct)
payload->tmpl = new std::string(template_str/*, template_len*/);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} __construct */
@@ -153,7 +159,7 @@ PHP_METHOD(MustacheTemplate, toArray)
mustache_node_to_zval(payload->node, return_value TSRMLS_CC);
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} toArray */
@@ -185,7 +191,7 @@ PHP_METHOD(MustacheTemplate, __toString)
RETURN_STRING(payload->tmpl->c_str(), 1); // Yes reallocate
} catch(...) {
- mustache_exception_handler();
+ mustache_exception_handler(TSRMLS_C);
}
}
/* }}} toArray */
View
5 mustache_template.hpp
@@ -2,10 +2,11 @@
#ifndef 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 {
View
15 php_mustache.cpp
@@ -1,7 +1,7 @@
#include "php_mustache.hpp"
-
#include "mustache_mustache.hpp"
+#include "mustache_exceptions.hpp"
#include "mustache_data.hpp"
#include "mustache_template.hpp"
@@ -33,6 +33,7 @@ PHP_MINIT_FUNCTION(mustache)
PHP_MINIT(mustache_mustache)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(mustache_data)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(mustache_template)(INIT_FUNC_ARGS_PASSTHRU);
+ PHP_MINIT(mustache_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
return SUCCESS;
}
@@ -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
throw;
#else
try {
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 ) {
- // @todo change this to an exception
- php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what());
+ zend_throw_exception_ex(MustacheException_ce_ptr, 0 TSRMLS_CC,
+ (char *) e.what(), "MustacheException");
+ //php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what());
} catch( InvalidParameterException& e ) {
// @todo change this to an exception
php_error_docref(NULL TSRMLS_CC, E_WARNING, e.what());
View
6 php_mustache.hpp
@@ -23,8 +23,10 @@ extern "C" {
#include <php_ini.h>
#include <SAPI.h>
#include <ext/standard/info.h>
+#include "zend_interfaces.h"
+#include <Zend/zend_exceptions.h>
#include <Zend/zend_extensions.h>
-
+
#ifdef PHP_WIN32
#define PHP_FFTW_API __declspec(dllexport)
#else
@@ -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);
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_exception_handler();
+void mustache_exception_handler(TSRMLS_D);
bool mustache_parse_template_param(zval * tmpl, mustache::Mustache * mustache,
mustache::Node ** node TSRMLS_DC);

0 comments on commit 9549b64

Please sign in to comment.