Skip to content

Commit 043a439

Browse files
committed
Merge pull request #486
2 parents 4d2b8f2 + 0dd057d commit 043a439

File tree

4 files changed

+226
-180
lines changed

4 files changed

+226
-180
lines changed

php_phongo.c

Lines changed: 0 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,182 +1677,6 @@ void php_phongo_new_regex_from_regex_and_options(zval *object, const char *patte
16771677
intern->flags = estrndup(flags, intern->flags_len);
16781678
} /* }}} */
16791679

1680-
static void php_phongo_cursor_free_current(php_phongo_cursor_t *cursor) /* {{{ */
1681-
{
1682-
if (!Z_ISUNDEF(cursor->visitor_data.zchild)) {
1683-
zval_ptr_dtor(&cursor->visitor_data.zchild);
1684-
#if PHP_VERSION_ID >= 70000
1685-
ZVAL_UNDEF(&cursor->visitor_data.zchild);
1686-
#else
1687-
cursor->visitor_data.zchild = NULL;
1688-
#endif
1689-
}
1690-
} /* }}} */
1691-
1692-
void php_phongo_cursor_free(php_phongo_cursor_t *cursor) /* {{{ */
1693-
{
1694-
if (cursor->cursor) {
1695-
mongoc_cursor_destroy(cursor->cursor);
1696-
cursor->cursor = NULL;
1697-
}
1698-
1699-
php_phongo_cursor_free_current(cursor);
1700-
} /* }}} */
1701-
1702-
/* {{{ Iterator */
1703-
static void php_phongo_cursor_iterator_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1704-
{
1705-
php_phongo_cursor_iterator *cursor_it = (php_phongo_cursor_iterator *)iter;
1706-
1707-
if (!Z_ISUNDEF(cursor_it->intern.data)) {
1708-
#if PHP_VERSION_ID >= 70000
1709-
zval_ptr_dtor(&cursor_it->intern.data);
1710-
#else
1711-
zval_ptr_dtor((zval**)&cursor_it->intern.data);
1712-
cursor_it->intern.data = NULL;
1713-
#endif
1714-
}
1715-
1716-
#if PHP_VERSION_ID < 70000
1717-
efree(cursor_it);
1718-
#endif
1719-
} /* }}} */
1720-
1721-
static int php_phongo_cursor_iterator_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1722-
{
1723-
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
1724-
1725-
if (!Z_ISUNDEF(cursor->visitor_data.zchild)) {
1726-
return SUCCESS;
1727-
}
1728-
1729-
return FAILURE;
1730-
} /* }}} */
1731-
1732-
#if PHP_VERSION_ID < 50500
1733-
static int php_phongo_cursor_iterator_get_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC) /* {{{ */
1734-
{
1735-
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
1736-
1737-
*int_key = (ulong) cursor->current;
1738-
return HASH_KEY_IS_LONG;
1739-
} /* }}} */
1740-
#else
1741-
static void php_phongo_cursor_iterator_get_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC) /* {{{ */
1742-
{
1743-
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
1744-
1745-
ZVAL_LONG(key, cursor->current);
1746-
} /* }}} */
1747-
#endif
1748-
1749-
#if PHP_VERSION_ID < 70000
1750-
static void php_phongo_cursor_iterator_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
1751-
{
1752-
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
1753-
1754-
*data = &cursor->visitor_data.zchild;
1755-
} /* }}} */
1756-
#else
1757-
static zval* php_phongo_cursor_iterator_get_current_data(zend_object_iterator *iter) /* {{{ */
1758-
{
1759-
php_phongo_cursor_t *cursor = ((php_phongo_cursor_iterator *)iter)->cursor;
1760-
1761-
return &cursor->visitor_data.zchild;
1762-
} /* }}} */
1763-
#endif
1764-
1765-
static void php_phongo_cursor_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1766-
{
1767-
php_phongo_cursor_iterator *cursor_it = (php_phongo_cursor_iterator *)iter;
1768-
php_phongo_cursor_t *cursor = cursor_it->cursor;
1769-
const bson_t *doc;
1770-
1771-
php_phongo_cursor_free_current(cursor);
1772-
cursor->current++;
1773-
1774-
if (mongoc_cursor_next(cursor->cursor, &doc)) {
1775-
phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &cursor->visitor_data);
1776-
} else {
1777-
bson_error_t error;
1778-
1779-
if (mongoc_cursor_error(cursor->cursor, &error)) {
1780-
/* Intentionally not destroying the cursor as it will happen
1781-
* naturally now that there are no more results */
1782-
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
1783-
}
1784-
}
1785-
} /* }}} */
1786-
1787-
static void php_phongo_cursor_iterator_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
1788-
{
1789-
php_phongo_cursor_iterator *cursor_it = (php_phongo_cursor_iterator *)iter;
1790-
php_phongo_cursor_t *cursor = cursor_it->cursor;
1791-
const bson_t *doc;
1792-
1793-
if (cursor->current > 0) {
1794-
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Cursors cannot rewind after starting iteration");
1795-
return;
1796-
}
1797-
1798-
php_phongo_cursor_free_current(cursor);
1799-
1800-
doc = mongoc_cursor_current(cursor->cursor);
1801-
1802-
if (doc) {
1803-
phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &cursor->visitor_data);
1804-
}
1805-
} /* }}} */
1806-
1807-
/* iterator handler table */
1808-
zend_object_iterator_funcs php_phongo_cursor_iterator_funcs = {
1809-
php_phongo_cursor_iterator_dtor,
1810-
php_phongo_cursor_iterator_valid,
1811-
php_phongo_cursor_iterator_get_current_data,
1812-
php_phongo_cursor_iterator_get_current_key,
1813-
php_phongo_cursor_iterator_move_forward,
1814-
php_phongo_cursor_iterator_rewind,
1815-
NULL /* invalidate_current is not used */
1816-
};
1817-
1818-
zend_object_iterator *php_phongo_cursor_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
1819-
{
1820-
php_phongo_cursor_iterator *cursor_it = NULL;
1821-
php_phongo_cursor_t *cursor = Z_CURSOR_OBJ_P(object);
1822-
1823-
if (by_ref) {
1824-
zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1825-
}
1826-
1827-
if (cursor->got_iterator) {
1828-
phongo_throw_exception(PHONGO_ERROR_LOGIC TSRMLS_CC, "Cursors cannot yield multiple iterators");
1829-
return NULL;
1830-
}
1831-
1832-
cursor->got_iterator = 1;
1833-
1834-
cursor_it = ecalloc(1, sizeof(php_phongo_cursor_iterator));
1835-
#if PHP_VERSION_ID >= 70000
1836-
zend_iterator_init(&cursor_it->intern);
1837-
#endif
1838-
1839-
#if PHP_VERSION_ID >= 70000
1840-
ZVAL_COPY(&cursor_it->intern.data, object);
1841-
#else
1842-
Z_ADDREF_P(object);
1843-
cursor_it->intern.data = (void*)object;
1844-
#endif
1845-
cursor_it->intern.funcs = &php_phongo_cursor_iterator_funcs;
1846-
cursor_it->cursor = cursor;
1847-
/* cursor_it->current should already be allocated to zero */
1848-
1849-
php_phongo_cursor_free_current(cursor_it->cursor);
1850-
1851-
return &cursor_it->intern;
1852-
} /* }}} */
1853-
/* }}} */
1854-
1855-
18561680
/* {{{ Memory allocation wrappers */
18571681
static void* php_phongo_malloc(size_t num_bytes) /* {{{ */
18581682
{

php_phongo.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ void php_phongo_new_regex_from_regex_and_options(zval *object, const char *patte
154154
zend_bool phongo_writeerror_init(zval *return_value, bson_t *bson TSRMLS_DC);
155155
zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_DC);
156156

157-
void php_phongo_cursor_free(php_phongo_cursor_t *cursor);
158-
zend_object_iterator* php_phongo_cursor_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC);
159-
160157
#if PHP_VERSION_ID >= 70000
161158
#define PHONGO_CE_FINAL(ce) do { \
162159
ce->ce_flags |= ZEND_ACC_FINAL; \

0 commit comments

Comments
 (0)