spl_observer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. | https://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: Marcus Boerger <helly@php.net> |
  14. | Etienne Kneuss <colder@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include "config.h"
  19. #endif
  20. #include "php.h"
  21. #include "php_ini.h"
  22. #include "ext/standard/info.h"
  23. #include "ext/standard/php_array.h"
  24. #include "ext/standard/php_var.h"
  25. #include "zend_smart_str.h"
  26. #include "zend_interfaces.h"
  27. #include "zend_exceptions.h"
  28. #include "php_spl.h"
  29. #include "spl_functions.h"
  30. #include "spl_engine.h"
  31. #include "spl_observer.h"
  32. #include "spl_observer_arginfo.h"
  33. #include "spl_iterators.h"
  34. #include "spl_array.h"
  35. #include "spl_exceptions.h"
  36. PHPAPI zend_class_entry *spl_ce_SplObserver;
  37. PHPAPI zend_class_entry *spl_ce_SplSubject;
  38. PHPAPI zend_class_entry *spl_ce_SplObjectStorage;
  39. PHPAPI zend_class_entry *spl_ce_MultipleIterator;
  40. PHPAPI zend_object_handlers spl_handler_SplObjectStorage;
  41. typedef struct _spl_SplObjectStorage { /* {{{ */
  42. HashTable storage;
  43. zend_long index;
  44. HashPosition pos;
  45. zend_long flags;
  46. zend_function *fptr_get_hash;
  47. zend_object std;
  48. } spl_SplObjectStorage; /* }}} */
  49. /* {{{ storage is an assoc array of [zend_object*]=>[zval *obj, zval *inf] */
  50. typedef struct _spl_SplObjectStorageElement {
  51. zend_object *obj;
  52. zval inf;
  53. } spl_SplObjectStorageElement; /* }}} */
  54. static inline spl_SplObjectStorage *spl_object_storage_from_obj(zend_object *obj) /* {{{ */ {
  55. return (spl_SplObjectStorage*)((char*)(obj) - XtOffsetOf(spl_SplObjectStorage, std));
  56. }
  57. /* }}} */
  58. #define Z_SPLOBJSTORAGE_P(zv) spl_object_storage_from_obj(Z_OBJ_P((zv)))
  59. void spl_SplObjectStorage_free_storage(zend_object *object) /* {{{ */
  60. {
  61. spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
  62. zend_object_std_dtor(&intern->std);
  63. zend_hash_destroy(&intern->storage);
  64. } /* }}} */
  65. static int spl_object_storage_get_hash(zend_hash_key *key, spl_SplObjectStorage *intern, zend_object *obj) {
  66. if (intern->fptr_get_hash) {
  67. zval param;
  68. zval rv;
  69. ZVAL_OBJ(&param, obj);
  70. zend_call_method_with_1_params(
  71. &intern->std, intern->std.ce, &intern->fptr_get_hash, "getHash", &rv, &param);
  72. if (!Z_ISUNDEF(rv)) {
  73. if (Z_TYPE(rv) == IS_STRING) {
  74. key->key = Z_STR(rv);
  75. return SUCCESS;
  76. } else {
  77. zend_throw_exception(spl_ce_RuntimeException, "Hash needs to be a string", 0);
  78. zval_ptr_dtor(&rv);
  79. return FAILURE;
  80. }
  81. } else {
  82. return FAILURE;
  83. }
  84. } else {
  85. key->key = NULL;
  86. key->h = obj->handle;
  87. return SUCCESS;
  88. }
  89. }
  90. static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, zend_hash_key *key) {
  91. if (key->key) {
  92. zend_string_release_ex(key->key, 0);
  93. }
  94. }
  95. static void spl_object_storage_dtor(zval *element) /* {{{ */
  96. {
  97. spl_SplObjectStorageElement *el = Z_PTR_P(element);
  98. zend_object_release(el->obj);
  99. zval_ptr_dtor(&el->inf);
  100. efree(el);
  101. } /* }}} */
  102. static spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, zend_hash_key *key) /* {{{ */
  103. {
  104. if (key->key) {
  105. return zend_hash_find_ptr(&intern->storage, key->key);
  106. } else {
  107. return zend_hash_index_find_ptr(&intern->storage, key->h);
  108. }
  109. } /* }}} */
  110. spl_SplObjectStorageElement *spl_object_storage_attach(spl_SplObjectStorage *intern, zend_object *obj, zval *inf) /* {{{ */
  111. {
  112. spl_SplObjectStorageElement *pelement, element;
  113. zend_hash_key key;
  114. if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
  115. return NULL;
  116. }
  117. pelement = spl_object_storage_get(intern, &key);
  118. if (pelement) {
  119. zval_ptr_dtor(&pelement->inf);
  120. if (inf) {
  121. ZVAL_COPY(&pelement->inf, inf);
  122. } else {
  123. ZVAL_NULL(&pelement->inf);
  124. }
  125. spl_object_storage_free_hash(intern, &key);
  126. return pelement;
  127. }
  128. element.obj = obj;
  129. GC_ADDREF(obj);
  130. if (inf) {
  131. ZVAL_COPY(&element.inf, inf);
  132. } else {
  133. ZVAL_NULL(&element.inf);
  134. }
  135. if (key.key) {
  136. pelement = zend_hash_update_mem(&intern->storage, key.key, &element, sizeof(spl_SplObjectStorageElement));
  137. } else {
  138. pelement = zend_hash_index_update_mem(&intern->storage, key.h, &element, sizeof(spl_SplObjectStorageElement));
  139. }
  140. spl_object_storage_free_hash(intern, &key);
  141. return pelement;
  142. } /* }}} */
  143. static int spl_object_storage_detach(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */
  144. {
  145. int ret = FAILURE;
  146. zend_hash_key key;
  147. if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
  148. return ret;
  149. }
  150. if (key.key) {
  151. ret = zend_hash_del(&intern->storage, key.key);
  152. } else {
  153. ret = zend_hash_index_del(&intern->storage, key.h);
  154. }
  155. spl_object_storage_free_hash(intern, &key);
  156. return ret;
  157. } /* }}}*/
  158. void spl_object_storage_addall(spl_SplObjectStorage *intern, spl_SplObjectStorage *other) { /* {{{ */
  159. spl_SplObjectStorageElement *element;
  160. ZEND_HASH_FOREACH_PTR(&other->storage, element) {
  161. spl_object_storage_attach(intern, element->obj, &element->inf);
  162. } ZEND_HASH_FOREACH_END();
  163. intern->index = 0;
  164. } /* }}} */
  165. static zend_object *spl_object_storage_new_ex(zend_class_entry *class_type, zend_object *orig) /* {{{ */
  166. {
  167. spl_SplObjectStorage *intern;
  168. zend_class_entry *parent = class_type;
  169. intern = emalloc(sizeof(spl_SplObjectStorage) + zend_object_properties_size(parent));
  170. memset(intern, 0, sizeof(spl_SplObjectStorage) - sizeof(zval));
  171. intern->pos = 0;
  172. zend_object_std_init(&intern->std, class_type);
  173. object_properties_init(&intern->std, class_type);
  174. zend_hash_init(&intern->storage, 0, NULL, spl_object_storage_dtor, 0);
  175. intern->std.handlers = &spl_handler_SplObjectStorage;
  176. while (parent) {
  177. if (parent == spl_ce_SplObjectStorage) {
  178. if (class_type != spl_ce_SplObjectStorage) {
  179. intern->fptr_get_hash = zend_hash_str_find_ptr(&class_type->function_table, "gethash", sizeof("gethash") - 1);
  180. if (intern->fptr_get_hash->common.scope == spl_ce_SplObjectStorage) {
  181. intern->fptr_get_hash = NULL;
  182. }
  183. }
  184. break;
  185. }
  186. parent = parent->parent;
  187. }
  188. if (orig) {
  189. spl_SplObjectStorage *other = spl_object_storage_from_obj(orig);
  190. spl_object_storage_addall(intern, other);
  191. }
  192. return &intern->std;
  193. }
  194. /* }}} */
  195. /* {{{ spl_object_storage_clone */
  196. static zend_object *spl_object_storage_clone(zend_object *old_object)
  197. {
  198. zend_object *new_object;
  199. new_object = spl_object_storage_new_ex(old_object->ce, old_object);
  200. zend_objects_clone_members(new_object, old_object);
  201. return new_object;
  202. }
  203. /* }}} */
  204. static inline HashTable* spl_object_storage_debug_info(zend_object *obj) /* {{{ */
  205. {
  206. spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj);
  207. spl_SplObjectStorageElement *element;
  208. HashTable *props;
  209. zval tmp, storage;
  210. zend_string *zname;
  211. HashTable *debug_info;
  212. props = obj->handlers->get_properties(obj);
  213. debug_info = zend_new_array(zend_hash_num_elements(props) + 1);
  214. zend_hash_copy(debug_info, props, (copy_ctor_func_t)zval_add_ref);
  215. array_init(&storage);
  216. ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
  217. array_init(&tmp);
  218. /* Incrementing the refcount of obj and inf would confuse the garbage collector.
  219. * Prefer to null the destructor */
  220. Z_ARRVAL_P(&tmp)->pDestructor = NULL;
  221. zval obj;
  222. ZVAL_OBJ(&obj, element->obj);
  223. add_assoc_zval_ex(&tmp, "obj", sizeof("obj") - 1, &obj);
  224. add_assoc_zval_ex(&tmp, "inf", sizeof("inf") - 1, &element->inf);
  225. zend_hash_next_index_insert(Z_ARRVAL(storage), &tmp);
  226. } ZEND_HASH_FOREACH_END();
  227. zname = spl_gen_private_prop_name(spl_ce_SplObjectStorage, "storage", sizeof("storage")-1);
  228. zend_symtable_update(debug_info, zname, &storage);
  229. zend_string_release_ex(zname, 0);
  230. return debug_info;
  231. }
  232. /* }}} */
  233. /* overridden for garbage collection */
  234. static HashTable *spl_object_storage_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */
  235. {
  236. spl_SplObjectStorage *intern = spl_object_storage_from_obj(obj);
  237. spl_SplObjectStorageElement *element;
  238. zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
  239. ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
  240. zend_get_gc_buffer_add_obj(gc_buffer, element->obj);
  241. zend_get_gc_buffer_add_zval(gc_buffer, &element->inf);
  242. } ZEND_HASH_FOREACH_END();
  243. zend_get_gc_buffer_use(gc_buffer, table, n);
  244. return zend_std_get_properties(obj);
  245. }
  246. /* }}} */
  247. static int spl_object_storage_compare_info(zval *e1, zval *e2) /* {{{ */
  248. {
  249. spl_SplObjectStorageElement *s1 = (spl_SplObjectStorageElement*)Z_PTR_P(e1);
  250. spl_SplObjectStorageElement *s2 = (spl_SplObjectStorageElement*)Z_PTR_P(e2);
  251. return zend_compare(&s1->inf, &s2->inf);
  252. }
  253. /* }}} */
  254. static int spl_object_storage_compare_objects(zval *o1, zval *o2) /* {{{ */
  255. {
  256. zend_object *zo1;
  257. zend_object *zo2;
  258. ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
  259. zo1 = (zend_object *)Z_OBJ_P(o1);
  260. zo2 = (zend_object *)Z_OBJ_P(o2);
  261. if (zo1->ce != spl_ce_SplObjectStorage || zo2->ce != spl_ce_SplObjectStorage) {
  262. return ZEND_UNCOMPARABLE;
  263. }
  264. return zend_hash_compare(&(Z_SPLOBJSTORAGE_P(o1))->storage, &(Z_SPLOBJSTORAGE_P(o2))->storage, (compare_func_t)spl_object_storage_compare_info, 0);
  265. }
  266. /* }}} */
  267. /* {{{ spl_array_object_new */
  268. static zend_object *spl_SplObjectStorage_new(zend_class_entry *class_type)
  269. {
  270. return spl_object_storage_new_ex(class_type, NULL);
  271. }
  272. /* }}} */
  273. int spl_object_storage_contains(spl_SplObjectStorage *intern, zend_object *obj) /* {{{ */
  274. {
  275. int found;
  276. zend_hash_key key;
  277. if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
  278. return 0;
  279. }
  280. if (key.key) {
  281. found = zend_hash_exists(&intern->storage, key.key);
  282. } else {
  283. found = zend_hash_index_exists(&intern->storage, key.h);
  284. }
  285. spl_object_storage_free_hash(intern, &key);
  286. return found;
  287. } /* }}} */
  288. /* {{{ Attaches an object to the storage if not yet contained */
  289. PHP_METHOD(SplObjectStorage, attach)
  290. {
  291. zend_object *obj;
  292. zval *inf = NULL;
  293. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  294. ZEND_PARSE_PARAMETERS_START(1, 2)
  295. Z_PARAM_OBJ(obj)
  296. Z_PARAM_OPTIONAL
  297. Z_PARAM_ZVAL(inf)
  298. ZEND_PARSE_PARAMETERS_END();
  299. spl_object_storage_attach(intern, obj, inf);
  300. } /* }}} */
  301. /* {{{ Detaches an object from the storage */
  302. PHP_METHOD(SplObjectStorage, detach)
  303. {
  304. zend_object *obj;
  305. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  306. ZEND_PARSE_PARAMETERS_START(1, 1)
  307. Z_PARAM_OBJ(obj)
  308. ZEND_PARSE_PARAMETERS_END();
  309. spl_object_storage_detach(intern, obj);
  310. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  311. intern->index = 0;
  312. } /* }}} */
  313. /* {{{ Returns the hash of an object */
  314. PHP_METHOD(SplObjectStorage, getHash)
  315. {
  316. zend_object *obj;
  317. ZEND_PARSE_PARAMETERS_START(1, 1)
  318. Z_PARAM_OBJ(obj)
  319. ZEND_PARSE_PARAMETERS_END();
  320. RETURN_NEW_STR(php_spl_object_hash(obj));
  321. } /* }}} */
  322. /* {{{ Returns associated information for a stored object */
  323. PHP_METHOD(SplObjectStorage, offsetGet)
  324. {
  325. zend_object *obj;
  326. spl_SplObjectStorageElement *element;
  327. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  328. zend_hash_key key;
  329. ZEND_PARSE_PARAMETERS_START(1, 1)
  330. Z_PARAM_OBJ(obj)
  331. ZEND_PARSE_PARAMETERS_END();
  332. if (spl_object_storage_get_hash(&key, intern, obj) == FAILURE) {
  333. return;
  334. }
  335. element = spl_object_storage_get(intern, &key);
  336. spl_object_storage_free_hash(intern, &key);
  337. if (!element) {
  338. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Object not found");
  339. } else {
  340. RETURN_COPY_DEREF(&element->inf);
  341. }
  342. } /* }}} */
  343. /* {{{ Add all elements contained in $os */
  344. PHP_METHOD(SplObjectStorage, addAll)
  345. {
  346. zval *obj;
  347. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  348. spl_SplObjectStorage *other;
  349. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
  350. RETURN_THROWS();
  351. }
  352. other = Z_SPLOBJSTORAGE_P(obj);
  353. spl_object_storage_addall(intern, other);
  354. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  355. } /* }}} */
  356. /* {{{ Remove all elements contained in $os */
  357. PHP_METHOD(SplObjectStorage, removeAll)
  358. {
  359. zval *obj;
  360. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  361. spl_SplObjectStorage *other;
  362. spl_SplObjectStorageElement *element;
  363. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
  364. RETURN_THROWS();
  365. }
  366. other = Z_SPLOBJSTORAGE_P(obj);
  367. zend_hash_internal_pointer_reset(&other->storage);
  368. while ((element = zend_hash_get_current_data_ptr(&other->storage)) != NULL) {
  369. if (spl_object_storage_detach(intern, element->obj) == FAILURE) {
  370. zend_hash_move_forward(&other->storage);
  371. }
  372. }
  373. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  374. intern->index = 0;
  375. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  376. } /* }}} */
  377. /* {{{ Remove elements not common to both this SplObjectStorage instance and $os */
  378. PHP_METHOD(SplObjectStorage, removeAllExcept)
  379. {
  380. zval *obj;
  381. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  382. spl_SplObjectStorage *other;
  383. spl_SplObjectStorageElement *element;
  384. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
  385. RETURN_THROWS();
  386. }
  387. other = Z_SPLOBJSTORAGE_P(obj);
  388. ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
  389. if (!spl_object_storage_contains(other, element->obj)) {
  390. spl_object_storage_detach(intern, element->obj);
  391. }
  392. } ZEND_HASH_FOREACH_END();
  393. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  394. intern->index = 0;
  395. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  396. }
  397. /* }}} */
  398. /* {{{ Determine whether an object is contained in the storage */
  399. PHP_METHOD(SplObjectStorage, contains)
  400. {
  401. zend_object *obj;
  402. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  403. ZEND_PARSE_PARAMETERS_START(1, 1)
  404. Z_PARAM_OBJ(obj)
  405. ZEND_PARSE_PARAMETERS_END();
  406. RETURN_BOOL(spl_object_storage_contains(intern, obj));
  407. } /* }}} */
  408. /* {{{ Determine number of objects in storage */
  409. PHP_METHOD(SplObjectStorage, count)
  410. {
  411. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  412. zend_long mode = COUNT_NORMAL;
  413. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) {
  414. RETURN_THROWS();
  415. }
  416. if (mode == COUNT_RECURSIVE) {
  417. RETURN_LONG(php_count_recursive(&intern->storage));
  418. }
  419. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  420. } /* }}} */
  421. /* {{{ Rewind to first position */
  422. PHP_METHOD(SplObjectStorage, rewind)
  423. {
  424. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  425. if (zend_parse_parameters_none() == FAILURE) {
  426. RETURN_THROWS();
  427. }
  428. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  429. intern->index = 0;
  430. } /* }}} */
  431. /* {{{ Returns whether current position is valid */
  432. PHP_METHOD(SplObjectStorage, valid)
  433. {
  434. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  435. if (zend_parse_parameters_none() == FAILURE) {
  436. RETURN_THROWS();
  437. }
  438. RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->storage, &intern->pos) == SUCCESS);
  439. } /* }}} */
  440. /* {{{ Returns current key */
  441. PHP_METHOD(SplObjectStorage, key)
  442. {
  443. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  444. if (zend_parse_parameters_none() == FAILURE) {
  445. RETURN_THROWS();
  446. }
  447. RETURN_LONG(intern->index);
  448. } /* }}} */
  449. /* {{{ Returns current element */
  450. PHP_METHOD(SplObjectStorage, current)
  451. {
  452. spl_SplObjectStorageElement *element;
  453. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  454. if (zend_parse_parameters_none() == FAILURE) {
  455. RETURN_THROWS();
  456. }
  457. if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
  458. zend_throw_exception(spl_ce_RuntimeException, "Called current() on invalid iterator", 0);
  459. RETURN_THROWS();
  460. }
  461. ZVAL_OBJ_COPY(return_value, element->obj);
  462. } /* }}} */
  463. /* {{{ Returns associated information to current element */
  464. PHP_METHOD(SplObjectStorage, getInfo)
  465. {
  466. spl_SplObjectStorageElement *element;
  467. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  468. if (zend_parse_parameters_none() == FAILURE) {
  469. RETURN_THROWS();
  470. }
  471. if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
  472. return;
  473. }
  474. ZVAL_COPY(return_value, &element->inf);
  475. } /* }}} */
  476. /* {{{ Sets associated information of current element to $inf */
  477. PHP_METHOD(SplObjectStorage, setInfo)
  478. {
  479. spl_SplObjectStorageElement *element;
  480. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  481. zval *inf;
  482. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &inf) == FAILURE) {
  483. RETURN_THROWS();
  484. }
  485. if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) == NULL) {
  486. return;
  487. }
  488. zval_ptr_dtor(&element->inf);
  489. ZVAL_COPY(&element->inf, inf);
  490. } /* }}} */
  491. /* {{{ Moves position forward */
  492. PHP_METHOD(SplObjectStorage, next)
  493. {
  494. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  495. if (zend_parse_parameters_none() == FAILURE) {
  496. RETURN_THROWS();
  497. }
  498. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  499. intern->index++;
  500. } /* }}} */
  501. /* {{{ Serializes storage */
  502. PHP_METHOD(SplObjectStorage, serialize)
  503. {
  504. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  505. spl_SplObjectStorageElement *element;
  506. zval members, flags;
  507. HashPosition pos;
  508. php_serialize_data_t var_hash;
  509. smart_str buf = {0};
  510. if (zend_parse_parameters_none() == FAILURE) {
  511. RETURN_THROWS();
  512. }
  513. PHP_VAR_SERIALIZE_INIT(var_hash);
  514. /* storage */
  515. smart_str_appendl(&buf, "x:", 2);
  516. ZVAL_LONG(&flags, zend_hash_num_elements(&intern->storage));
  517. php_var_serialize(&buf, &flags, &var_hash);
  518. zend_hash_internal_pointer_reset_ex(&intern->storage, &pos);
  519. while (zend_hash_has_more_elements_ex(&intern->storage, &pos) == SUCCESS) {
  520. zval obj;
  521. if ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &pos)) == NULL) {
  522. smart_str_free(&buf);
  523. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  524. RETURN_NULL();
  525. }
  526. ZVAL_OBJ(&obj, element->obj);
  527. php_var_serialize(&buf, &obj, &var_hash);
  528. smart_str_appendc(&buf, ',');
  529. php_var_serialize(&buf, &element->inf, &var_hash);
  530. smart_str_appendc(&buf, ';');
  531. zend_hash_move_forward_ex(&intern->storage, &pos);
  532. }
  533. /* members */
  534. smart_str_appendl(&buf, "m:", 2);
  535. ZVAL_ARR(&members, zend_array_dup(zend_std_get_properties(Z_OBJ_P(ZEND_THIS))));
  536. php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
  537. zval_ptr_dtor(&members);
  538. /* done */
  539. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  540. RETURN_NEW_STR(buf.s);
  541. } /* }}} */
  542. /* {{{ Unserializes storage */
  543. PHP_METHOD(SplObjectStorage, unserialize)
  544. {
  545. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  546. char *buf;
  547. size_t buf_len;
  548. const unsigned char *p, *s;
  549. php_unserialize_data_t var_hash;
  550. zval *pcount, *pmembers;
  551. spl_SplObjectStorageElement *element;
  552. zend_long count;
  553. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
  554. RETURN_THROWS();
  555. }
  556. if (buf_len == 0) {
  557. return;
  558. }
  559. /* storage */
  560. s = p = (const unsigned char*)buf;
  561. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  562. if (*p!= 'x' || *++p != ':') {
  563. goto outexcept;
  564. }
  565. ++p;
  566. pcount = var_tmp_var(&var_hash);
  567. if (!php_var_unserialize(pcount, &p, s + buf_len, &var_hash) || Z_TYPE_P(pcount) != IS_LONG) {
  568. goto outexcept;
  569. }
  570. --p; /* for ';' */
  571. count = Z_LVAL_P(pcount);
  572. if (count < 0) {
  573. goto outexcept;
  574. }
  575. while (count-- > 0) {
  576. spl_SplObjectStorageElement *pelement;
  577. zend_hash_key key;
  578. zval *entry = var_tmp_var(&var_hash);
  579. zval inf;
  580. ZVAL_UNDEF(&inf);
  581. if (*p != ';') {
  582. goto outexcept;
  583. }
  584. ++p;
  585. if(*p != 'O' && *p != 'C' && *p != 'r') {
  586. goto outexcept;
  587. }
  588. /* store reference to allow cross-references between different elements */
  589. if (!php_var_unserialize(entry, &p, s + buf_len, &var_hash)) {
  590. goto outexcept;
  591. }
  592. if (*p == ',') { /* new version has inf */
  593. ++p;
  594. if (!php_var_unserialize(&inf, &p, s + buf_len, &var_hash)) {
  595. zval_ptr_dtor(&inf);
  596. goto outexcept;
  597. }
  598. }
  599. if (Z_TYPE_P(entry) != IS_OBJECT) {
  600. zval_ptr_dtor(&inf);
  601. goto outexcept;
  602. }
  603. if (spl_object_storage_get_hash(&key, intern, Z_OBJ_P(entry)) == FAILURE) {
  604. zval_ptr_dtor(&inf);
  605. goto outexcept;
  606. }
  607. pelement = spl_object_storage_get(intern, &key);
  608. spl_object_storage_free_hash(intern, &key);
  609. if (pelement) {
  610. zval obj;
  611. if (!Z_ISUNDEF(pelement->inf)) {
  612. var_push_dtor(&var_hash, &pelement->inf);
  613. }
  614. ZVAL_OBJ(&obj, pelement->obj);
  615. var_push_dtor(&var_hash, &obj);
  616. }
  617. element = spl_object_storage_attach(intern, Z_OBJ_P(entry), Z_ISUNDEF(inf)?NULL:&inf);
  618. var_replace(&var_hash, &inf, &element->inf);
  619. zval_ptr_dtor(&inf);
  620. }
  621. if (*p != ';') {
  622. goto outexcept;
  623. }
  624. ++p;
  625. /* members */
  626. if (*p!= 'm' || *++p != ':') {
  627. goto outexcept;
  628. }
  629. ++p;
  630. pmembers = var_tmp_var(&var_hash);
  631. if (!php_var_unserialize(pmembers, &p, s + buf_len, &var_hash) || Z_TYPE_P(pmembers) != IS_ARRAY) {
  632. goto outexcept;
  633. }
  634. /* copy members */
  635. object_properties_load(&intern->std, Z_ARRVAL_P(pmembers));
  636. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  637. return;
  638. outexcept:
  639. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  640. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %zd of %zd bytes", ((char*)p - buf), buf_len);
  641. RETURN_THROWS();
  642. } /* }}} */
  643. /* {{{ */
  644. PHP_METHOD(SplObjectStorage, __serialize)
  645. {
  646. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  647. spl_SplObjectStorageElement *elem;
  648. zval tmp;
  649. if (zend_parse_parameters_none() == FAILURE) {
  650. RETURN_THROWS();
  651. }
  652. array_init(return_value);
  653. /* storage */
  654. array_init_size(&tmp, 2 * zend_hash_num_elements(&intern->storage));
  655. ZEND_HASH_FOREACH_PTR(&intern->storage, elem) {
  656. zval obj;
  657. ZVAL_OBJ_COPY(&obj, elem->obj);
  658. zend_hash_next_index_insert(Z_ARRVAL(tmp), &obj);
  659. Z_TRY_ADDREF(elem->inf);
  660. zend_hash_next_index_insert(Z_ARRVAL(tmp), &elem->inf);
  661. } ZEND_HASH_FOREACH_END();
  662. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  663. /* members */
  664. ZVAL_ARR(&tmp, zend_proptable_to_symtable(
  665. zend_std_get_properties(&intern->std), /* always_duplicate */ 1));
  666. zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
  667. } /* }}} */
  668. /* {{{ */
  669. PHP_METHOD(SplObjectStorage, __unserialize)
  670. {
  671. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  672. HashTable *data;
  673. zval *storage_zv, *members_zv, *key, *val;
  674. if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
  675. RETURN_THROWS();
  676. }
  677. storage_zv = zend_hash_index_find(data, 0);
  678. members_zv = zend_hash_index_find(data, 1);
  679. if (!storage_zv || !members_zv ||
  680. Z_TYPE_P(storage_zv) != IS_ARRAY || Z_TYPE_P(members_zv) != IS_ARRAY) {
  681. zend_throw_exception(spl_ce_UnexpectedValueException,
  682. "Incomplete or ill-typed serialization data", 0);
  683. RETURN_THROWS();
  684. }
  685. if (zend_hash_num_elements(Z_ARRVAL_P(storage_zv)) % 2 != 0) {
  686. zend_throw_exception(spl_ce_UnexpectedValueException, "Odd number of elements", 0);
  687. RETURN_THROWS();
  688. }
  689. key = NULL;
  690. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(storage_zv), val) {
  691. if (key) {
  692. if (Z_TYPE_P(key) != IS_OBJECT) {
  693. zend_throw_exception(spl_ce_UnexpectedValueException, "Non-object key", 0);
  694. RETURN_THROWS();
  695. }
  696. spl_object_storage_attach(intern, Z_OBJ_P(key), val);
  697. key = NULL;
  698. } else {
  699. key = val;
  700. }
  701. } ZEND_HASH_FOREACH_END();
  702. object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
  703. }
  704. /* {{{ */
  705. PHP_METHOD(SplObjectStorage, __debugInfo)
  706. {
  707. if (zend_parse_parameters_none() == FAILURE) {
  708. return;
  709. }
  710. RETURN_ARR(spl_object_storage_debug_info(Z_OBJ_P(ZEND_THIS)));
  711. }
  712. /* }}} */
  713. typedef enum {
  714. MIT_NEED_ANY = 0,
  715. MIT_NEED_ALL = 1,
  716. MIT_KEYS_NUMERIC = 0,
  717. MIT_KEYS_ASSOC = 2
  718. } MultipleIteratorFlags;
  719. #define SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT 1
  720. #define SPL_MULTIPLE_ITERATOR_GET_ALL_KEY 2
  721. /* {{{ Iterator that iterates over several iterators one after the other */
  722. PHP_METHOD(MultipleIterator, __construct)
  723. {
  724. spl_SplObjectStorage *intern;
  725. zend_long flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC;
  726. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags) == FAILURE) {
  727. RETURN_THROWS();
  728. }
  729. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  730. intern->flags = flags;
  731. }
  732. /* }}} */
  733. /* {{{ Return current flags */
  734. PHP_METHOD(MultipleIterator, getFlags)
  735. {
  736. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  737. if (zend_parse_parameters_none() == FAILURE) {
  738. RETURN_THROWS();
  739. }
  740. RETURN_LONG(intern->flags);
  741. }
  742. /* }}} */
  743. /* {{{ Set flags */
  744. PHP_METHOD(MultipleIterator, setFlags)
  745. {
  746. spl_SplObjectStorage *intern;
  747. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  748. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
  749. RETURN_THROWS();
  750. }
  751. }
  752. /* }}} */
  753. /* {{{ Attach a new iterator */
  754. PHP_METHOD(MultipleIterator, attachIterator)
  755. {
  756. spl_SplObjectStorage *intern;
  757. zend_object *iterator = NULL;
  758. zval zinfo;
  759. zend_string *info_str;
  760. zend_long info_long;
  761. bool info_is_null = 1;
  762. ZEND_PARSE_PARAMETERS_START(1, 2)
  763. Z_PARAM_OBJ_OF_CLASS(iterator, zend_ce_iterator)
  764. Z_PARAM_OPTIONAL
  765. Z_PARAM_STR_OR_LONG_OR_NULL(info_str, info_long, info_is_null)
  766. ZEND_PARSE_PARAMETERS_END();
  767. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  768. if (!info_is_null) {
  769. spl_SplObjectStorageElement *element;
  770. if (info_str) {
  771. ZVAL_STR(&zinfo, info_str);
  772. } else {
  773. ZVAL_LONG(&zinfo, info_long);
  774. }
  775. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  776. while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL) {
  777. if (fast_is_identical_function(&zinfo, &element->inf)) {
  778. zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0);
  779. RETURN_THROWS();
  780. }
  781. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  782. }
  783. spl_object_storage_attach(intern, iterator, &zinfo);
  784. } else {
  785. spl_object_storage_attach(intern, iterator, NULL);
  786. }
  787. }
  788. /* }}} */
  789. /* {{{ Detaches an iterator */
  790. PHP_METHOD(MultipleIterator, detachIterator)
  791. {
  792. zval *iterator;
  793. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  794. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) {
  795. RETURN_THROWS();
  796. }
  797. spl_object_storage_detach(intern, Z_OBJ_P(iterator));
  798. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  799. intern->index = 0;
  800. } /* }}} */
  801. /* {{{ Determine whether the iterator exists */
  802. PHP_METHOD(MultipleIterator, containsIterator)
  803. {
  804. zval *iterator;
  805. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  806. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &iterator, zend_ce_iterator) == FAILURE) {
  807. RETURN_THROWS();
  808. }
  809. RETURN_BOOL(spl_object_storage_contains(intern, Z_OBJ_P(iterator)));
  810. } /* }}} */
  811. PHP_METHOD(MultipleIterator, countIterators)
  812. {
  813. spl_SplObjectStorage *intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  814. if (zend_parse_parameters_none() == FAILURE) {
  815. RETURN_THROWS();
  816. }
  817. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  818. }
  819. /* {{{ Rewind all attached iterator instances */
  820. PHP_METHOD(MultipleIterator, rewind)
  821. {
  822. spl_SplObjectStorage *intern;
  823. spl_SplObjectStorageElement *element;
  824. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  825. if (zend_parse_parameters_none() == FAILURE) {
  826. RETURN_THROWS();
  827. }
  828. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  829. while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
  830. zend_object *it = element->obj;
  831. zend_call_method_with_0_params(it, it->ce, it->ce->iterator_funcs_ptr ? &it->ce->iterator_funcs_ptr->zf_rewind : NULL, "rewind", NULL);
  832. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  833. }
  834. }
  835. /* }}} */
  836. /* {{{ Move all attached iterator instances forward */
  837. PHP_METHOD(MultipleIterator, next)
  838. {
  839. spl_SplObjectStorage *intern;
  840. spl_SplObjectStorageElement *element;
  841. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  842. if (zend_parse_parameters_none() == FAILURE) {
  843. RETURN_THROWS();
  844. }
  845. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  846. while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
  847. zend_object *it = element->obj;
  848. zend_call_method_with_0_params(it, it->ce, it->ce->iterator_funcs_ptr ? &it->ce->iterator_funcs_ptr->zf_next : NULL, "next", NULL);
  849. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  850. }
  851. }
  852. /* }}} */
  853. /* {{{ Return whether all or one sub iterator is valid depending on flags */
  854. PHP_METHOD(MultipleIterator, valid)
  855. {
  856. spl_SplObjectStorage *intern;
  857. spl_SplObjectStorageElement *element;
  858. zval retval;
  859. zend_long expect, valid;
  860. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  861. if (zend_parse_parameters_none() == FAILURE) {
  862. RETURN_THROWS();
  863. }
  864. if (!zend_hash_num_elements(&intern->storage)) {
  865. RETURN_FALSE;
  866. }
  867. expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0;
  868. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  869. while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
  870. zend_object *it = element->obj;
  871. zend_call_method_with_0_params(it, it->ce, it->ce->iterator_funcs_ptr ? &it->ce->iterator_funcs_ptr->zf_valid : NULL, "valid", &retval);
  872. if (!Z_ISUNDEF(retval)) {
  873. valid = (Z_TYPE(retval) == IS_TRUE);
  874. zval_ptr_dtor(&retval);
  875. } else {
  876. valid = 0;
  877. }
  878. if (expect != valid) {
  879. RETURN_BOOL(!expect);
  880. }
  881. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  882. }
  883. RETURN_BOOL(expect);
  884. }
  885. /* }}} */
  886. static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value) /* {{{ */
  887. {
  888. spl_SplObjectStorageElement *element;
  889. zval retval;
  890. int valid = 1, num_elements;
  891. num_elements = zend_hash_num_elements(&intern->storage);
  892. if (num_elements < 1) {
  893. zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Called %s() on an invalid iterator",
  894. get_type == SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT ? "current" : "key");
  895. RETURN_THROWS();
  896. }
  897. array_init_size(return_value, num_elements);
  898. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  899. while ((element = zend_hash_get_current_data_ptr_ex(&intern->storage, &intern->pos)) != NULL && !EG(exception)) {
  900. zend_object *it = element->obj;
  901. zend_call_method_with_0_params(it, it->ce, it->ce->iterator_funcs_ptr ? &it->ce->iterator_funcs_ptr->zf_valid : NULL, "valid", &retval);
  902. if (!Z_ISUNDEF(retval)) {
  903. valid = Z_TYPE(retval) == IS_TRUE;
  904. zval_ptr_dtor(&retval);
  905. } else {
  906. valid = 0;
  907. }
  908. if (valid) {
  909. if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
  910. zend_call_method_with_0_params(it, it->ce, it->ce->iterator_funcs_ptr ? &it->ce->iterator_funcs_ptr->zf_current : NULL, "current", &retval);
  911. } else {
  912. zend_call_method_with_0_params(it, it->ce, it->ce->iterator_funcs_ptr ? &it->ce->iterator_funcs_ptr->zf_key : NULL, "key", &retval);
  913. }
  914. if (Z_ISUNDEF(retval)) {
  915. zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0);
  916. return;
  917. }
  918. } else if (intern->flags & MIT_NEED_ALL) {
  919. if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
  920. zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0);
  921. } else {
  922. zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0);
  923. }
  924. return;
  925. } else {
  926. ZVAL_NULL(&retval);
  927. }
  928. if (intern->flags & MIT_KEYS_ASSOC) {
  929. switch (Z_TYPE(element->inf)) {
  930. case IS_LONG:
  931. add_index_zval(return_value, Z_LVAL(element->inf), &retval);
  932. break;
  933. case IS_STRING:
  934. zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR(element->inf), &retval);
  935. break;
  936. default:
  937. zval_ptr_dtor(&retval);
  938. zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0);
  939. return;
  940. }
  941. } else {
  942. add_next_index_zval(return_value, &retval);
  943. }
  944. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  945. }
  946. }
  947. /* }}} */
  948. /* {{{ Return an array of all registered Iterator instances current() result */
  949. PHP_METHOD(MultipleIterator, current)
  950. {
  951. spl_SplObjectStorage *intern;
  952. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  953. if (zend_parse_parameters_none() == FAILURE) {
  954. RETURN_THROWS();
  955. }
  956. spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value);
  957. }
  958. /* }}} */
  959. /* {{{ Return an array of all registered Iterator instances key() result */
  960. PHP_METHOD(MultipleIterator, key)
  961. {
  962. spl_SplObjectStorage *intern;
  963. intern = Z_SPLOBJSTORAGE_P(ZEND_THIS);
  964. if (zend_parse_parameters_none() == FAILURE) {
  965. RETURN_THROWS();
  966. }
  967. spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_KEY, return_value);
  968. }
  969. /* }}} */
  970. /* {{{ PHP_MINIT_FUNCTION(spl_observer) */
  971. PHP_MINIT_FUNCTION(spl_observer)
  972. {
  973. spl_ce_SplObserver = register_class_SplObserver();
  974. spl_ce_SplSubject = register_class_SplSubject();
  975. spl_ce_SplObjectStorage = register_class_SplObjectStorage(zend_ce_countable, zend_ce_iterator, zend_ce_serializable, zend_ce_arrayaccess);
  976. spl_ce_SplObjectStorage->create_object = spl_SplObjectStorage_new;
  977. memcpy(&spl_handler_SplObjectStorage, &std_object_handlers, sizeof(zend_object_handlers));
  978. spl_handler_SplObjectStorage.offset = XtOffsetOf(spl_SplObjectStorage, std);
  979. spl_handler_SplObjectStorage.compare = spl_object_storage_compare_objects;
  980. spl_handler_SplObjectStorage.clone_obj = spl_object_storage_clone;
  981. spl_handler_SplObjectStorage.get_gc = spl_object_storage_get_gc;
  982. spl_handler_SplObjectStorage.free_obj = spl_SplObjectStorage_free_storage;
  983. spl_ce_MultipleIterator = register_class_MultipleIterator(zend_ce_iterator);
  984. spl_ce_MultipleIterator->create_object = spl_SplObjectStorage_new;
  985. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_NEED_ANY", MIT_NEED_ANY);
  986. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_NEED_ALL", MIT_NEED_ALL);
  987. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_KEYS_NUMERIC", MIT_KEYS_NUMERIC);
  988. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_KEYS_ASSOC", MIT_KEYS_ASSOC);
  989. return SUCCESS;
  990. }
  991. /* }}} */