• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jbboehr / php-mustache / 11081581606

28 Sep 2024 05:56AM UTC coverage: 75.922%. Remained the same
11081581606

push

github

jbboehr
try setting a prefix on osx

741 of 976 relevant lines covered (75.92%)

183.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.74
/mustache_ast.cpp
1

2
#ifdef HAVE_CONFIG_H
3
#include "config.h"
4
#endif
5

6
#include "php_mustache.h"
7
#include "mustache_private.hpp"
8
#include "mustache_exceptions.hpp"
9
#include "mustache_ast.hpp"
10

11
/* {{{ ZE2 OO definitions */
12
zend_class_entry * MustacheAST_ce_ptr;
13
static zend_object_handlers MustacheAST_obj_handlers;
14
/* }}} */
15

16
/* {{{ arginfo */
17
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____construct_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
18
    ZEND_ARG_INFO(0, vars)
19
ZEND_END_ARG_INFO()
20

21
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____sleep_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
22
ZEND_END_ARG_INFO()
23

24
ZEND_BEGIN_ARG_INFO_EX(MustacheAST__toArray_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
25
ZEND_END_ARG_INFO()
26

27
#if PHP_VERSION_ID >= 80200
28
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(MustacheAST____toString_args, 0, 0, IS_STRING, 0)
29
#else
30
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____toString_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
31
#endif
32
ZEND_END_ARG_INFO()
33

34
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____wakeup_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
35
ZEND_END_ARG_INFO()
36
/* }}} */
37

38
/* {{{ MustacheAST_methods */
39
static zend_function_entry MustacheAST_methods[] = {
40
  PHP_ME(MustacheAST, __construct, MustacheAST____construct_args, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
41
  PHP_ME(MustacheAST, __sleep, MustacheAST____sleep_args, ZEND_ACC_PUBLIC)
42
  PHP_ME(MustacheAST, toArray, MustacheAST__toArray_args, ZEND_ACC_PUBLIC)
43
  PHP_ME(MustacheAST, __toString, MustacheAST____toString_args, ZEND_ACC_PUBLIC)
44
  PHP_ME(MustacheAST, __wakeup, MustacheAST____wakeup_args, ZEND_ACC_PUBLIC)
45
  { NULL, NULL, NULL }
46
};
47
/* }}} */
48

49
/* {{{ mustache_node_from_binary_string */
50
void mustache_node_from_binary_string(mustache::Node ** node, char * str, int len)
20✔
51
{
52
  std::vector<uint8_t> uint_str;
20✔
53
  uint_str.resize(len);
20✔
54
  int i = 0;
20✔
55
  for( i = 0; i < len; i++ ) {
680✔
56
    uint_str[i] = str[i];
660✔
57
  }
58

59
  size_t vpos = 0;
20✔
60
  *node = mustache::Node::unserialize(uint_str, 0, &vpos);
20✔
61
}
20✔
62
/* }}} */
63

64
/* {{{ mustache_node_to_binary_string */
65
void mustache_node_to_binary_string(mustache::Node * node, char ** estr, int * elen)
16✔
66
{
67
  std::vector<uint8_t> * serialPtr = node->serialize();
16✔
68
  std::vector<uint8_t> & serial = *serialPtr;
16✔
69
  int serialLen = serial.size();
16✔
70

71
  char * str = (char *) emalloc(sizeof(char *) * (serialLen + 1));
16✔
72
  for( int i = 0 ; i < serialLen; i++ ) {
544✔
73
    str[i] = (char) serial[i];
528✔
74
  }
75
  str[serialLen] = '\0';
16✔
76
  delete serialPtr;
16✔
77

78
  *elen = serialLen;
16✔
79
  *estr = str;
16✔
80
}
16✔
81
/* }}} */
82

83
/* {{{ mustache_node_to_zval */
84
void mustache_node_to_zval(mustache::Node * node, zval * current)
16✔
85
{
86
  zval children = {0};
16✔
87
  zval child = {0};
16✔
88

89
  array_init(current);
16✔
90

91
  // Basic data
92
  add_assoc_long(current, "type", node->type);
16✔
93
  add_assoc_long(current, "flags", node->flags);
16✔
94
  if( NULL != node->data && node->data->length() > 0 ) {
16✔
95
    add_assoc_stringl_ex(current, ZEND_STRL("data"), (char *) node->data->c_str(), node->data->length());
8✔
96
  }
97

98
  // Children
99
  if( node->children.size() > 0 ) {
16✔
100
    ZVAL_NULL(&children);
8✔
101
    array_init(&children);
8✔
102

103
    mustache::Node::Children::iterator it;
8✔
104
    for ( it = node->children.begin() ; it != node->children.end(); it++ ) {
16✔
105
      ZVAL_NULL(&child);
8✔
106
      mustache_node_to_zval(*it, &child);
8✔
107
      add_next_index_zval(&children, &child);
108
    }
109

110
    add_assoc_zval(current, "children", &children);
2✔
111
  }
112

113
  // Partials
114
  if( node->partials.size() > 0 ) {
16✔
115
    ZVAL_NULL(&children);
×
116
    array_init(&children);
×
117

118
    mustache::Node::Partials::iterator it;
×
119
    for ( it = node->partials.begin() ; it != node->partials.end(); it++ ) {
×
120
      ZVAL_NULL(&child);
×
121
      mustache_node_to_zval(&(it->second), &child);
×
122
      add_assoc_zval(&children, it->first.c_str(), &child);
×
123
    }
124

125
    add_assoc_zval(current, "partials", &children);
126
  }
127
}
16✔
128
/* }}} */
129

130
/* {{{ php_mustache_ast_object_fetch_object */
131
static inline struct php_obj_MustacheAST * php_mustache_ast_fetch_object(zend_object * obj)
92✔
132
{
133
  return (struct php_obj_MustacheAST *)((char*)(obj) - XtOffsetOf(struct php_obj_MustacheAST, std));
92✔
134
}
135

136
struct php_obj_MustacheAST * php_mustache_ast_object_fetch_object(zval * zv)
60✔
137
{
138
  return php_mustache_ast_fetch_object(Z_OBJ_P(zv));
60✔
139
}
140
/* }}} */
141

142
/* {{{ MustacheAST_obj_free */
143
static void MustacheAST_obj_free(zend_object * object)
32✔
144
{
145
  try {
146
    struct php_obj_MustacheAST * payload = php_mustache_ast_fetch_object(object);
32✔
147

148
    if( payload->node != NULL ) {
32✔
149
      delete payload->node;
32✔
150
    }
151

152
    zend_object_std_dtor((zend_object *)object);
32✔
153
  } catch(...) {
×
154
    mustache_exception_handler();
×
155
  }
×
156
}
32✔
157
/* }}} */
158

159
/* {{{ MustacheAST_obj_create */
160
static zend_object * MustacheAST_obj_create(zend_class_entry * ce)
32✔
161
{
162
  struct php_obj_MustacheAST * intern;
163

164
  try {
165
    intern = (struct php_obj_MustacheAST *) ecalloc(1, sizeof(struct php_obj_MustacheAST) + zend_object_properties_size(ce));
32✔
166
    zend_object_std_init(&intern->std, ce);
32✔
167
    intern->std.handlers = &MustacheAST_obj_handlers;
32✔
168
    intern->node = NULL;
32✔
169
    return &intern->std;
32✔
170
  } catch(...) {
×
171
    mustache_exception_handler();
×
172
  }
×
173

174
  return NULL;
×
175
}
176
/* }}} */
177

178
/* {{{ PHP_MINIT_FUNCTION */
179
PHP_MINIT_FUNCTION(mustache_ast)
737✔
180
{
181
  try {
182
    zend_class_entry ce;
183

184
    memcpy(&MustacheAST_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
737✔
185
    MustacheAST_obj_handlers.offset = XtOffsetOf(struct php_obj_MustacheAST, std);
737✔
186
    MustacheAST_obj_handlers.free_obj = MustacheAST_obj_free;
737✔
187
    MustacheAST_obj_handlers.clone_obj = NULL;
737✔
188

189
    INIT_CLASS_ENTRY(ce, "MustacheAST", MustacheAST_methods);
737✔
190
    ce.create_object = MustacheAST_obj_create;
737✔
191

192
    MustacheAST_ce_ptr = zend_register_internal_class(&ce);
737✔
193
    MustacheAST_ce_ptr->create_object = MustacheAST_obj_create;
737✔
194

195
    zend_declare_property_null(MustacheAST_ce_ptr, ZEND_STRL("binaryString"), ZEND_ACC_PROTECTED);
737✔
196

197
    return SUCCESS;
737✔
198
  } catch(...) {
×
199
    mustache_exception_handler();
×
200
    return FAILURE;
×
201
  }
×
202
}
203
/* }}} */
204

205
/* {{{ proto void MustacheAST::__construct(string binaryString) */
206
PHP_METHOD(MustacheAST, __construct)
16✔
207
{
208
  try {
209
    // Custom parameters
210
    char * str = NULL;
16✔
211
    long str_len = 0;
16✔
212

213
    // Check parameters
214
    zval * _this_zval = NULL;
16✔
215
    if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O|s",
32✔
216
            &_this_zval, MustacheAST_ce_ptr, &str, &str_len) == FAILURE) {
16✔
217
      throw PhpInvalidParameterException();
×
218
    }
219

220
    // Class parameters
221
    _this_zval = getThis();
32✔
222
    struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
16✔
223

224
    // Check payload
225
    if( payload->node != NULL ) {
16✔
226
      throw InvalidParameterException("MustacheAST is already initialized");
×
227
    }
228

229
    // Unserialize
230
    mustache_node_from_binary_string(&payload->node, str, str_len);
16✔
231

232
  } catch(...) {
×
233
    mustache_exception_handler();
×
234
  }
×
235
}
16✔
236
/* }}} MustacheAST::__construct */
237

238
/* {{{ proto void MustacheAST::__sleep() */
239
PHP_METHOD(MustacheAST, __sleep)
4✔
240
{
241
  try {
242
    // Check parameters
243
    zval * _this_zval = NULL;
4✔
244
    if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
8✔
245
            &_this_zval, MustacheAST_ce_ptr) == FAILURE) {
4✔
246
      throw PhpInvalidParameterException();
×
247
    }
248

249
    // Class parameters
250
    _this_zval = getThis();
8✔
251
    struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
4✔
252

253
    array_init(return_value);
4✔
254

255
    // Check payload
256
    if( payload->node != NULL ) {
4✔
257
      // Serialize and store
258
      char * str = NULL;
4✔
259
      int len = 0;
4✔
260
      mustache_node_to_binary_string(payload->node, &str, &len);
4✔
261
      if( str != NULL ) {
4✔
262
#if PHP_VERSION_ID < 80000
263
        zend_update_property_stringl(MustacheAST_ce_ptr, _this_zval, ZEND_STRL("binaryString"), str, len);
264
#else
265
        zend_update_property_stringl(MustacheAST_ce_ptr, Z_OBJ_P(_this_zval), ZEND_STRL("binaryString"), str, len);
4✔
266
#endif
267
        add_next_index_string(return_value, "binaryString");
4✔
268
        efree(str);
4✔
269
      }
270
    }
271

272
  } catch(...) {
×
273
    mustache_exception_handler();
×
274
  }
