spl_observer.c 37 KB

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