spl_observer.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Marcus Boerger <helly@php.net> |
  16. | Etienne Kneuss <colder@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. # include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "ext/standard/php_array.h"
  27. #include "ext/standard/php_var.h"
  28. #include "ext/standard/php_smart_str.h"
  29. #include "zend_interfaces.h"
  30. #include "zend_exceptions.h"
  31. #include "php_spl.h"
  32. #include "spl_functions.h"
  33. #include "spl_engine.h"
  34. #include "spl_observer.h"
  35. #include "spl_iterators.h"
  36. #include "spl_array.h"
  37. #include "spl_exceptions.h"
  38. SPL_METHOD(SplObserver, update);
  39. SPL_METHOD(SplSubject, attach);
  40. SPL_METHOD(SplSubject, detach);
  41. SPL_METHOD(SplSubject, notify);
  42. ZEND_BEGIN_ARG_INFO(arginfo_SplObserver_update, 0)
  43. ZEND_ARG_OBJ_INFO(0, SplSubject, SplSubject, 0)
  44. ZEND_END_ARG_INFO();
  45. static const zend_function_entry spl_funcs_SplObserver[] = {
  46. SPL_ABSTRACT_ME(SplObserver, update, arginfo_SplObserver_update)
  47. {NULL, NULL, NULL}
  48. };
  49. ZEND_BEGIN_ARG_INFO(arginfo_SplSubject_attach, 0)
  50. ZEND_ARG_OBJ_INFO(0, SplObserver, SplObserver, 0)
  51. ZEND_END_ARG_INFO();
  52. ZEND_BEGIN_ARG_INFO(arginfo_SplSubject_void, 0)
  53. ZEND_END_ARG_INFO();
  54. /*ZEND_BEGIN_ARG_INFO_EX(arginfo_SplSubject_notify, 0, 0, 1)
  55. ZEND_ARG_OBJ_INFO(0, ignore, SplObserver, 1)
  56. ZEND_END_ARG_INFO();*/
  57. static const zend_function_entry spl_funcs_SplSubject[] = {
  58. SPL_ABSTRACT_ME(SplSubject, attach, arginfo_SplSubject_attach)
  59. SPL_ABSTRACT_ME(SplSubject, detach, arginfo_SplSubject_attach)
  60. SPL_ABSTRACT_ME(SplSubject, notify, arginfo_SplSubject_void)
  61. {NULL, NULL, NULL}
  62. };
  63. PHPAPI zend_class_entry *spl_ce_SplObserver;
  64. PHPAPI zend_class_entry *spl_ce_SplSubject;
  65. PHPAPI zend_class_entry *spl_ce_SplObjectStorage;
  66. PHPAPI zend_class_entry *spl_ce_MultipleIterator;
  67. PHPAPI zend_object_handlers spl_handler_SplObjectStorage;
  68. typedef struct _spl_SplObjectStorage { /* {{{ */
  69. zend_object std;
  70. HashTable storage;
  71. long index;
  72. HashPosition pos;
  73. long flags;
  74. zend_function *fptr_get_hash;
  75. HashTable *debug_info;
  76. zval **gcdata;
  77. long gcdata_num;
  78. } spl_SplObjectStorage; /* }}} */
  79. /* {{{ storage is an assoc aray of [zend_object_value]=>[zval *obj, zval *inf] */
  80. typedef struct _spl_SplObjectStorageElement {
  81. zval* obj;
  82. zval* inf;
  83. } spl_SplObjectStorageElement; /* }}} */
  84. void spl_SplOjectStorage_free_storage(void *object TSRMLS_DC) /* {{{ */
  85. {
  86. spl_SplObjectStorage *intern = (spl_SplObjectStorage *)object;
  87. zend_object_std_dtor(&intern->std TSRMLS_CC);
  88. zend_hash_destroy(&intern->storage);
  89. if (intern->debug_info != NULL) {
  90. zend_hash_destroy(intern->debug_info);
  91. efree(intern->debug_info);
  92. }
  93. if (intern->gcdata != NULL) {
  94. efree(intern->gcdata);
  95. }
  96. efree(object);
  97. } /* }}} */
  98. static char *spl_object_storage_get_hash(spl_SplObjectStorage *intern, zval *this, zval *obj, int *hash_len_ptr TSRMLS_DC) {
  99. if (intern->fptr_get_hash) {
  100. zval *rv;
  101. zend_call_method_with_1_params(&this, intern->std.ce, &intern->fptr_get_hash, "getHash", &rv, obj);
  102. if (rv) {
  103. if (Z_TYPE_P(rv) == IS_STRING) {
  104. int hash_len = Z_STRLEN_P(rv);
  105. char *hash = emalloc((hash_len+1)*sizeof(char));
  106. strncpy(hash, Z_STRVAL_P(rv), hash_len);
  107. hash[hash_len] = 0;
  108. zval_ptr_dtor(&rv);
  109. if (hash_len_ptr) {
  110. *hash_len_ptr = hash_len;
  111. }
  112. return hash;
  113. } else {
  114. zend_throw_exception(spl_ce_RuntimeException, "Hash needs to be a string", 0 TSRMLS_CC);
  115. zval_ptr_dtor(&rv);
  116. return NULL;
  117. }
  118. } else {
  119. return NULL;
  120. }
  121. } else {
  122. int hash_len = sizeof(zend_object_value);
  123. #if HAVE_PACKED_OBJECT_VALUE
  124. if (hash_len_ptr) {
  125. *hash_len_ptr = hash_len;
  126. }
  127. return (char*)&Z_OBJVAL_P(obj);
  128. #else
  129. char *hash = emalloc(hash_len + 1);
  130. zend_object_value zvalue;
  131. memset(&zvalue, 0, sizeof(zend_object_value));
  132. zvalue.handle = Z_OBJ_HANDLE_P(obj);
  133. zvalue.handlers = Z_OBJ_HT_P(obj);
  134. memcpy(hash, (char *)&zvalue, hash_len);
  135. hash[hash_len] = 0;
  136. if (hash_len_ptr) {
  137. *hash_len_ptr = hash_len;
  138. }
  139. return hash;
  140. #endif
  141. }
  142. }
  143. static void spl_object_storage_free_hash(spl_SplObjectStorage *intern, char *hash) {
  144. if (intern->fptr_get_hash) {
  145. efree(hash);
  146. } else {
  147. #if HAVE_PACKED_OBJECT_VALUE
  148. /* Nothing to do */
  149. #else
  150. efree(hash);
  151. #endif
  152. }
  153. }
  154. static void spl_object_storage_dtor(spl_SplObjectStorageElement *element) /* {{{ */
  155. {
  156. zval_ptr_dtor(&element->obj);
  157. zval_ptr_dtor(&element->inf);
  158. } /* }}} */
  159. spl_SplObjectStorageElement* spl_object_storage_get(spl_SplObjectStorage *intern, char *hash, int hash_len TSRMLS_DC) /* {{{ */
  160. {
  161. spl_SplObjectStorageElement *element;
  162. if (zend_hash_find(&intern->storage, hash, hash_len, (void**)&element) == SUCCESS) {
  163. return element;
  164. } else {
  165. return NULL;
  166. }
  167. } /* }}} */
  168. void spl_object_storage_attach(spl_SplObjectStorage *intern, zval *this, zval *obj, zval *inf TSRMLS_DC) /* {{{ */
  169. {
  170. spl_SplObjectStorageElement *pelement, element;
  171. int hash_len;
  172. char *hash = spl_object_storage_get_hash(intern, this, obj, &hash_len TSRMLS_CC);
  173. if (!hash) {
  174. return;
  175. }
  176. pelement = spl_object_storage_get(intern, hash, hash_len TSRMLS_CC);
  177. if (inf) {
  178. Z_ADDREF_P(inf);
  179. } else {
  180. ALLOC_INIT_ZVAL(inf);
  181. }
  182. if (pelement) {
  183. zval_ptr_dtor(&pelement->inf);
  184. pelement->inf = inf;
  185. spl_object_storage_free_hash(intern, hash);
  186. return;
  187. }
  188. Z_ADDREF_P(obj);
  189. element.obj = obj;
  190. element.inf = inf;
  191. zend_hash_update(&intern->storage, hash, hash_len, &element, sizeof(spl_SplObjectStorageElement), NULL);
  192. spl_object_storage_free_hash(intern, hash);
  193. } /* }}} */
  194. int spl_object_storage_detach(spl_SplObjectStorage *intern, zval *this, zval *obj TSRMLS_DC) /* {{{ */
  195. {
  196. int hash_len, ret = FAILURE;
  197. char *hash = spl_object_storage_get_hash(intern, this, obj, &hash_len TSRMLS_CC);
  198. if (!hash) {
  199. return ret;
  200. }
  201. ret = zend_hash_del(&intern->storage, hash, hash_len);
  202. spl_object_storage_free_hash(intern, hash);
  203. return ret;
  204. } /* }}}*/
  205. void spl_object_storage_addall(spl_SplObjectStorage *intern, zval *this, spl_SplObjectStorage *other TSRMLS_DC) { /* {{{ */
  206. HashPosition pos;
  207. spl_SplObjectStorageElement *element;
  208. zend_hash_internal_pointer_reset_ex(&other->storage, &pos);
  209. while (zend_hash_get_current_data_ex(&other->storage, (void **)&element, &pos) == SUCCESS) {
  210. spl_object_storage_attach(intern, this, element->obj, element->inf TSRMLS_CC);
  211. zend_hash_move_forward_ex(&other->storage, &pos);
  212. }
  213. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  214. intern->index = 0;
  215. } /* }}} */
  216. static zend_object_value spl_object_storage_new_ex(zend_class_entry *class_type, spl_SplObjectStorage **obj, zval *orig TSRMLS_DC) /* {{{ */
  217. {
  218. zend_object_value retval;
  219. spl_SplObjectStorage *intern;
  220. zend_class_entry *parent = class_type;
  221. intern = emalloc(sizeof(spl_SplObjectStorage));
  222. memset(intern, 0, sizeof(spl_SplObjectStorage));
  223. *obj = intern;
  224. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  225. object_properties_init(&intern->std, class_type);
  226. zend_hash_init(&intern->storage, 0, NULL, (void (*)(void *))spl_object_storage_dtor, 0);
  227. retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) spl_SplOjectStorage_free_storage, NULL TSRMLS_CC);
  228. retval.handlers = &spl_handler_SplObjectStorage;
  229. while (parent) {
  230. if (parent == spl_ce_SplObjectStorage) {
  231. if (class_type != spl_ce_SplObjectStorage) {
  232. zend_hash_find(&class_type->function_table, "gethash", sizeof("gethash"), (void **) &intern->fptr_get_hash);
  233. if (intern->fptr_get_hash->common.scope == spl_ce_SplObjectStorage) {
  234. intern->fptr_get_hash = NULL;
  235. }
  236. }
  237. break;
  238. }
  239. parent = parent->parent;
  240. }
  241. if (orig) {
  242. spl_SplObjectStorage *other = (spl_SplObjectStorage*)zend_object_store_get_object(orig TSRMLS_CC);
  243. spl_object_storage_addall(intern, orig, other TSRMLS_CC);
  244. }
  245. return retval;
  246. }
  247. /* }}} */
  248. /* {{{ spl_object_storage_clone */
  249. static zend_object_value spl_object_storage_clone(zval *zobject TSRMLS_DC)
  250. {
  251. zend_object_value new_obj_val;
  252. zend_object *old_object;
  253. zend_object *new_object;
  254. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  255. spl_SplObjectStorage *intern;
  256. old_object = zend_objects_get_address(zobject TSRMLS_CC);
  257. new_obj_val = spl_object_storage_new_ex(old_object->ce, &intern, zobject TSRMLS_CC);
  258. new_object = &intern->std;
  259. zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
  260. return new_obj_val;
  261. }
  262. /* }}} */
  263. static HashTable* spl_object_storage_debug_info(zval *obj, int *is_temp TSRMLS_DC) /* {{{ */
  264. {
  265. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(obj TSRMLS_CC);
  266. spl_SplObjectStorageElement *element;
  267. HashTable *props;
  268. HashPosition pos;
  269. zval *tmp, *storage;
  270. char md5str[33];
  271. int name_len;
  272. char *zname;
  273. *is_temp = 0;
  274. props = Z_OBJPROP_P(obj);
  275. if (intern->debug_info == NULL) {
  276. ALLOC_HASHTABLE(intern->debug_info);
  277. ZEND_INIT_SYMTABLE_EX(intern->debug_info, zend_hash_num_elements(props) + 1, 0);
  278. }
  279. if (intern->debug_info->nApplyCount == 0) {
  280. zend_hash_copy(intern->debug_info, props, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  281. MAKE_STD_ZVAL(storage);
  282. array_init(storage);
  283. zend_hash_internal_pointer_reset_ex(&intern->storage, &pos);
  284. while (zend_hash_get_current_data_ex(&intern->storage, (void **)&element, &pos) == SUCCESS) {
  285. php_spl_object_hash(element->obj, md5str TSRMLS_CC);
  286. MAKE_STD_ZVAL(tmp);
  287. array_init(tmp);
  288. /* Incrementing the refcount of obj and inf would confuse the garbage collector.
  289. * Prefer to null the destructor */
  290. Z_ARRVAL_P(tmp)->pDestructor = NULL;
  291. add_assoc_zval_ex(tmp, "obj", sizeof("obj"), element->obj);
  292. add_assoc_zval_ex(tmp, "inf", sizeof("inf"), element->inf);
  293. add_assoc_zval_ex(storage, md5str, 33, tmp);
  294. zend_hash_move_forward_ex(&intern->storage, &pos);
  295. }
  296. zname = spl_gen_private_prop_name(spl_ce_SplObjectStorage, "storage", sizeof("storage")-1, &name_len TSRMLS_CC);
  297. zend_symtable_update(intern->debug_info, zname, name_len+1, &storage, sizeof(zval *), NULL);
  298. efree(zname);
  299. }
  300. return intern->debug_info;
  301. }
  302. /* }}} */
  303. /* overriden for garbage collection */
  304. static HashTable *spl_object_storage_get_gc(zval *obj, zval ***table, int *n TSRMLS_DC) /* {{{ */
  305. {
  306. long i = 0;
  307. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(obj TSRMLS_CC);
  308. spl_SplObjectStorageElement *element;
  309. HashPosition pos;
  310. if (intern->storage.nNumOfElements * 2 > intern->gcdata_num) {
  311. intern->gcdata_num = intern->storage.nNumOfElements * 2;
  312. intern->gcdata = (zval**)erealloc(intern->gcdata, sizeof(zval*) * intern->gcdata_num);
  313. }
  314. zend_hash_internal_pointer_reset_ex(&intern->storage, &pos);
  315. while (zend_hash_get_current_data_ex(&intern->storage, (void **)&element, &pos) == SUCCESS) {
  316. intern->gcdata[i++] = element->obj;
  317. intern->gcdata[i++] = element->inf;
  318. zend_hash_move_forward_ex(&intern->storage, &pos);
  319. }
  320. *table = intern->gcdata;
  321. *n = i;
  322. return std_object_handlers.get_properties(obj TSRMLS_CC);
  323. }
  324. /* }}} */
  325. static int spl_object_storage_compare_info(spl_SplObjectStorageElement *e1, spl_SplObjectStorageElement *e2 TSRMLS_DC) /* {{{ */
  326. {
  327. zval result;
  328. if (compare_function(&result, e1->inf, e2->inf TSRMLS_CC) == FAILURE) {
  329. return 1;
  330. }
  331. return Z_LVAL(result);
  332. }
  333. /* }}} */
  334. static int spl_object_storage_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
  335. {
  336. zend_object *zo1 = (zend_object *)zend_object_store_get_object(o1 TSRMLS_CC);
  337. zend_object *zo2 = (zend_object *)zend_object_store_get_object(o2 TSRMLS_CC);
  338. if (zo1->ce != spl_ce_SplObjectStorage || zo2->ce != spl_ce_SplObjectStorage) {
  339. return 1;
  340. }
  341. return zend_hash_compare(&((spl_SplObjectStorage *)zo1)->storage, &((spl_SplObjectStorage *)zo2)->storage, (compare_func_t) spl_object_storage_compare_info, 0 TSRMLS_CC);
  342. }
  343. /* }}} */
  344. /* {{{ spl_array_object_new */
  345. static zend_object_value spl_SplObjectStorage_new(zend_class_entry *class_type TSRMLS_DC)
  346. {
  347. spl_SplObjectStorage *tmp;
  348. return spl_object_storage_new_ex(class_type, &tmp, NULL TSRMLS_CC);
  349. }
  350. /* }}} */
  351. int spl_object_storage_contains(spl_SplObjectStorage *intern, zval *this, zval *obj TSRMLS_DC) /* {{{ */
  352. {
  353. int hash_len, found;
  354. char *hash = spl_object_storage_get_hash(intern, this, obj, &hash_len TSRMLS_CC);
  355. if (!hash) {
  356. return 0;
  357. }
  358. found = zend_hash_exists(&intern->storage, hash, hash_len);
  359. spl_object_storage_free_hash(intern, hash);
  360. return found;
  361. } /* }}} */
  362. /* {{{ proto void SplObjectStorage::attach($obj, $inf = NULL)
  363. Attaches an object to the storage if not yet contained */
  364. SPL_METHOD(SplObjectStorage, attach)
  365. {
  366. zval *obj, *inf = NULL;
  367. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  368. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o|z!", &obj, &inf) == FAILURE) {
  369. return;
  370. }
  371. spl_object_storage_attach(intern, getThis(), obj, inf TSRMLS_CC);
  372. } /* }}} */
  373. /* {{{ proto void SplObjectStorage::detach($obj)
  374. Detaches an object from the storage */
  375. SPL_METHOD(SplObjectStorage, detach)
  376. {
  377. zval *obj;
  378. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  379. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
  380. return;
  381. }
  382. spl_object_storage_detach(intern, getThis(), obj TSRMLS_CC);
  383. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  384. intern->index = 0;
  385. } /* }}} */
  386. /* {{{ proto string SplObjectStorage::getHash($object)
  387. Returns the hash of an object */
  388. SPL_METHOD(SplObjectStorage, getHash)
  389. {
  390. zval *obj;
  391. char *hash;
  392. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
  393. return;
  394. }
  395. hash = emalloc(33);
  396. php_spl_object_hash(obj, hash TSRMLS_CC);
  397. RETVAL_STRING(hash, 0);
  398. } /* }}} */
  399. /* {{{ proto mixed SplObjectStorage::offsetGet($object)
  400. Returns associated information for a stored object */
  401. SPL_METHOD(SplObjectStorage, offsetGet)
  402. {
  403. zval *obj;
  404. spl_SplObjectStorageElement *element;
  405. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  406. char *hash;
  407. int hash_len;
  408. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
  409. return;
  410. }
  411. hash = spl_object_storage_get_hash(intern, getThis(), obj, &hash_len TSRMLS_CC);
  412. if (!hash) {
  413. return;
  414. }
  415. element = spl_object_storage_get(intern, hash, hash_len TSRMLS_CC);
  416. spl_object_storage_free_hash(intern, hash);
  417. if (!element) {
  418. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Object not found");
  419. } else {
  420. RETURN_ZVAL(element->inf,1, 0);
  421. }
  422. } /* }}} */
  423. /* {{{ proto bool SplObjectStorage::addAll(SplObjectStorage $os)
  424. Add all elements contained in $os */
  425. SPL_METHOD(SplObjectStorage, addAll)
  426. {
  427. zval *obj;
  428. spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
  429. spl_SplObjectStorage *other;
  430. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
  431. return;
  432. }
  433. other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
  434. spl_object_storage_addall(intern, getThis(), other TSRMLS_CC);
  435. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  436. } /* }}} */
  437. /* {{{ proto bool SplObjectStorage::removeAll(SplObjectStorage $os)
  438. Remove all elements contained in $os */
  439. SPL_METHOD(SplObjectStorage, removeAll)
  440. {
  441. zval *obj;
  442. spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
  443. spl_SplObjectStorage *other;
  444. spl_SplObjectStorageElement *element;
  445. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
  446. return;
  447. }
  448. other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
  449. zend_hash_internal_pointer_reset(&other->storage);
  450. while (zend_hash_get_current_data(&other->storage, (void **)&element) == SUCCESS) {
  451. if (spl_object_storage_detach(intern, getThis(), element->obj TSRMLS_CC) == FAILURE) {
  452. zend_hash_move_forward(&other->storage);
  453. }
  454. }
  455. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  456. intern->index = 0;
  457. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  458. } /* }}} */
  459. /* {{{ proto bool SplObjectStorage::removeAllExcept(SplObjectStorage $os)
  460. Remove elements not common to both this SplObjectStorage instance and $os */
  461. SPL_METHOD(SplObjectStorage, removeAllExcept)
  462. {
  463. zval *obj;
  464. spl_SplObjectStorage *intern = (spl_SplObjectStorage *)zend_object_store_get_object(getThis() TSRMLS_CC);
  465. spl_SplObjectStorage *other;
  466. spl_SplObjectStorageElement *element;
  467. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, spl_ce_SplObjectStorage) == FAILURE) {
  468. return;
  469. }
  470. other = (spl_SplObjectStorage *)zend_object_store_get_object(obj TSRMLS_CC);
  471. zend_hash_internal_pointer_reset(&intern->storage);
  472. while (zend_hash_get_current_data(&intern->storage, (void **)&element) == SUCCESS) {
  473. if (!spl_object_storage_contains(other, getThis(), element->obj TSRMLS_CC)) {
  474. spl_object_storage_detach(intern, getThis(), element->obj TSRMLS_CC);
  475. }
  476. zend_hash_move_forward(&intern->storage);
  477. }
  478. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  479. intern->index = 0;
  480. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  481. }
  482. /* }}} */
  483. /* {{{ proto bool SplObjectStorage::contains($obj)
  484. Determine whethe an object is contained in the storage */
  485. SPL_METHOD(SplObjectStorage, contains)
  486. {
  487. zval *obj;
  488. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  489. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
  490. return;
  491. }
  492. RETURN_BOOL(spl_object_storage_contains(intern, getThis(), obj TSRMLS_CC));
  493. } /* }}} */
  494. /* {{{ proto int SplObjectStorage::count()
  495. Determine number of objects in storage */
  496. SPL_METHOD(SplObjectStorage, count)
  497. {
  498. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  499. long mode = COUNT_NORMAL;
  500. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
  501. return;
  502. }
  503. if (mode == COUNT_RECURSIVE) {
  504. long ret = zend_hash_num_elements(&intern->storage);
  505. HashPosition position;
  506. zval *element;
  507. for (zend_hash_internal_pointer_reset_ex(&intern->storage, &position);
  508. zend_hash_get_current_data_ex(&intern->storage, (void**) &element, &position) == SUCCESS;
  509. zend_hash_move_forward_ex(&intern->storage, &position)) {
  510. ret += php_count_recursive(element, mode TSRMLS_CC);
  511. }
  512. RETURN_LONG(ret);
  513. return;
  514. }
  515. RETURN_LONG(zend_hash_num_elements(&intern->storage));
  516. } /* }}} */
  517. /* {{{ proto void SplObjectStorage::rewind()
  518. Rewind to first position */
  519. SPL_METHOD(SplObjectStorage, rewind)
  520. {
  521. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  522. if (zend_parse_parameters_none() == FAILURE) {
  523. return;
  524. }
  525. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  526. intern->index = 0;
  527. } /* }}} */
  528. /* {{{ proto bool SplObjectStorage::valid()
  529. Returns whether current position is valid */
  530. SPL_METHOD(SplObjectStorage, valid)
  531. {
  532. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  533. if (zend_parse_parameters_none() == FAILURE) {
  534. return;
  535. }
  536. RETURN_BOOL(zend_hash_has_more_elements_ex(&intern->storage, &intern->pos) == SUCCESS);
  537. } /* }}} */
  538. /* {{{ proto mixed SplObjectStorage::key()
  539. Returns current key */
  540. SPL_METHOD(SplObjectStorage, key)
  541. {
  542. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  543. if (zend_parse_parameters_none() == FAILURE) {
  544. return;
  545. }
  546. RETURN_LONG(intern->index);
  547. } /* }}} */
  548. /* {{{ proto mixed SplObjectStorage::current()
  549. Returns current element */
  550. SPL_METHOD(SplObjectStorage, current)
  551. {
  552. spl_SplObjectStorageElement *element;
  553. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  554. if (zend_parse_parameters_none() == FAILURE) {
  555. return;
  556. }
  557. if (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == FAILURE) {
  558. return;
  559. }
  560. RETVAL_ZVAL(element->obj, 1, 0);
  561. } /* }}} */
  562. /* {{{ proto mixed SplObjectStorage::getInfo()
  563. Returns associated information to current element */
  564. SPL_METHOD(SplObjectStorage, getInfo)
  565. {
  566. spl_SplObjectStorageElement *element;
  567. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  568. if (zend_parse_parameters_none() == FAILURE) {
  569. return;
  570. }
  571. if (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == FAILURE) {
  572. return;
  573. }
  574. RETVAL_ZVAL(element->inf, 1, 0);
  575. } /* }}} */
  576. /* {{{ proto mixed SplObjectStorage::setInfo(mixed $inf)
  577. Sets associated information of current element to $inf */
  578. SPL_METHOD(SplObjectStorage, setInfo)
  579. {
  580. spl_SplObjectStorageElement *element;
  581. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  582. zval *inf;
  583. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &inf) == FAILURE) {
  584. return;
  585. }
  586. if (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == FAILURE) {
  587. return;
  588. }
  589. zval_ptr_dtor(&element->inf);
  590. element->inf = inf;
  591. Z_ADDREF_P(inf);
  592. } /* }}} */
  593. /* {{{ proto void SplObjectStorage::next()
  594. Moves position forward */
  595. SPL_METHOD(SplObjectStorage, next)
  596. {
  597. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  598. if (zend_parse_parameters_none() == FAILURE) {
  599. return;
  600. }
  601. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  602. intern->index++;
  603. } /* }}} */
  604. /* {{{ proto string SplObjectStorage::serialize()
  605. Serializes storage */
  606. SPL_METHOD(SplObjectStorage, serialize)
  607. {
  608. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  609. spl_SplObjectStorageElement *element;
  610. zval members, *pmembers, *flags;
  611. HashPosition pos;
  612. php_serialize_data_t var_hash;
  613. smart_str buf = {0};
  614. if (zend_parse_parameters_none() == FAILURE) {
  615. return;
  616. }
  617. PHP_VAR_SERIALIZE_INIT(var_hash);
  618. /* storage */
  619. smart_str_appendl(&buf, "x:", 2);
  620. MAKE_STD_ZVAL(flags);
  621. ZVAL_LONG(flags, zend_hash_num_elements(&intern->storage));
  622. php_var_serialize(&buf, &flags, &var_hash TSRMLS_CC);
  623. zval_ptr_dtor(&flags);
  624. zend_hash_internal_pointer_reset_ex(&intern->storage, &pos);
  625. while(zend_hash_has_more_elements_ex(&intern->storage, &pos) == SUCCESS) {
  626. if (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &pos) == FAILURE) {
  627. smart_str_free(&buf);
  628. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  629. RETURN_NULL();
  630. }
  631. php_var_serialize(&buf, &element->obj, &var_hash TSRMLS_CC);
  632. smart_str_appendc(&buf, ',');
  633. php_var_serialize(&buf, &element->inf, &var_hash TSRMLS_CC);
  634. smart_str_appendc(&buf, ';');
  635. zend_hash_move_forward_ex(&intern->storage, &pos);
  636. }
  637. /* members */
  638. smart_str_appendl(&buf, "m:", 2);
  639. INIT_PZVAL(&members);
  640. Z_ARRVAL(members) = zend_std_get_properties(getThis() TSRMLS_CC);
  641. Z_TYPE(members) = IS_ARRAY;
  642. pmembers = &members;
  643. php_var_serialize(&buf, &pmembers, &var_hash TSRMLS_CC); /* finishes the string */
  644. /* done */
  645. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  646. if (buf.c) {
  647. RETURN_STRINGL(buf.c, buf.len, 0);
  648. } else {
  649. RETURN_NULL();
  650. }
  651. } /* }}} */
  652. /* {{{ proto void SplObjectStorage::unserialize(string serialized)
  653. Unserializes storage */
  654. SPL_METHOD(SplObjectStorage, unserialize)
  655. {
  656. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  657. char *buf;
  658. int buf_len;
  659. const unsigned char *p, *s;
  660. php_unserialize_data_t var_hash;
  661. zval *pentry, *pmembers, *pcount = NULL, *pinf;
  662. long count;
  663. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &buf_len) == FAILURE) {
  664. return;
  665. }
  666. if (buf_len == 0) {
  667. return;
  668. }
  669. /* storage */
  670. s = p = (const unsigned char*)buf;
  671. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  672. if (*p!= 'x' || *++p != ':') {
  673. goto outexcept;
  674. }
  675. ++p;
  676. ALLOC_INIT_ZVAL(pcount);
  677. if (!php_var_unserialize(&pcount, &p, s + buf_len, &var_hash TSRMLS_CC) || Z_TYPE_P(pcount) != IS_LONG) {
  678. goto outexcept;
  679. }
  680. var_push_dtor(&var_hash, &pcount);
  681. --p; /* for ';' */
  682. count = Z_LVAL_P(pcount);
  683. while(count-- > 0) {
  684. spl_SplObjectStorageElement *pelement;
  685. char *hash;
  686. int hash_len;
  687. if (*p != ';') {
  688. goto outexcept;
  689. }
  690. ++p;
  691. if(*p != 'O' && *p != 'C' && *p != 'r') {
  692. goto outexcept;
  693. }
  694. ALLOC_INIT_ZVAL(pentry);
  695. if (!php_var_unserialize(&pentry, &p, s + buf_len, &var_hash TSRMLS_CC)) {
  696. zval_ptr_dtor(&pentry);
  697. goto outexcept;
  698. }
  699. var_push_dtor(&var_hash, &pentry);
  700. if(Z_TYPE_P(pentry) != IS_OBJECT) {
  701. zval_ptr_dtor(&pentry);
  702. goto outexcept;
  703. }
  704. ALLOC_INIT_ZVAL(pinf);
  705. if (*p == ',') { /* new version has inf */
  706. ++p;
  707. if (!php_var_unserialize(&pinf, &p, s + buf_len, &var_hash TSRMLS_CC)) {
  708. zval_ptr_dtor(&pinf);
  709. goto outexcept;
  710. }
  711. var_push_dtor(&var_hash, &pinf);
  712. }
  713. hash = spl_object_storage_get_hash(intern, getThis(), pentry, &hash_len TSRMLS_CC);
  714. if (!hash) {
  715. zval_ptr_dtor(&pentry);
  716. zval_ptr_dtor(&pinf);
  717. goto outexcept;
  718. }
  719. pelement = spl_object_storage_get(intern, hash, hash_len TSRMLS_CC);
  720. spl_object_storage_free_hash(intern, hash);
  721. if(pelement) {
  722. if(pelement->inf) {
  723. var_push_dtor(&var_hash, &pelement->inf);
  724. }
  725. if(pelement->obj) {
  726. var_push_dtor(&var_hash, &pelement->obj);
  727. }
  728. }
  729. spl_object_storage_attach(intern, getThis(), pentry, pinf TSRMLS_CC);
  730. zval_ptr_dtor(&pentry);
  731. zval_ptr_dtor(&pinf);
  732. }
  733. if (*p != ';') {
  734. goto outexcept;
  735. }
  736. ++p;
  737. /* members */
  738. if (*p!= 'm' || *++p != ':') {
  739. goto outexcept;
  740. }
  741. ++p;
  742. ALLOC_INIT_ZVAL(pmembers);
  743. if (!php_var_unserialize(&pmembers, &p, s + buf_len, &var_hash TSRMLS_CC) || Z_TYPE_P(pmembers) != IS_ARRAY) {
  744. zval_ptr_dtor(&pmembers);
  745. goto outexcept;
  746. }
  747. var_push_dtor(&var_hash, &pmembers);
  748. /* copy members */
  749. if (!intern->std.properties) {
  750. rebuild_object_properties(&intern->std);
  751. }
  752. zend_hash_copy(intern->std.properties, Z_ARRVAL_P(pmembers), (copy_ctor_func_t) zval_add_ref, (void *) NULL, sizeof(zval *));
  753. zval_ptr_dtor(&pmembers);
  754. /* done reading $serialized */
  755. if (pcount) {
  756. zval_ptr_dtor(&pcount);
  757. }
  758. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  759. return;
  760. outexcept:
  761. if (pcount) {
  762. zval_ptr_dtor(&pcount);
  763. }
  764. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  765. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Error at offset %ld of %d bytes", (long)((char*)p - buf), buf_len);
  766. return;
  767. } /* }}} */
  768. ZEND_BEGIN_ARG_INFO(arginfo_Object, 0)
  769. ZEND_ARG_INFO(0, object)
  770. ZEND_END_ARG_INFO();
  771. ZEND_BEGIN_ARG_INFO_EX(arginfo_attach, 0, 0, 1)
  772. ZEND_ARG_INFO(0, object)
  773. ZEND_ARG_INFO(0, inf)
  774. ZEND_END_ARG_INFO();
  775. ZEND_BEGIN_ARG_INFO(arginfo_Serialized, 0)
  776. ZEND_ARG_INFO(0, serialized)
  777. ZEND_END_ARG_INFO();
  778. ZEND_BEGIN_ARG_INFO(arginfo_setInfo, 0)
  779. ZEND_ARG_INFO(0, info)
  780. ZEND_END_ARG_INFO();
  781. ZEND_BEGIN_ARG_INFO(arginfo_getHash, 0)
  782. ZEND_ARG_INFO(0, object)
  783. ZEND_END_ARG_INFO();
  784. ZEND_BEGIN_ARG_INFO_EX(arginfo_offsetGet, 0, 0, 1)
  785. ZEND_ARG_INFO(0, object)
  786. ZEND_END_ARG_INFO()
  787. ZEND_BEGIN_ARG_INFO(arginfo_splobject_void, 0)
  788. ZEND_END_ARG_INFO()
  789. static const zend_function_entry spl_funcs_SplObjectStorage[] = {
  790. SPL_ME(SplObjectStorage, attach, arginfo_attach, 0)
  791. SPL_ME(SplObjectStorage, detach, arginfo_Object, 0)
  792. SPL_ME(SplObjectStorage, contains, arginfo_Object, 0)
  793. SPL_ME(SplObjectStorage, addAll, arginfo_Object, 0)
  794. SPL_ME(SplObjectStorage, removeAll, arginfo_Object, 0)
  795. SPL_ME(SplObjectStorage, removeAllExcept, arginfo_Object, 0)
  796. SPL_ME(SplObjectStorage, getInfo, arginfo_splobject_void,0)
  797. SPL_ME(SplObjectStorage, setInfo, arginfo_setInfo, 0)
  798. SPL_ME(SplObjectStorage, getHash, arginfo_getHash, 0)
  799. /* Countable */
  800. SPL_ME(SplObjectStorage, count, arginfo_splobject_void,0)
  801. /* Iterator */
  802. SPL_ME(SplObjectStorage, rewind, arginfo_splobject_void,0)
  803. SPL_ME(SplObjectStorage, valid, arginfo_splobject_void,0)
  804. SPL_ME(SplObjectStorage, key, arginfo_splobject_void,0)
  805. SPL_ME(SplObjectStorage, current, arginfo_splobject_void,0)
  806. SPL_ME(SplObjectStorage, next, arginfo_splobject_void,0)
  807. /* Serializable */
  808. SPL_ME(SplObjectStorage, unserialize, arginfo_Serialized, 0)
  809. SPL_ME(SplObjectStorage, serialize, arginfo_splobject_void,0)
  810. /* ArrayAccess */
  811. SPL_MA(SplObjectStorage, offsetExists, SplObjectStorage, contains, arginfo_offsetGet, 0)
  812. SPL_MA(SplObjectStorage, offsetSet, SplObjectStorage, attach, arginfo_attach, 0)
  813. SPL_MA(SplObjectStorage, offsetUnset, SplObjectStorage, detach, arginfo_offsetGet, 0)
  814. SPL_ME(SplObjectStorage, offsetGet, arginfo_offsetGet, 0)
  815. {NULL, NULL, NULL}
  816. };
  817. typedef enum {
  818. MIT_NEED_ANY = 0,
  819. MIT_NEED_ALL = 1,
  820. MIT_KEYS_NUMERIC = 0,
  821. MIT_KEYS_ASSOC = 2
  822. } MultipleIteratorFlags;
  823. #define SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT 1
  824. #define SPL_MULTIPLE_ITERATOR_GET_ALL_KEY 2
  825. /* {{{ proto void MultipleIterator::__construct([int flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC])
  826. Iterator that iterates over several iterators one after the other */
  827. SPL_METHOD(MultipleIterator, __construct)
  828. {
  829. spl_SplObjectStorage *intern;
  830. long flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC;
  831. zend_error_handling error_handling;
  832. zend_replace_error_handling(EH_THROW, spl_ce_InvalidArgumentException, &error_handling TSRMLS_CC);
  833. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags) == FAILURE) {
  834. zend_restore_error_handling(&error_handling TSRMLS_CC);
  835. return;
  836. }
  837. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  838. intern->flags = flags;
  839. zend_restore_error_handling(&error_handling TSRMLS_CC);
  840. }
  841. /* }}} */
  842. /* {{{ proto int MultipleIterator::getFlags()
  843. Return current flags */
  844. SPL_METHOD(MultipleIterator, getFlags)
  845. {
  846. spl_SplObjectStorage *intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  847. if (zend_parse_parameters_none() == FAILURE) {
  848. return;
  849. }
  850. RETURN_LONG(intern->flags);
  851. }
  852. /* }}} */
  853. /* {{{ proto int MultipleIterator::setFlags(int flags)
  854. Set flags */
  855. SPL_METHOD(MultipleIterator, setFlags)
  856. {
  857. spl_SplObjectStorage *intern;
  858. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  859. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &intern->flags) == FAILURE) {
  860. return;
  861. }
  862. }
  863. /* }}} */
  864. /* {{{ proto void attachIterator(Iterator iterator[, mixed info]) throws InvalidArgumentException
  865. Attach a new iterator */
  866. SPL_METHOD(MultipleIterator, attachIterator)
  867. {
  868. spl_SplObjectStorage *intern;
  869. zval *iterator = NULL, *info = NULL;
  870. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|z!", &iterator, zend_ce_iterator, &info) == FAILURE) {
  871. return;
  872. }
  873. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  874. if (info != NULL) {
  875. spl_SplObjectStorageElement *element;
  876. zval compare_result;
  877. if (Z_TYPE_P(info) != IS_LONG && Z_TYPE_P(info) != IS_STRING) {
  878. zend_throw_exception(spl_ce_InvalidArgumentException, "Info must be NULL, integer or string", 0 TSRMLS_CC);
  879. return;
  880. }
  881. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  882. while (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == SUCCESS) {
  883. is_identical_function(&compare_result, info, element->inf TSRMLS_CC);
  884. if (Z_LVAL(compare_result)) {
  885. zend_throw_exception(spl_ce_InvalidArgumentException, "Key duplication error", 0 TSRMLS_CC);
  886. return;
  887. }
  888. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  889. }
  890. }
  891. spl_object_storage_attach(intern, getThis(), iterator, info TSRMLS_CC);
  892. }
  893. /* }}} */
  894. /* {{{ proto void MultipleIterator::rewind()
  895. Rewind all attached iterator instances */
  896. SPL_METHOD(MultipleIterator, rewind)
  897. {
  898. spl_SplObjectStorage *intern;
  899. spl_SplObjectStorageElement *element;
  900. zval *it;
  901. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  902. if (zend_parse_parameters_none() == FAILURE) {
  903. return;
  904. }
  905. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  906. while (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == SUCCESS && !EG(exception)) {
  907. it = element->obj;
  908. zend_call_method_with_0_params(&it, Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs.zf_rewind, "rewind", NULL);
  909. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  910. }
  911. }
  912. /* }}} */
  913. /* {{{ proto void MultipleIterator::next()
  914. Move all attached iterator instances forward */
  915. SPL_METHOD(MultipleIterator, next)
  916. {
  917. spl_SplObjectStorage *intern;
  918. spl_SplObjectStorageElement *element;
  919. zval *it;
  920. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  921. if (zend_parse_parameters_none() == FAILURE) {
  922. return;
  923. }
  924. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  925. while (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == SUCCESS && !EG(exception)) {
  926. it = element->obj;
  927. zend_call_method_with_0_params(&it, Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs.zf_next, "next", NULL);
  928. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  929. }
  930. }
  931. /* }}} */
  932. /* {{{ proto bool MultipleIterator::valid()
  933. Return whether all or one sub iterator is valid depending on flags */
  934. SPL_METHOD(MultipleIterator, valid)
  935. {
  936. spl_SplObjectStorage *intern;
  937. spl_SplObjectStorageElement *element;
  938. zval *it, *retval = NULL;
  939. long expect, valid;
  940. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  941. if (zend_parse_parameters_none() == FAILURE) {
  942. return;
  943. }
  944. if (!zend_hash_num_elements(&intern->storage)) {
  945. RETURN_FALSE;
  946. }
  947. expect = (intern->flags & MIT_NEED_ALL) ? 1 : 0;
  948. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  949. while (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == SUCCESS && !EG(exception)) {
  950. it = element->obj;
  951. zend_call_method_with_0_params(&it, Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs.zf_valid, "valid", &retval);
  952. if (retval) {
  953. valid = Z_LVAL_P(retval);
  954. zval_ptr_dtor(&retval);
  955. } else {
  956. valid = 0;
  957. }
  958. if (expect != valid) {
  959. RETURN_BOOL(!expect);
  960. }
  961. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  962. }
  963. RETURN_BOOL(expect);
  964. }
  965. /* }}} */
  966. static void spl_multiple_iterator_get_all(spl_SplObjectStorage *intern, int get_type, zval *return_value TSRMLS_DC) /* {{{ */
  967. {
  968. spl_SplObjectStorageElement *element;
  969. zval *it, *retval = NULL;
  970. int valid = 1, num_elements;
  971. num_elements = zend_hash_num_elements(&intern->storage);
  972. if (num_elements < 1) {
  973. RETURN_FALSE;
  974. }
  975. array_init_size(return_value, num_elements);
  976. zend_hash_internal_pointer_reset_ex(&intern->storage, &intern->pos);
  977. while (zend_hash_get_current_data_ex(&intern->storage, (void**)&element, &intern->pos) == SUCCESS && !EG(exception)) {
  978. it = element->obj;
  979. zend_call_method_with_0_params(&it, Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs.zf_valid, "valid", &retval);
  980. if (retval) {
  981. valid = Z_LVAL_P(retval);
  982. zval_ptr_dtor(&retval);
  983. } else {
  984. valid = 0;
  985. }
  986. if (valid) {
  987. if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
  988. zend_call_method_with_0_params(&it, Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs.zf_current, "current", &retval);
  989. } else {
  990. zend_call_method_with_0_params(&it, Z_OBJCE_P(it), &Z_OBJCE_P(it)->iterator_funcs.zf_key, "key", &retval);
  991. }
  992. if (!retval) {
  993. zend_throw_exception(spl_ce_RuntimeException, "Failed to call sub iterator method", 0 TSRMLS_CC);
  994. return;
  995. }
  996. } else if (intern->flags & MIT_NEED_ALL) {
  997. if (SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT == get_type) {
  998. zend_throw_exception(spl_ce_RuntimeException, "Called current() with non valid sub iterator", 0 TSRMLS_CC);
  999. } else {
  1000. zend_throw_exception(spl_ce_RuntimeException, "Called key() with non valid sub iterator", 0 TSRMLS_CC);
  1001. }
  1002. return;
  1003. } else {
  1004. ALLOC_INIT_ZVAL(retval);
  1005. }
  1006. if (intern->flags & MIT_KEYS_ASSOC) {
  1007. switch (Z_TYPE_P(element->inf)) {
  1008. case IS_LONG:
  1009. add_index_zval(return_value, Z_LVAL_P(element->inf), retval);
  1010. break;
  1011. case IS_STRING:
  1012. add_assoc_zval_ex(return_value, Z_STRVAL_P(element->inf), Z_STRLEN_P(element->inf)+1U, retval);
  1013. break;
  1014. default:
  1015. zval_ptr_dtor(&retval);
  1016. zend_throw_exception(spl_ce_InvalidArgumentException, "Sub-Iterator is associated with NULL", 0 TSRMLS_CC);
  1017. return;
  1018. }
  1019. } else {
  1020. add_next_index_zval(return_value, retval);
  1021. }
  1022. zend_hash_move_forward_ex(&intern->storage, &intern->pos);
  1023. }
  1024. }
  1025. /* }}} */
  1026. /* {{{ proto array current() throws RuntimeException throws InvalidArgumentException
  1027. Return an array of all registered Iterator instances current() result */
  1028. SPL_METHOD(MultipleIterator, current)
  1029. {
  1030. spl_SplObjectStorage *intern;
  1031. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1032. if (zend_parse_parameters_none() == FAILURE) {
  1033. return;
  1034. }
  1035. spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_CURRENT, return_value TSRMLS_CC);
  1036. }
  1037. /* }}} */
  1038. /* {{{ proto array MultipleIterator::key()
  1039. Return an array of all registered Iterator instances key() result */
  1040. SPL_METHOD(MultipleIterator, key)
  1041. {
  1042. spl_SplObjectStorage *intern;
  1043. intern = (spl_SplObjectStorage*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1044. if (zend_parse_parameters_none() == FAILURE) {
  1045. return;
  1046. }
  1047. spl_multiple_iterator_get_all(intern, SPL_MULTIPLE_ITERATOR_GET_ALL_KEY, return_value TSRMLS_CC);
  1048. }
  1049. /* }}} */
  1050. ZEND_BEGIN_ARG_INFO_EX(arginfo_MultipleIterator_attachIterator, 0, 0, 1)
  1051. ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
  1052. ZEND_ARG_INFO(0, infos)
  1053. ZEND_END_ARG_INFO();
  1054. ZEND_BEGIN_ARG_INFO_EX(arginfo_MultipleIterator_detachIterator, 0, 0, 1)
  1055. ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
  1056. ZEND_END_ARG_INFO();
  1057. ZEND_BEGIN_ARG_INFO_EX(arginfo_MultipleIterator_containsIterator, 0, 0, 1)
  1058. ZEND_ARG_OBJ_INFO(0, iterator, Iterator, 0)
  1059. ZEND_END_ARG_INFO();
  1060. ZEND_BEGIN_ARG_INFO_EX(arginfo_MultipleIterator_setflags, 0, 0, 1)
  1061. ZEND_ARG_INFO(0, flags)
  1062. ZEND_END_ARG_INFO();
  1063. static const zend_function_entry spl_funcs_MultipleIterator[] = {
  1064. SPL_ME(MultipleIterator, __construct, arginfo_MultipleIterator_setflags, 0)
  1065. SPL_ME(MultipleIterator, getFlags, arginfo_splobject_void, 0)
  1066. SPL_ME(MultipleIterator, setFlags, arginfo_MultipleIterator_setflags, 0)
  1067. SPL_ME(MultipleIterator, attachIterator, arginfo_MultipleIterator_attachIterator, 0)
  1068. SPL_MA(MultipleIterator, detachIterator, SplObjectStorage, detach, arginfo_MultipleIterator_detachIterator, 0)
  1069. SPL_MA(MultipleIterator, containsIterator, SplObjectStorage, contains, arginfo_MultipleIterator_containsIterator, 0)
  1070. SPL_MA(MultipleIterator, countIterators, SplObjectStorage, count, arginfo_splobject_void, 0)
  1071. /* Iterator */
  1072. SPL_ME(MultipleIterator, rewind, arginfo_splobject_void, 0)
  1073. SPL_ME(MultipleIterator, valid, arginfo_splobject_void, 0)
  1074. SPL_ME(MultipleIterator, key, arginfo_splobject_void, 0)
  1075. SPL_ME(MultipleIterator, current, arginfo_splobject_void, 0)
  1076. SPL_ME(MultipleIterator, next, arginfo_splobject_void, 0)
  1077. {NULL, NULL, NULL}
  1078. };
  1079. /* {{{ PHP_MINIT_FUNCTION(spl_observer) */
  1080. PHP_MINIT_FUNCTION(spl_observer)
  1081. {
  1082. REGISTER_SPL_INTERFACE(SplObserver);
  1083. REGISTER_SPL_INTERFACE(SplSubject);
  1084. REGISTER_SPL_STD_CLASS_EX(SplObjectStorage, spl_SplObjectStorage_new, spl_funcs_SplObjectStorage);
  1085. memcpy(&spl_handler_SplObjectStorage, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1086. spl_handler_SplObjectStorage.get_debug_info = spl_object_storage_debug_info;
  1087. spl_handler_SplObjectStorage.compare_objects = spl_object_storage_compare_objects;
  1088. spl_handler_SplObjectStorage.clone_obj = spl_object_storage_clone;
  1089. spl_handler_SplObjectStorage.get_gc = spl_object_storage_get_gc;
  1090. REGISTER_SPL_IMPLEMENTS(SplObjectStorage, Countable);
  1091. REGISTER_SPL_IMPLEMENTS(SplObjectStorage, Iterator);
  1092. REGISTER_SPL_IMPLEMENTS(SplObjectStorage, Serializable);
  1093. REGISTER_SPL_IMPLEMENTS(SplObjectStorage, ArrayAccess);
  1094. REGISTER_SPL_STD_CLASS_EX(MultipleIterator, spl_SplObjectStorage_new, spl_funcs_MultipleIterator);
  1095. REGISTER_SPL_ITERATOR(MultipleIterator);
  1096. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_NEED_ANY", MIT_NEED_ANY);
  1097. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_NEED_ALL", MIT_NEED_ALL);
  1098. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_KEYS_NUMERIC", MIT_KEYS_NUMERIC);
  1099. REGISTER_SPL_CLASS_CONST_LONG(MultipleIterator, "MIT_KEYS_ASSOC", MIT_KEYS_ASSOC);
  1100. return SUCCESS;
  1101. }
  1102. /* }}} */
  1103. /*
  1104. * Local variables:
  1105. * tab-width: 4
  1106. * c-basic-offset: 4
  1107. * End:
  1108. * vim600: fdm=marker
  1109. * vim: noet sw=4 ts=4
  1110. */