×
275
}
4✔
276
/* }}} MustacheAST::__sleep */
277

278
/* {{{ proto array MustacheAST::toArray() */
279
PHP_METHOD(MustacheAST, toArray)
4✔
280
{
281
  try {
282
    // Check parameters
283
    zval * _this_zval = NULL;
4✔
284
    if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
8✔
285
            &_this_zval, MustacheAST_ce_ptr) == FAILURE) {
4✔
286
      throw PhpInvalidParameterException();
×
287
    }
288

289
    // Class parameters
290
    _this_zval = getThis();
8✔
291
    struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
4✔
292

293
    // Check payload
294
    if( payload->node == NULL ) {
4✔
295
      throw InvalidParameterException("MustacheAST was not initialized properly");
×
296
    }
297

298
    // Convert to PHP array
299
    mustache_node_to_zval(payload->node, return_value);
4✔
300

301
  } catch(...) {
×
302
    mustache_exception_handler();
×
303
  }
×
304
}
4✔
305
/* }}} MustacheAST::toArray */
306

307
/* {{{ proto string MustacheAST::__toString() */
308
PHP_METHOD(MustacheAST, __toString)
12✔
309
{
310
  try {
311
    // Check parameters
312
    zval * _this_zval = NULL;
12✔
313
    if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
24✔
314
            &_this_zval, MustacheAST_ce_ptr) == FAILURE) {
12✔
315
      throw PhpInvalidParameterException();
×
316
    }
