Skip to content

Commit 4f98cc5

Browse files
author
David Viner
committed
adding the xslt_set_object function (as per discussion on php-dev and
the newly created sab-php@gingerall.cz) --dviner
1 parent 1665272 commit 4f98cc5

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

ext/xslt/php_sablot.h

+3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ PHP_FUNCTION(xslt_process);
6262
PHP_FUNCTION(xslt_error);
6363
PHP_FUNCTION(xslt_errno);
6464
PHP_FUNCTION(xslt_free);
65+
PHP_FUNCTION(xslt_set_object);
6566
PHP_FUNCTION(xslt_backend_version);
6667
PHP_FUNCTION(xslt_backend_name);
6768

69+
6870
struct scheme_handlers {
6971
zval *get_all;
7072
zval *open;
@@ -112,6 +114,7 @@ typedef struct {
112114
struct xslt_handlers *handlers;
113115
struct xslt_processor processor;
114116
struct xslt_error *err;
117+
zval *object;
115118
} php_xslt;
116119

117120
#else

ext/xslt/php_xslt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern void xslt_free_arguments(xslt_args *);
5151

5252
extern void xslt_assign_handler(struct xslt_function **, zval **);
5353
extern void xslt_free_handler(struct xslt_function *);
54-
extern void xslt_call_function(char *, zval *, int, zval **, zval **);
54+
extern void xslt_call_function(char *, zval *, zval *, int, zval **, zval **);
5555

5656
extern void xslt_debug(char *, char *, ...);
5757

ext/xslt/sablot.c

+38-15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ static int le_xslt;
7373

7474
/* {{{ xslt_functions[]
7575
*/
76+
static unsigned char second_args_force_ref[] = { 2, BYREF_NONE, BYREF_FORCE };
77+
7678
function_entry xslt_functions[] = {
7779
PHP_FE(xslt_create, NULL)
7880
PHP_FE(xslt_set_sax_handlers, NULL)
@@ -87,6 +89,7 @@ function_entry xslt_functions[] = {
8789
PHP_FE(xslt_error, NULL)
8890
PHP_FE(xslt_errno, NULL)
8991
PHP_FE(xslt_free, NULL)
92+
PHP_FE(xslt_set_object, second_args_force_ref)
9093
PHP_FE(xslt_backend_version, NULL)
9194
PHP_FE(xslt_backend_name, NULL)
9295
{NULL, NULL, NULL}
@@ -182,6 +185,7 @@ PHP_FUNCTION(xslt_create)
182185
handle = ecalloc(1, sizeof(php_xslt));
183186
handle->handlers = ecalloc(1, sizeof(struct xslt_handlers));
184187
handle->err = ecalloc(1, sizeof(struct xslt_error));
188+
handle->object = NULL;
185189

186190
XSLT_LOG(handle).path = NULL;
187191

@@ -610,6 +614,25 @@ PHP_FUNCTION(xslt_free)
610614
}
611615
/* }}} */
612616

617+
/* {{{ proto int xslt_set_object(resource parser, object obj)
618+
sets the object in which to resolve callback functions */
619+
PHP_FUNCTION(xslt_set_object)
620+
{
621+
zval *processor_p; /* Resource pointer to a PHP-XSLT processor */
622+
zval *myobj; /* The object that will handle the callback */
623+
php_xslt *handle; /* A PHP-XSLT processor */
624+
625+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zo", &processor_p, &myobj) == FAILURE)
626+
return;
627+
628+
ZEND_FETCH_RESOURCE(handle, php_xslt *, &processor_p, -1, le_xslt_name, le_xslt);
629+
630+
handle->object = myobj;
631+
632+
RETURN_TRUE;
633+
}
634+
/* }}} */
635+
613636
/* {{{ proto void xslt_backend_version()
614637
Returns the version number of Sablotron (if available) */
615638
PHP_FUNCTION(xslt_backend_version)
@@ -742,7 +765,7 @@ static int scheme_getall(void *user_data, SablotHandle proc, const char *scheme,
742765
ZVAL_STRING(argv[1], (char *) scheme, 1);
743766
ZVAL_STRING(argv[2], (char *) rest, 1);
744767

745-
xslt_call_function("scheme get all", XSLT_SCHEME(handle).get_all,
768+
xslt_call_function("scheme get all", XSLT_SCHEME(handle).get_all, handle->object,
746769
3, argv, &retval);
747770

748771
if(!retval) {
@@ -826,7 +849,7 @@ static int scheme_open(void *user_data, SablotHandle proc, const char *scheme,
826849
ZVAL_STRING(argv[2], (char *) rest, 1);
827850

828851
/* Call the function */
829-
xslt_call_function("scheme open", XSLT_SCHEME(handle).open,
852+
xslt_call_function("scheme open", XSLT_SCHEME(handle).open, handle->object,
830853
3, argv, &retval);
831854

832855
if(!retval) {
@@ -880,7 +903,7 @@ static int scheme_get(void *user_data, SablotHandle proc, int fd, char *buffer,
880903
ZVAL_STRINGL(argv[2], buffer, *byte_count, 0);
881904

882905
/* Call the function */
883-
xslt_call_function("scheme get", XSLT_SCHEME(handle).get,
906+
xslt_call_function("scheme get", XSLT_SCHEME(handle).get, handle->object,
884907
3, argv, &retval);
885908

886909
if(!retval) {
@@ -929,7 +952,7 @@ static int scheme_put(void *user_data, SablotHandle proc, int fd, const char *b
929952
ZVAL_STRINGL(argv[2], (char *) buffer, *byte_count, 1);
930953

931954
/* Call the scheme put function already */
932-
xslt_call_function("scheme put", XSLT_SCHEME(handle).put,
955+
xslt_call_function("scheme put", XSLT_SCHEME(handle).put, handle->object,
933956
3, argv, &retval);
934957

935958
if(!retval) {
@@ -975,7 +998,7 @@ static int scheme_close(void *user_data, SablotHandle proc, int fd)
975998
zend_list_addref(fd);
976999

9771000
/* Call the scheme handler close function */
978-
xslt_call_function("scheme close", XSLT_SCHEME(handle).close,
1001+
xslt_call_function("scheme close", XSLT_SCHEME(handle).close, handle->object,
9791002
2, argv, &retval);
9801003

9811004
if(!retval) {
@@ -1013,7 +1036,7 @@ static SAX_RETURN sax_startdoc(void *ctx, SablotHandle processor)
10131036
zend_list_addref(handle->processor.idx);
10141037

10151038
/* Call the Sax start doc function */
1016-
xslt_call_function("sax start doc", XSLT_SAX(handle).doc_start,
1039+
xslt_call_function("sax start doc", XSLT_SAX(handle).doc_start, handle->object,
10171040
1, argv, &retval);
10181041

10191042
/* Cleanup */
@@ -1062,7 +1085,7 @@ static SAX_RETURN sax_startelement(void *ctx, SablotHandle processor,
10621085
}
10631086

10641087
/* Call the sax element start function */
1065-
xslt_call_function("sax start element", XSLT_SAX(handle).element_start,
1088+
xslt_call_function("sax start element", XSLT_SAX(handle).element_start, handle->object,
10661089
3, argv, &retval);
10671090

10681091
/* Cleanup */
@@ -1097,7 +1120,7 @@ static SAX_RETURN sax_endelement(void *ctx, SablotHandle processor, const char *
10971120
ZVAL_STRING(argv[1], (char *) name, 1);
10981121

10991122
/* Call the sax end element function */
1100-
xslt_call_function("sax end element", XSLT_SAX(handle).element_end,
1123+
xslt_call_function("sax end element", XSLT_SAX(handle).element_end, handle->object,
11011124
2, argv, &retval);
11021125

11031126
/* Cleanup */
@@ -1137,7 +1160,7 @@ static SAX_RETURN sax_startnamespace(void *ctx, SablotHandle processor,
11371160
ZVAL_STRING(argv[2], (char *) uri, 1);
11381161

11391162
/* Call the sax start namespace function */
1140-
xslt_call_function("sax start namespace", XSLT_SAX(handle).namespace_start,
1163+
xslt_call_function("sax start namespace", XSLT_SAX(handle).namespace_start, handle->object,
11411164
3, argv, &retval);
11421165

11431166
/* Cleanup */
@@ -1172,7 +1195,7 @@ static SAX_RETURN sax_endnamespace(void *ctx, SablotHandle processor, const char
11721195
ZVAL_STRING(argv[1], (char *) prefix, 1);
11731196

11741197
/* Call the sax end namespace function */
1175-
xslt_call_function("sax end namespace", XSLT_SAX(handle).namespace_end,
1198+
xslt_call_function("sax end namespace", XSLT_SAX(handle).namespace_end, handle->object,
11761199
2, argv, &retval);
11771200

11781201
/* Cleanup */
@@ -1207,7 +1230,7 @@ static SAX_RETURN sax_comment(void *ctx, SablotHandle processor, const char *con
12071230
ZVAL_STRING(argv[1], (char *) contents, 1);
12081231

12091232
/* Call the sax comment function */
1210-
xslt_call_function("sax comment", XSLT_SAX(handle).comment,
1233+
xslt_call_function("sax comment", XSLT_SAX(handle).comment, handle->object,
12111234
2, argv, &retval);
12121235

12131236
/* Cleanup */
@@ -1247,7 +1270,7 @@ static SAX_RETURN sax_pi(void *ctx, SablotHandle processor,
12471270
ZVAL_STRING(argv[2], (char *) contents, 1);
12481271

12491272
/* Call processing instructions function */
1250-
xslt_call_function("sax processing instructions", XSLT_SAX(handle).pi,
1273+
xslt_call_function("sax processing instructions", XSLT_SAX(handle).pi, handle->object,
12511274
3, argv, &retval);
12521275

12531276
/* Cleanup */
@@ -1284,7 +1307,7 @@ static SAX_RETURN sax_characters(void *ctx, SablotHandle processor,
12841307
ZVAL_STRINGL(argv[1], (char *) contents, length, 1);
12851308

12861309
/* Call characters function */
1287-
xslt_call_function("sax characters", XSLT_SAX(handle).characters,
1310+
xslt_call_function("sax characters", XSLT_SAX(handle).characters, handle->object,
12881311
2, argv, &retval);
12891312

12901313
/* Cleanup */
@@ -1316,7 +1339,7 @@ static SAX_RETURN sax_enddoc(void *ctx, SablotHandle processor)
13161339
zend_list_addref(handle->processor.idx);
13171340

13181341
/* Call the function */
1319-
xslt_call_function("sax end doc", XSLT_SAX(handle).doc_end,
1342+
xslt_call_function("sax end doc", XSLT_SAX(handle).doc_end, handle->object,
13201343
1, argv, &retval);
13211344

13221345
/* Cleanup */
@@ -1535,7 +1558,7 @@ static MH_ERROR error_print(void *user_data, SablotHandle proc, MH_ERROR code, M
15351558
}
15361559

15371560
/* Call the function */
1538-
xslt_call_function("error handler", XSLT_ERROR(handle),
1561+
xslt_call_function("error handler", XSLT_ERROR(handle), handle->object,
15391562
4, argv, &retval);
15401563

15411564
/* Free up */

ext/xslt/xslt.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ extern void xslt_free_arguments(xslt_args *to_free)
213213
Call an XSLT handler */
214214
extern void xslt_call_function(char *name,
215215
zval *function,
216+
zval *object,
216217
int argc,
217218
zval **user_args,
218219
zval **retval)
@@ -227,10 +228,21 @@ extern void xslt_call_function(char *name,
227228
argv[idx] = &user_args[idx];
228229
}
229230

230-
/* Call the function */
231-
error = call_user_function_ex(EG(function_table),
232-
NULL, function,
233-
retval, argc, argv, 0, NULL TSRMLS_CC);
231+
232+
/* Call the function (with object when appropriate)*/
233+
if (object == NULL)
234+
{
235+
error = call_user_function_ex(EG(function_table),
236+
NULL, function,
237+
retval, argc, argv, 0, NULL TSRMLS_CC);
238+
}
239+
else
240+
{
241+
error = call_user_function_ex(EG(function_table),
242+
&object, function,
243+
retval, argc, argv, 0, NULL TSRMLS_CC);
244+
}
245+
234246
if (error == FAILURE) {
235247
php_error(E_WARNING, "Cannot call the %s handler: %s",
236248
name, Z_STRVAL_P(function));

0 commit comments

Comments
 (0)