Skip to content

Commit a327a22

Browse files
committed
Merge branch 'v1.1'
2 parents aa5d50c + df285ce commit a327a22

File tree

14 files changed

+344
-174
lines changed

14 files changed

+344
-174
lines changed

phongo_compat.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@
4343
# define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT
4444
#endif
4545

46-
#if PHP_VERSION_ID < 50400
47-
# define object_properties_init(_std, _class_type) \
48-
zend_hash_copy(*_std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
49-
#endif
50-
5146
#if PHP_VERSION_ID >= 70000
5247
# define str_efree(s) efree((char*)s)
53-
#elif PHP_VERSION_ID < 50400
54-
# define str_efree(s) efree((char*)s)
5548
#else
5649
# include <Zend/zend_string.h>
5750
#endif
@@ -102,15 +95,6 @@
10295
# endif
10396
#endif
10497

105-
#if PHP_VERSION_ID < 50400
106-
# define GET_DEFAULT_CONTEXT() \
107-
ctx = FG(default_context) ? FG(default_context) : php_stream_context_alloc()
108-
#else
109-
# define GET_DEFAULT_CONTEXT() \
110-
ctx = FG(default_context) ? FG(default_context) : php_stream_context_alloc(TSRMLS_C)
111-
#endif
112-
113-
11498
#ifndef php_ignore_value
11599
# if defined(__GNUC__) && __GNUC__ >= 4
116100
# define php_ignore_value(x) (({ __typeof__ (x) __x = (x); (void) __x; }))

php_phongo.c

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static void php_phongo_log(mongoc_log_level_t log_level, const char *log_domain,
234234
/* }}} */
235235

236236
/* {{{ Init objects */
237-
static void phongo_cursor_init(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client, zval *readPreference TSRMLS_DC) /* {{{ */
237+
static void phongo_cursor_init(zval *return_value, zval *manager, mongoc_cursor_t *cursor, zval *readPreference TSRMLS_DC) /* {{{ */
238238
{
239239
php_phongo_cursor_t *intern;
240240

@@ -243,7 +243,14 @@ static void phongo_cursor_init(zval *return_value, mongoc_cursor_t *cursor, mong
243243
intern = Z_CURSOR_OBJ_P(return_value);
244244
intern->cursor = cursor;
245245
intern->server_id = mongoc_cursor_get_hint(cursor);
246-
intern->client = client;
246+
intern->client = Z_MANAGER_OBJ_P(manager)->client;
247+
248+
#if PHP_VERSION_ID >= 70000
249+
ZVAL_COPY(&intern->manager, manager);
250+
#else
251+
Z_ADDREF_P(manager);
252+
intern->manager = manager;
253+
#endif
247254

248255
if (readPreference) {
249256
#if PHP_VERSION_ID >= 70000
@@ -255,11 +262,11 @@ static void phongo_cursor_init(zval *return_value, mongoc_cursor_t *cursor, mong
255262
}
256263
} /* }}} */
257264

258-
static void phongo_cursor_init_for_command(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client, const char *db, zval *command, zval *readPreference TSRMLS_DC) /* {{{ */
265+
static void phongo_cursor_init_for_command(zval *return_value, zval *manager, mongoc_cursor_t *cursor, const char *db, zval *command, zval *readPreference TSRMLS_DC) /* {{{ */
259266
{
260267
php_phongo_cursor_t *intern;
261268

262-
phongo_cursor_init(return_value, cursor, client, readPreference TSRMLS_CC);
269+
phongo_cursor_init(return_value, manager, cursor, readPreference TSRMLS_CC);
263270
intern = Z_CURSOR_OBJ_P(return_value);
264271

265272
intern->database = estrdup(db);
@@ -272,11 +279,11 @@ static void phongo_cursor_init_for_command(zval *return_value, mongoc_cursor_t *
272279
#endif
273280
} /* }}} */
274281

275-
static void phongo_cursor_init_for_query(zval *return_value, mongoc_cursor_t *cursor, mongoc_client_t *client, const char *namespace, zval *query, zval *readPreference TSRMLS_DC) /* {{{ */
282+
static void phongo_cursor_init_for_query(zval *return_value, zval *manager, mongoc_cursor_t *cursor, const char *namespace, zval *query, zval *readPreference TSRMLS_DC) /* {{{ */
276283
{
277284
php_phongo_cursor_t *intern;
278285

279-
phongo_cursor_init(return_value, cursor, client, readPreference TSRMLS_CC);
286+
phongo_cursor_init(return_value, manager, cursor, readPreference TSRMLS_CC);
280287
intern = Z_CURSOR_OBJ_P(return_value);
281288

282289
/* namespace has already been validated by phongo_execute_query() */
@@ -290,15 +297,22 @@ static void phongo_cursor_init_for_query(zval *return_value, mongoc_cursor_t *cu
290297
#endif
291298
} /* }}} */
292299

293-
void phongo_server_init(zval *return_value, mongoc_client_t *client, int server_id TSRMLS_DC) /* {{{ */
300+
void phongo_server_init(zval *return_value, zval *manager, int server_id TSRMLS_DC) /* {{{ */
294301
{
295302
php_phongo_server_t *server;
296303

297304
object_init_ex(return_value, php_phongo_server_ce);
298305

299306
server = Z_SERVER_OBJ_P(return_value);
300-
server->client = client;
301307
server->server_id = server_id;
308+
server->client = Z_MANAGER_OBJ_P(manager)->client;
309+
310+
#if PHP_VERSION_ID >= 70000
311+
ZVAL_COPY(&server->manager, manager);
312+
#else
313+
Z_ADDREF_P(manager);
314+
server->manager = manager;
315+
#endif
302316
}
303317
/* }}} */
304318

@@ -521,16 +535,23 @@ zend_bool phongo_writeerror_init(zval *return_value, bson_t *bson TSRMLS_DC) /*
521535
return true;
522536
} /* }}} */
523537

524-
php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, bson_t *reply, mongoc_client_t *client, int server_id TSRMLS_DC) /* {{{ */
538+
php_phongo_writeresult_t *phongo_writeresult_init(zval *return_value, bson_t *reply, zval *manager, int server_id TSRMLS_DC) /* {{{ */
525539
{
526540
php_phongo_writeresult_t *writeresult;
527541

528542
object_init_ex(return_value, php_phongo_writeresult_ce);
529543

530544
writeresult = Z_WRITERESULT_OBJ_P(return_value);
531545
writeresult->reply = bson_copy(reply);
532-
writeresult->client = client;
533546
writeresult->server_id = server_id;
547+
writeresult->client = Z_MANAGER_OBJ_P(manager)->client;
548+
549+
#if PHP_VERSION_ID >= 70000
550+
ZVAL_COPY(&writeresult->manager, manager);
551+
#else
552+
Z_ADDREF_P(manager);
553+
writeresult->manager = manager;
554+
#endif
534555

535556
return writeresult;
536557
} /* }}} */
@@ -560,14 +581,17 @@ mongoc_bulk_operation_t *phongo_bulkwrite_init(zend_bool ordered) { /* {{{ */
560581
return mongoc_bulk_operation_new(ordered);
561582
} /* }}} */
562583

563-
bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
584+
bool phongo_execute_write(zval *manager, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
564585
{
586+
mongoc_client_t *client;
565587
bson_error_t error;
566588
int success;
567589
bson_t reply = BSON_INITIALIZER;
568590
mongoc_bulk_operation_t *bulk = bulk_write->bulk;
569591
php_phongo_writeresult_t *writeresult;
570592

593+
client = Z_MANAGER_OBJ_P(manager)->client;
594+
571595
/* Since BulkWrite objects can currently be executed multiple times, ensure
572596
* that the database and collection name are freed before we overwrite them.
573597
* This may be removed once PHPC-676 is implemented. */
@@ -615,7 +639,7 @@ bool phongo_execute_write(mongoc_client_t *client, const char *namespace, php_ph
615639
return false;
616640
}
617641

618-
writeresult = phongo_writeresult_init(return_value, &reply, client, mongoc_bulk_operation_get_hint(bulk) TSRMLS_CC);
642+
writeresult = phongo_writeresult_init(return_value, &reply, manager, mongoc_bulk_operation_get_hint(bulk) TSRMLS_CC);
619643
writeresult->write_concern = mongoc_write_concern_copy(write_concern);
620644

621645
/* The Write failed */
@@ -659,14 +683,17 @@ static bool phongo_advance_cursor_and_check_for_error(mongoc_cursor_t *cursor TS
659683
return true;
660684
}
661685

662-
int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
686+
int phongo_execute_query(zval *manager, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
663687
{
688+
mongoc_client_t *client;
664689
const php_phongo_query_t *query;
665690
mongoc_cursor_t *cursor;
666691
char *dbname;
667692
char *collname;
668693
mongoc_collection_t *collection;
669694

695+
client = Z_MANAGER_OBJ_P(manager)->client;
696+
670697
if (!phongo_split_namespace(namespace, &dbname, &collname)) {
671698
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "%s: %s", "Invalid namespace provided", namespace);
672699
return false;
@@ -704,16 +731,18 @@ int phongo_execute_query(mongoc_client_t *client, const char *namespace, zval *z
704731
return true;
705732
}
706733

707-
phongo_cursor_init_for_query(return_value, cursor, client, namespace, zquery, zreadPreference TSRMLS_CC);
734+
phongo_cursor_init_for_query(return_value, manager, cursor, namespace, zquery, zreadPreference TSRMLS_CC);
708735
return true;
709736
} /* }}} */
710737

711-
int phongo_execute_command(mongoc_client_t *client, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
738+
int phongo_execute_command(zval *manager, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC) /* {{{ */
712739
{
740+
mongoc_client_t *client;
713741
const php_phongo_command_t *command;
714742
mongoc_cursor_t *cursor;
715743
bson_iter_t iter;
716744

745+
client = Z_MANAGER_OBJ_P(manager)->client;
717746
command = Z_COMMAND_OBJ_P(zcommand);
718747

719748
cursor = mongoc_client_command(client, db, MONGOC_QUERY_NONE, 0, 1, 0, command->bson, NULL, phongo_read_preference_from_zval(zreadPreference TSRMLS_CC));
@@ -745,11 +774,11 @@ int phongo_execute_command(mongoc_client_t *client, const char *db, zval *zcomma
745774
return false;
746775
}
747776

748-
phongo_cursor_init_for_command(return_value, cmd_cursor, client, db, zcommand, zreadPreference TSRMLS_CC);
777+
phongo_cursor_init_for_command(return_value, manager, cmd_cursor, db, zcommand, zreadPreference TSRMLS_CC);
749778
return true;
750779
}
751780

752-
phongo_cursor_init_for_command(return_value, cursor, client, db, zcommand, zreadPreference TSRMLS_CC);
781+
phongo_cursor_init_for_command(return_value, manager, cursor, db, zcommand, zreadPreference TSRMLS_CC);
753782
return true;
754783
} /* }}} */
755784

@@ -1847,9 +1876,11 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t *uri, bson_t *option
18471876
static mongoc_client_t *php_phongo_make_mongo_client(php_phongo_manager_t *manager, const mongoc_uri_t *uri, zval *driverOptions TSRMLS_DC) /* {{{ */
18481877
{
18491878
#if PHP_VERSION_ID >= 70000
1850-
zval *tmp;
1879+
zval *zdebug = NULL;
1880+
zval *zcontext = NULL;
18511881
#else
1852-
zval **tmp;
1882+
zval **zdebug = NULL;
1883+
zval **zcontext = NULL;
18531884
#endif
18541885
php_stream_context *ctx = NULL;
18551886
const char *mech, *mongoc_version, *bson_version;
@@ -1858,30 +1889,31 @@ static mongoc_client_t *php_phongo_make_mongo_client(php_phongo_manager_t *manag
18581889
ENTRY;
18591890

18601891
#if PHP_VERSION_ID >= 70000
1861-
if (driverOptions && (tmp = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "debug", sizeof("debug")-1)) != NULL) {
1892+
if (driverOptions && (zdebug = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "debug", sizeof("debug")-1)) != NULL) {
18621893
zend_string *key = zend_string_init(PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI)-1, 0);
1863-
zend_string *value_str = zval_get_string(tmp);
1894+
zend_string *value_str = zval_get_string(zdebug);
18641895
zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
18651896
zend_string_release(key);
18661897
zend_string_release(value_str);
18671898
}
18681899
#else
1869-
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "debug", strlen("debug") + 1, (void**)&tmp) == SUCCESS) {
1870-
convert_to_string(*tmp);
1900+
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "debug", strlen("debug") + 1, (void**)&zdebug) == SUCCESS) {
1901+
convert_to_string(*zdebug);
18711902

1872-
zend_alter_ini_entry_ex((char *)PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI), Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
1903+
zend_alter_ini_entry_ex((char *)PHONGO_DEBUG_INI, sizeof(PHONGO_DEBUG_INI), Z_STRVAL_PP(zdebug), Z_STRLEN_PP(zdebug), PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0 TSRMLS_CC);
18731904
}
18741905
#endif
18751906

18761907
#if PHP_VERSION_ID >= 70000
1877-
if (driverOptions && (tmp = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "context", sizeof("context")-1)) != NULL) {
1878-
ctx = php_stream_context_from_zval(tmp, 0);
1908+
if (driverOptions && (zcontext = zend_hash_str_find(Z_ARRVAL_P(driverOptions), "context", sizeof("context")-1)) != NULL) {
1909+
ctx = php_stream_context_from_zval(zcontext, 0);
18791910
#else
1880-
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "context", strlen("context") + 1, (void**)&tmp) == SUCCESS) {
1881-
ctx = php_stream_context_from_zval(*tmp, 0);
1911+
if (driverOptions && zend_hash_find(Z_ARRVAL_P(driverOptions), "context", strlen("context") + 1, (void**)&zcontext) == SUCCESS) {
1912+
ctx = php_stream_context_from_zval(*zcontext, 0);
18821913
#endif
18831914
} else {
1884-
GET_DEFAULT_CONTEXT();
1915+
zval *tmp = NULL; /* PHP 5.x requires an lvalue */
1916+
ctx = php_stream_context_from_zval(tmp, 0);
18851917
}
18861918

18871919
if (mongoc_uri_get_ssl(uri)) {
@@ -1983,6 +2015,17 @@ bool phongo_manager_init(php_phongo_manager_t *manager, const char *uri_string,
19832015
return false;
19842016
}
19852017

2018+
/* Keep a reference to driverOptions, since it may be referenced later for
2019+
* lazy stream initialization. */
2020+
if (driverOptions) {
2021+
#if PHP_VERSION_ID >= 70000
2022+
ZVAL_COPY(&manager->driverOptions, driverOptions);
2023+
#else
2024+
Z_ADDREF_P(driverOptions);
2025+
manager->driverOptions = driverOptions;
2026+
#endif
2027+
}
2028+
19862029
return true;
19872030
} /* }}} */
19882031

php_phongo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ void phongo_throw_exception_from_bson_error_t(bson_error_t *error TSRMLS_DC);
136136

137137
PHONGO_API zend_object_handlers *phongo_get_std_object_handlers(void);
138138

139-
void phongo_server_init (zval *return_value, mongoc_client_t *client, int server_id TSRMLS_DC);
139+
void phongo_server_init (zval *return_value, zval *manager, int server_id TSRMLS_DC);
140140
void phongo_readconcern_init (zval *return_value, const mongoc_read_concern_t *read_concern TSRMLS_DC);
141141
void phongo_readpreference_init (zval *return_value, const mongoc_read_prefs_t *read_prefs TSRMLS_DC);
142142
void phongo_writeconcern_init (zval *return_value, const mongoc_write_concern_t *write_concern TSRMLS_DC);
143143
bool phongo_query_init (php_phongo_query_t *query, bson_t *filter, bson_t *options TSRMLS_DC);
144144
mongoc_bulk_operation_t* phongo_bulkwrite_init (zend_bool ordered);
145-
bool phongo_execute_write (mongoc_client_t *client, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
146-
int phongo_execute_command (mongoc_client_t *client, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
147-
int phongo_execute_query (mongoc_client_t *client, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
145+
bool phongo_execute_write (zval *manager, const char *namespace, php_phongo_bulkwrite_t *bulk_write, const mongoc_write_concern_t *write_concern, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
146+
int phongo_execute_command (zval *manager, const char *db, zval *zcommand, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
147+
int phongo_execute_query (zval *manager, const char *namespace, zval *zquery, zval *zreadPreference, int server_id, zval *return_value, int return_value_used TSRMLS_DC);
148148

149149
mongoc_stream_t* phongo_stream_initiator (const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error);
150150
const mongoc_read_concern_t* phongo_read_concern_from_zval (zval *zread_concern TSRMLS_DC);

0 commit comments

Comments
 (0)