317

318
    // Class parameters
319
    _this_zval = getThis();
24✔
320
    struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
12✔
321

322
    // Check payload
323
    if( payload->node == NULL ) {
12✔
324
      throw InvalidParameterException("MustacheAST was not initialized properly");
×
325
    }
326

327
    // Convert to PHP binary string
328
    char * str = NULL;
12✔
329
    int len = 0;
12✔
330
    mustache_node_to_binary_string(payload->node, &str, &len);
12✔
331

332
    if( str != NULL ) {
12✔
333
      RETVAL_STRINGL(str, len);
12✔
334
      efree(str);
12✔
335
    }
336

337
  } catch(...) {
×
338
    mustache_exception_handler();
×
339
  }
×
340
}
12✔
341
/* }}} MustacheAST::__toString */
342

343
/* {{{ proto void MustacheAST::__wakeup() */
344
static inline void php_mustache_ast_wakeup(zval * _this_zval, zval * return_value)
4✔
345
{
346
    zval rv;
347
    struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
4✔
348
#if PHP_VERSION_ID < 80000
349
    zval * value = zend_read_property(Z_OBJCE_P(_this_zval), _this_zval, "binaryString", sizeof("binaryString")-1, 1, &rv);
350
#else
351
    zval * value = zend_read_property(Z_OBJCE_P(_this_zval), Z_OBJ_P(_this_zval), "binaryString", sizeof("binaryString")-1, 1, &rv);
4✔
352
#endif
353

354
    if( Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) > 0 ) {
4✔
355
            mustache_node_from_binary_string(&payload->node, Z_STRVAL_P(value), Z_STRLEN_P(value));
4✔
356
    }
357
}
4✔
358

359
PHP_METHOD(MustacheAST, __wakeup)
4✔
360
{
361
  try {
362
    // Check parameters
363
    zval * _this_zval = NULL;
4✔
364
    if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
8✔
365
            &_this_zval, MustacheAST_ce_ptr) == FAILURE) {
4✔
366
      throw PhpInvalidParameterException();
×
367
    }
368

369
    php_mustache_ast_wakeup(getThis(), return_value);
8✔
370
  } catch(...) {
×
371
    mustache_exception_handler();
×
372
  }
×
373
}
4✔
374
/* }}} MustacheAST::__wakeup */
375

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc