Repository navigation
Expand file tree
/
Copy pathmustache_data.cpp
More file actions
555 lines (484 loc) · 16.7 KB
/
Copy pathmustache_data.cpp
File metadata and controls
555 lines (484 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <mustache/mustache.hpp>
#include <mustache/lambda.hpp>
#include "php_mustache.h"
#include "mustache_private.hpp"
#include <Zend/zend_closures.h>
#include "mustache_class_method_lambda.hpp"
#include "mustache_exceptions.hpp"
#include "mustache_lambda.hpp"
#include "mustache_zend_closure_lambda.hpp"
#include "mustache_data.hpp"
/* {{{ ZE2 OO definitions */
zend_class_entry * MustacheData_ce_ptr;
static zend_object_handlers MustacheData_obj_handlers;
/* }}} */
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(MustacheData____construct_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_INFO(0, tmpl)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(MustacheData__toValue_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ MustacheData_methods */
static zend_function_entry MustacheData_methods[] = {
PHP_ME(MustacheData, __construct, MustacheData____construct_args, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(MustacheData, toValue, MustacheData__toValue_args, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/* }}} */
/* {{{ php_mustache_data_object_fetch_object */
static inline struct php_obj_MustacheData * php_mustache_data_fetch_object(zend_object * obj)
{
return (struct php_obj_MustacheData *)((char*)(obj) - XtOffsetOf(struct php_obj_MustacheData, std));
}
struct php_obj_MustacheData * php_mustache_data_object_fetch_object(zval * zv)
{
return php_mustache_data_fetch_object(Z_OBJ_P(zv));
}
/* }}} */
/* {{{ MustacheData_obj_free */
static void MustacheData_obj_free(zend_object *object)
{
try {
struct php_obj_MustacheData * payload = php_mustache_data_fetch_object(object);
if( payload->data != NULL ) {
delete payload->data;
}
zend_object_std_dtor((zend_object *)object);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} */
/* {{{ MustacheData_obj_create */
static zend_object * MustacheData_obj_create(zend_class_entry * ce)
{
struct php_obj_MustacheData * intern;
try {
intern = (struct php_obj_MustacheData *) ecalloc(1, sizeof(struct php_obj_MustacheData) + zend_object_properties_size(ce));
zend_object_std_init(&intern->std, ce);
intern->std.handlers = &MustacheData_obj_handlers;
return &intern->std;
} catch(...) {
mustache_exception_handler();
}
return NULL;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(mustache_data)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "MustacheData", MustacheData_methods);
ce.create_object = MustacheData_obj_create;
MustacheData_ce_ptr = zend_register_internal_class(&ce);
memcpy(&MustacheData_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
MustacheData_obj_handlers.offset = XtOffsetOf(struct php_obj_MustacheData, std);
MustacheData_obj_handlers.free_obj = MustacheData_obj_free;
MustacheData_obj_handlers.clone_obj = NULL;
return SUCCESS;
}
/* }}} */
/* {{{ is_invokable_object */
static zend_always_inline bool is_invokable_object(const zend_class_entry * ce)
{
const HashTable * function_table = ce != NULL ? &ce->function_table : NULL;
return function_table != NULL && zend_hash_str_exists(function_table, "__invoke", strlen("__invoke"));
}
/* }}} is_invokable_object */
/* {{{ is_valid_function */
static zend_always_inline bool is_valid_function(const zend_function * f)
{
return (f->common.fn_flags & ZEND_ACC_STATIC) == 0 &&
#ifdef ZEND_ACC_CTOR
(f->common.fn_flags & ZEND_ACC_CTOR) == 0 &&
#endif
#ifdef ZEND_ACC_DTOR
(f->common.fn_flags & ZEND_ACC_DTOR) == 0 &&
#endif
(f->common.fn_flags & ZEND_ACC_PROTECTED) == 0 &&
(f->common.fn_flags & ZEND_ACC_PRIVATE) == 0;
}
/* }}} */
/* {{{ is_valid_property */
static zend_always_inline bool is_valid_property(const zend_property_info * prop)
{
return (prop->flags & ZEND_ACC_PROTECTED) == 0 &&
#ifdef ZEND_ACC_SHADOW
(prop->flags & ZEND_ACC_SHADOW) == 0 &&
#endif
(prop->flags & ZEND_ACC_PRIVATE) == 0;
}
/* }}} */
/* {{{ mustache_data_from_array_zval */
static zend_always_inline void mustache_data_from_array_zval(mustache::Data * node, zval * current)
{
HashTable * data_hash = NULL;
long data_count = 0;
zend_ulong key_nindex = 0;
zend_string * key;
std::string ckey;
zval * data_entry = NULL;
int length = 0;
mustache::Data * child = NULL;
node->type = mustache::Data::TypeNone;
data_hash = HASH_OF(current);
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) && ++data_hash->u.v.nApplyCount > 1 ) {
php_error(E_WARNING, "Data includes circular reference");
data_hash->u.v.nApplyCount--;
return;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(data_hash)) {
php_error(E_WARNING, "Data includes circular reference");
return;
}
GC_PROTECT_RECURSION(data_hash);
#endif
}
data_count = zend_hash_num_elements(data_hash);
ZEND_HASH_FOREACH_KEY_VAL_IND(data_hash, key_nindex, key, data_entry) {
(void)key_nindex; /* avoid [-Wunused-but-set-variable] */
if( !key ) {
if( node->type == mustache::Data::TypeNone ) {
node->init(mustache::Data::TypeArray, data_count);
} else if( node->type != mustache::Data::TypeArray ) {
php_error(E_WARNING, "Mixed numeric and associative arrays are not supported");
return; // EXIT
}
} else {
if( node->type == mustache::Data::TypeNone ) {
node->type = mustache::Data::TypeMap;
} else if( node->type != mustache::Data::TypeMap ) {
php_error(E_WARNING, "Mixed numeric and associative arrays are not supported");
return; // EXIT
}
}
// Store value
if( node->type == mustache::Data::TypeArray ) {
child = new mustache::Data();
mustache_data_from_zval(child, data_entry);
node->array.push_back(child);
node->length = ++length;
} else if( node->type == mustache::Data::TypeMap ) {
child = new mustache::Data;
mustache_data_from_zval(child, data_entry);
ckey.assign(ZSTR_VAL(key));
node->data.insert(std::pair<std::string,mustache::Data*>(ckey, child));
} else {
php_error(E_WARNING, "Weird data conflict");
// Whoops
}
} ZEND_HASH_FOREACH_END();
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) ) {
data_hash->u.v.nApplyCount--;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(data_hash);
#endif
}
}
/* }}} mustache_data_from_array_zval */
/* {{{ mustache_data_from_double_zval */
static zend_always_inline void mustache_data_from_double_zval(mustache::Data * node, zval * current)
{
char * double_as_string;
spprintf(&double_as_string, 0, "%.*G", (int) EG(precision), Z_DVAL_P(current));
node->type = mustache::Data::TypeString;
node->val = new std::string(double_as_string);
efree(double_as_string);
}
/* }}} */
/* {{{ mustache_data_from_object_properties_zval */
static zend_always_inline void mustache_data_from_object_properties_zval(mustache::Data * node, zval * current)
{
HashTable * data_hash = NULL;
zend_ulong key_nindex = 0;
zend_string * key;
std::string ckey;
zval * data_entry = NULL;
zend_class_entry * ce = Z_OBJCE_P(current);
mustache::Data * child = NULL;
zval * prop_zv;
zend_property_info * prop;
char * prop_name;
const char * class_name;
node->type = mustache::Data::TypeNone;
if( Z_OBJ_HT_P(current)->get_properties != NULL ) {
#if PHP_VERSION_ID >= 80000
data_hash = Z_OBJ_HT_P(current)->get_properties(Z_OBJ_P(current));
#else
data_hash = Z_OBJ_HT_P(current)->get_properties(current);
#endif
}
if( data_hash != NULL && zend_hash_num_elements(data_hash) > 0 ) {
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) && ++data_hash->u.v.nApplyCount > 1 ) {
php_error(E_WARNING, "Data includes circular reference");
data_hash->u.v.nApplyCount--;
return;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(data_hash)) {
php_error(E_WARNING, "Data includes circular reference");
return;
}
GC_PROTECT_RECURSION(data_hash);
#endif
}
ZEND_HASH_FOREACH_KEY_VAL_IND(data_hash, key_nindex, key, data_entry) {
(void)key_nindex; /* avoid [-Wunused-but-set-variable] */
if( key && ZSTR_LEN(key) && ZSTR_VAL(key)[0] ) { // skip private/protected
prop_name = ZSTR_VAL(key);
// defined properties must be public
// implicit properties won't be in properties_info, so they'll be assumed to be visible
bool is_visible = true;
if( ce != NULL && &ce->properties_info != NULL ) {
prop_zv = zend_hash_find(&ce->properties_info, key);
if( prop_zv != NULL ) {
prop = (zend_property_info *) Z_PTR_P(prop_zv);
is_visible = is_valid_property(prop);
zend_unmangle_property_name(prop->name, &class_name, (const char **) &prop_name);
}
}
if( is_visible ) {
node->type = mustache::Data::TypeMap;
child = new mustache::Data;
mustache_data_from_zval(child, data_entry);
ckey.assign(prop_name);
node->data.insert(std::pair<std::string,mustache::Data*>(ckey, child));
}
}
} ZEND_HASH_FOREACH_END();
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) ) {
data_hash->u.v.nApplyCount--;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(data_hash);
#endif
}
}
}
/* }}} mustache_data_from_object_properties_zval */
/* {{{ mustache_data_from_object_functions_zval */
static zend_always_inline void mustache_data_from_object_functions_zval(mustache::Data * node, zval * current)
{
HashTable * data_hash = NULL;
zend_ulong key_nindex = 0;
zend_string * key;
std::string ckey;
zval * data_entry = NULL;
zend_function * function_entry = NULL;
zend_class_entry * ce = Z_OBJCE_P(current);
mustache::Data * child = NULL;
if( ce != NULL ) {
data_hash = &ce->function_table;
}
if( data_hash != NULL && zend_hash_num_elements(data_hash) > 0 ) {
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) && ++data_hash->u.v.nApplyCount > 1 ) {
php_error(E_WARNING, "Data includes circular reference");
data_hash->u.v.nApplyCount--;
return;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(data_hash)) {
php_error(E_WARNING, "Data includes circular reference");
return;
}
GC_PROTECT_RECURSION(data_hash);
#endif
}
ZEND_HASH_FOREACH_KEY_VAL_IND(data_hash, key_nindex, key, data_entry) {
function_entry = (zend_function *) Z_PTR_P(data_entry);
(void)key; /* avoid [-Wunused-but-set-variable] */
(void)key_nindex;
if( is_valid_function(function_entry) ) {
node->type = mustache::Data::TypeMap;
ckey.assign(ZSTR_VAL(function_entry->common.function_name));
child = new mustache::Data();
child->type = mustache::Data::TypeLambda;
child->lambda = new ClassMethodLambda(current, ZSTR_VAL(function_entry->common.function_name));
node->data.insert(std::pair<std::string,mustache::Data*>(ckey,child));
}
} ZEND_HASH_FOREACH_END();
#if PHP_VERSION_ID < 70300
if( ZEND_HASH_APPLY_PROTECTION(data_hash) ) {
data_hash->u.v.nApplyCount--;
#else
if (!(GC_FLAGS(data_hash) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(data_hash);
#endif
}
}
}
/* }}} mustache_data_from_object_functions_zval */
/* {{{ mustache_data_from_object_zval */
static zend_always_inline void mustache_data_from_object_zval(mustache::Data * node, zval * current)
{
zend_class_entry * ce = Z_OBJCE_P(current);
node->type = mustache::Data::TypeNone;
if( ce == MustacheData_ce_ptr ) {
// @todo
php_error(E_WARNING, "MustacheData not implemented here");
} else if( ce == zend_ce_closure ) {
node->type = mustache::Data::TypeLambda;
node->lambda = new ZendClosureLambda(current);
} else if( is_invokable_object(ce) ) {
node->type = mustache::Data::TypeLambda;
node->lambda = new ClassMethodLambda(current, "__invoke");
} else {
// functions should take precendence over properties
mustache_data_from_object_properties_zval(node, current);
mustache_data_from_object_functions_zval(node, current);
}
}
/* }}} mustache_data_from_object_zval */
/* {{{ mustache_data_from_zval */
void mustache_data_from_zval(mustache::Data * node, zval * current)
{
if( Z_TYPE_P(current) == IS_INDIRECT ) {
current = Z_INDIRECT_P(current);
}
ZVAL_DEREF(current);
switch( Z_TYPE_P(current) ) {
case IS_NULL:
node->type = mustache::Data::TypeString;
node->val = new std::string();
break;
case IS_LONG:
node->type = mustache::Data::TypeString;
node->val = new std::string(std::to_string((long long)Z_LVAL_P(current)));
break;
case IS_TRUE:
node->type = mustache::Data::TypeString;
node->val = new std::string("1");
break;
case IS_FALSE:
node->type = mustache::Data::TypeString;
node->val = new std::string();
break;
case IS_DOUBLE:
mustache_data_from_double_zval(node, current);
break;
case IS_STRING:
node->type = mustache::Data::TypeString;
node->val = new std::string(Z_STRVAL_P(current)/*, (size_t) Z_STRLEN_P(current)*/);
break;
case IS_ARRAY:
mustache_data_from_array_zval(node, current);
break;
case IS_OBJECT:
mustache_data_from_object_zval(node, current);
break;
default:
php_error(E_WARNING, "Invalid data type: %d", Z_TYPE_P(current));
break;
}
}
/* }}} mustache_data_from_zval */
/* {{{ mustache_data_to_zval */
void mustache_data_to_zval(mustache::Data * node, zval * current)
{
mustache::Data::Array::iterator a_it;
mustache::Data::List::iterator l_it;
mustache::Data::Map::iterator m_it;
mustache::Data::Array childNode;
int pos = 0;
zval child = {0};
switch( node->type ) {
case mustache::Data::TypeNone:
ZVAL_NULL(current);
break;
case mustache::Data::TypeString:
ZVAL_STRINGL(current, node->val->c_str(), node->val->length());
break;
case mustache::Data::TypeArray:
array_init(current);
for( pos = 0; pos < node->length; pos++ ) {
ZVAL_NULL(&child);
mustache_data_to_zval(node->array[pos], &child);
add_next_index_zval(current, &child);
}
break;
case mustache::Data::TypeList:
array_init(current);
for ( l_it = node->children.begin() ; l_it != node->children.end(); l_it++ ) {
ZVAL_NULL(&child);
mustache_data_to_zval(*l_it, &child);
add_next_index_zval(current, &child);
}
break;
case mustache::Data::TypeMap:
array_init(current);
for ( m_it = node->data.begin() ; m_it != node->data.end(); m_it++ ) {
ZVAL_NULL(&child);
mustache_data_to_zval((*m_it).second, &child);
add_assoc_zval(current, (*m_it).first.c_str(), &child);
}
break;
default:
ZVAL_NULL(current);
php_error(E_WARNING, "Invalid data type");
break;
}
}
/* }}} mustache_data_to_zval */
/* {{{ proto void MustacheData::__construct() */
PHP_METHOD(MustacheData, __construct)
{
try {
// Custom parameters
zval * data = NULL;
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "Oz",
&_this_zval, MustacheData_ce_ptr, &data) == FAILURE) {
throw PhpInvalidParameterException();
}
// Class parameters
_this_zval = getThis();
struct php_obj_MustacheData * payload = php_mustache_data_object_fetch_object(_this_zval);
// Check if argument was given
if( data == NULL ) {
throw PhpInvalidParameterException();
}
// Convert data
payload->data = new mustache::Data();
mustache_data_from_zval(payload->data, data);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheData::__construct */
/* {{{ proto mixed MustacheData::toValue() */
PHP_METHOD(MustacheData, toValue)
{
try {
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
&_this_zval, MustacheData_ce_ptr) == FAILURE) {
throw PhpInvalidParameterException();
}
_this_zval = getThis();
struct php_obj_MustacheData * payload = php_mustache_data_object_fetch_object(_this_zval);
// Check if data was initialized
if( payload->data == NULL ) {
php_error_docref(NULL, E_WARNING, "MustacheData was not initialized properly");
RETURN_FALSE;
return;
}
// Reverse template data
mustache_data_to_zval(payload->data, return_value);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheData::toValue */