1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors:                                                             |
14    |          Israel Ekpo <iekpo@php.net>                                 |
15    |          Omar Shaban <omars@php.net>                                 |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php_solr.h"
20 
21 /* {{{ static void solr_prepare_internal_info(zval *object, zval *return_value) */
solr_prepare_internal_info(zval * object,zval * return_value)22 static void solr_prepare_internal_info(zval *object, zval *return_value)
23 {
24     zval *rv = NULL;
25 	zval *line_no = zend_read_property(Z_OBJCE_P(object), OBJ_FOR_PROP(object), SOLR_SOURCELINE_NO_PROPERTY_NAME, sizeof(SOLR_SOURCELINE_NO_PROPERTY_NAME)-1, 0, rv);
26 	zval *solr_file = zend_read_property(Z_OBJCE_P(object), OBJ_FOR_PROP(object), SOLR_SOURCEFILE_PROPERTY_NAME, sizeof(SOLR_SOURCEFILE_PROPERTY_NAME)-1, 0, rv);
27 	zval *solr_zif_name = zend_read_property(Z_OBJCE_P(object), OBJ_FOR_PROP(object), SOLR_ZIFNAME_PROPERTY_NAME, sizeof(SOLR_ZIFNAME_PROPERTY_NAME)-1, 0, rv);
28 	long int source_line = Z_LVAL_P(line_no);
29 	char *solr_source_file = Z_STRVAL_P(solr_file);
30 	char *solr_zifname = Z_STRVAL_P(solr_zif_name);
31 
32 	array_init(return_value);
33 
34 	add_assoc_long(return_value, SOLR_SOURCELINE_NO_PROPERTY_NAME, source_line);
35 	add_assoc_string(return_value, SOLR_SOURCEFILE_PROPERTY_NAME, solr_source_file);
36 	add_assoc_string(return_value, SOLR_ZIFNAME_PROPERTY_NAME, solr_zifname);
37 }
38 /* }}} */
39 
40 /* {{{ void solr_throw_exception_ex(zend_class_entry *exception_ce, long code, const char *filename, int file_line, const char *function_name, char *format, ...) */
solr_throw_exception_ex(zend_class_entry * exception_ce,long code,const char * filename,int file_line,const char * function_name,char * format,...)41 PHP_SOLR_API void solr_throw_exception_ex(zend_class_entry *exception_ce, long code, const char *filename, int file_line, const char *function_name, char *format, ...)
42 {
43 	char *message = NULL;
44 	zval object;
45 	zend_object *object_val = NULL;
46 
47 	va_list args;
48 
49 	va_start(args,format);
50 
51 	/* Preparing the message string from the format and variable argument list, if any. */
52 	ap_php_vasprintf(&message, format, args);
53 
54 	va_end(args);
55 
56 	/* Retrieves the thrown object and updates the properties */
57 	object_val = zend_throw_exception(exception_ce, message, code);
58 
59 	ZVAL_OBJ(&object, object_val);
60 
61 	/* This is the line number in the source file where it was thrown */
62 	zend_update_property_long(exception_ce, OBJ_FOR_PROP(&object), SOLR_SOURCELINE_NO_PROPERTY_NAME, sizeof(SOLR_SOURCELINE_NO_PROPERTY_NAME)-1, file_line);
63 
64 	/* This is the line source file where it was thrown */
65 	zend_update_property_string(exception_ce, OBJ_FOR_PROP(&object), SOLR_SOURCEFILE_PROPERTY_NAME, sizeof(SOLR_SOURCEFILE_PROPERTY_NAME)-1, (char *) filename);
66 
67 	/* This is the C function where it was thrown */
68 	zend_update_property_string(exception_ce, OBJ_FOR_PROP(&object), SOLR_ZIFNAME_PROPERTY_NAME, sizeof(SOLR_ZIFNAME_PROPERTY_NAME)-1, (char *) function_name);
69 
70 	/* message must be freed */
71 	if (message != NULL) {
72 
73 		free(message);
74 	}
75 }
76 /* }}} */
77 
78 /* {{{ void solr_throw_exception(zend_class_entry *exception_ce, char *message, long code, const char *filename, int file_line, const char *function_name) */
solr_throw_exception(zend_class_entry * exception_ce,char * message,long code,const char * filename,int file_line,const char * function_name)79 PHP_SOLR_API void solr_throw_exception(zend_class_entry *exception_ce, char *message, long code, const char *filename, int file_line, const char *function_name)
80 {
81 	/* Retrieves the thrown object and updates the properties */
82     zend_object *object_val = NULL;
83     zval object;
84 
85 	object_val = zend_throw_exception(exception_ce, message, code);
86 	ZVAL_OBJ(&object, object_val);
87 
88 	/* This is the line number in the source file where it was thrown */
89 	zend_update_property_long(exception_ce, OBJ_FOR_PROP(&object), SOLR_SOURCELINE_NO_PROPERTY_NAME, sizeof(SOLR_SOURCELINE_NO_PROPERTY_NAME)-1, file_line);
90 
91 	/* This is the line source file where it was thrown */
92 	zend_update_property_string(exception_ce, OBJ_FOR_PROP(&object), SOLR_SOURCEFILE_PROPERTY_NAME, sizeof(SOLR_SOURCEFILE_PROPERTY_NAME)-1, (char *) filename);
93 
94 	/* This is the C function where it was thrown */
95 	zend_update_property_string(exception_ce, OBJ_FOR_PROP(&object), SOLR_ZIFNAME_PROPERTY_NAME, sizeof(SOLR_ZIFNAME_PROPERTY_NAME)-1, (char *) function_name);
96 }
97 /* }}} */
98 
99 /* Macro for preparing the return value array */
100 #define solr_exception_return_internal_info() { \
101     solr_prepare_internal_info(getThis(), return_value); \
102 }
103 
104 /* {{{ proto array SolrException::getInternalInfo(void)
105    Returns the internal API information where the exception was generated */
PHP_METHOD(SolrException,getInternalInfo)106 PHP_METHOD(SolrException, getInternalInfo)
107 {
108     solr_exception_return_internal_info()
109 }
110 /* }}} */
111 
112 /* {{{ proto array SolrClientException::getInternalInfo(void)
113    Returns the internal API information where the exception was generated */
PHP_METHOD(SolrClientException,getInternalInfo)114 PHP_METHOD(SolrClientException, getInternalInfo)
115 {
116     solr_exception_return_internal_info()
117 }
118 /* }}} */
119 
120 /* {{{ proto array SolrIllegalOperationException::getInternalInfo(void)
121    Returns the internal API information where the exception was generated */
PHP_METHOD(SolrIllegalOperationException,getInternalInfo)122 PHP_METHOD(SolrIllegalOperationException, getInternalInfo)
123 {
124     solr_exception_return_internal_info()
125 }
126 /* }}} */
127 
128 /* {{{ proto array SolrIllegalArgumentException::getInternalInfo(void)
129    Returns the internal API information where the exception was generated */
PHP_METHOD(SolrIllegalArgumentException,getInternalInfo)130 PHP_METHOD(SolrIllegalArgumentException, getInternalInfo)
131 {
132     solr_exception_return_internal_info()
133 }
134 /* }}} */
135 
136 /* {{{ proto array SolrServerException::getInternalInfo(void)
137    Returns the internal API information where the exception was generated */
PHP_METHOD(SolrServerException,getInternalInfo)138 PHP_METHOD(SolrServerException, getInternalInfo)
139 {
140     solr_exception_return_internal_info()
141 }
142 /* }}} */
143 
144 /*
145  * Local variables:
146  * tab-width: 4
147  * c-basic-offset: 4
148  * indent-tabs-mode: t
149  * End:
150  * vim600: fdm=marker
151  * vim: noet sw=4 ts=4
152  */
153