Skip to content

Commit 997fe40

Browse files
committed
PHPC-878: Remove dependency on php_json_serializable_ce symbol
Since JsonSerializable is required, this also adds a helpful startup error message if json.so is not already loaded.
1 parent 521b3af commit 997fe40

File tree

11 files changed

+48
-18
lines changed

11 files changed

+48
-18
lines changed

php_phongo.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ ZEND_DECLARE_MODULE_GLOBALS(mongodb)
7373
#endif
7474
#endif
7575

76+
/* Declare zend_class_entry dependencies, which are initialized in MINIT */
77+
zend_class_entry *php_phongo_json_serializable_ce;
78+
7679
php_phongo_server_description_type_map_t
7780
php_phongo_server_description_type_map[PHONGO_SERVER_DESCRIPTION_TYPES] = {
7881
{ PHONGO_SERVER_UNKNOWN, "Unknown" },
@@ -1854,6 +1857,25 @@ PHP_GINIT_FUNCTION(mongodb)
18541857
}
18551858
/* }}} */
18561859

1860+
static zend_class_entry *php_phongo_fetch_internal_class(const char *class_name, size_t class_name_len TSRMLS_DC)
1861+
{
1862+
#if PHP_VERSION_ID >= 70000
1863+
zend_class_entry *pce;
1864+
1865+
if ((pce = zend_hash_str_find_ptr(CG(class_table), class_name, class_name_len))) {
1866+
return pce;
1867+
}
1868+
#else
1869+
zend_class_entry **pce;
1870+
1871+
if (zend_hash_find(CG(class_table), class_name, class_name_len + 1, (void **) &pce) == SUCCESS) {
1872+
return *pce;
1873+
}
1874+
#endif
1875+
1876+
return NULL;
1877+
}
1878+
18571879
/* {{{ PHP_MINIT_FUNCTION */
18581880
PHP_MINIT_FUNCTION(mongodb)
18591881
{
@@ -1886,6 +1908,20 @@ PHP_MINIT_FUNCTION(mongodb)
18861908
phongo_std_object_handlers.get_closure = NULL;
18871909
*/
18881910

1911+
/* Initialize zend_class_entry dependencies.
1912+
*
1913+
* Although JsonSerializable was introduced in PHP 5.4.0,
1914+
* php_json_serializable_ce is not exported in PHP versions before 5.4.26
1915+
* and 5.5.10. For later PHP versions, looking up the class manually also
1916+
* helps with distros that disable LTDL_LAZY for dlopen() (e.g. Fedora).
1917+
*/
1918+
php_phongo_json_serializable_ce = php_phongo_fetch_internal_class(ZEND_STRL("jsonserializable") TSRMLS_CC);
1919+
1920+
if (php_phongo_json_serializable_ce == NULL) {
1921+
zend_error(E_ERROR, "JsonSerializable class is not defined. Please ensure that the 'json' module is loaded before the 'mongodb' module.");
1922+
return FAILURE;
1923+
}
1924+
18891925
PHP_MINIT(bson)(INIT_FUNC_ARGS_PASSTHRU);
18901926

18911927
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "php_phongo_structs.h"
2121

22+
/* Export zend_class_entry dependencies, which are initialized in MINIT */
23+
extern zend_class_entry *php_phongo_json_serializable_ce;
24+
2225
#if PHP_VERSION_ID >= 70000
2326

2427
static inline php_phongo_bulkwrite_t* php_bulkwrite_fetch_object(zend_object *obj) {

src/BSON/Binary.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/base64.h>
3029
#include <ext/standard/info.h>
3130
#include <Zend/zend_interfaces.h>
@@ -453,7 +452,7 @@ PHP_MINIT_FUNCTION(Binary)
453452
php_phongo_binary_ce->create_object = php_phongo_binary_create_object;
454453
PHONGO_CE_FINAL(php_phongo_binary_ce);
455454

456-
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_json_serializable_ce);
455+
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
457456
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, php_phongo_type_ce);
458457
zend_class_implements(php_phongo_binary_ce TSRMLS_CC, 1, zend_ce_serializable);
459458

src/BSON/Decimal128.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -368,7 +367,7 @@ PHP_MINIT_FUNCTION(Decimal128)
368367
php_phongo_decimal128_ce->create_object = php_phongo_decimal128_create_object;
369368
PHONGO_CE_FINAL(php_phongo_decimal128_ce);
370369

371-
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, php_json_serializable_ce);
370+
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
372371
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, php_phongo_type_ce);
373372
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, zend_ce_serializable);
374373

src/BSON/Javascript.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -533,7 +532,7 @@ PHP_MINIT_FUNCTION(Javascript)
533532
php_phongo_javascript_ce->create_object = php_phongo_javascript_create_object;
534533
PHONGO_CE_FINAL(php_phongo_javascript_ce);
535534

536-
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_json_serializable_ce);
535+
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
537536
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_phongo_type_ce);
538537
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, zend_ce_serializable);
539538

src/BSON/MaxKey.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -166,7 +165,7 @@ PHP_MINIT_FUNCTION(MaxKey)
166165
php_phongo_maxkey_ce->create_object = php_phongo_maxkey_create_object;
167166
PHONGO_CE_FINAL(php_phongo_maxkey_ce);
168167

169-
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, php_json_serializable_ce);
168+
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
170169
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, php_phongo_type_ce);
171170
zend_class_implements(php_phongo_maxkey_ce TSRMLS_CC, 1, zend_ce_serializable);
172171

src/BSON/MinKey.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -167,7 +166,7 @@ PHP_MINIT_FUNCTION(MinKey)
167166
php_phongo_minkey_ce->create_object = php_phongo_minkey_create_object;
168167
PHONGO_CE_FINAL(php_phongo_minkey_ce);
169168

170-
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, php_json_serializable_ce);
169+
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
171170
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, php_phongo_type_ce);
172171
zend_class_implements(php_phongo_minkey_ce TSRMLS_CC, 1, zend_ce_serializable);
173172

src/BSON/ObjectID.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -418,7 +417,7 @@ PHP_MINIT_FUNCTION(ObjectID)
418417
php_phongo_objectid_ce->create_object = php_phongo_objectid_create_object;
419418
PHONGO_CE_FINAL(php_phongo_objectid_ce);
420419

421-
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, php_json_serializable_ce);
420+
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
422421
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, php_phongo_type_ce);
423422
zend_class_implements(php_phongo_objectid_ce TSRMLS_CC, 1, zend_ce_serializable);
424423

src/BSON/Regex.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -467,7 +466,7 @@ PHP_MINIT_FUNCTION(Regex)
467466

468467
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_type_ce);
469468
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, zend_ce_serializable);
470-
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_json_serializable_ce);
469+
zend_class_implements(php_phongo_regex_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
471470

472471
memcpy(&php_phongo_handler_regex, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
473472
php_phongo_handler_regex.compare_objects = php_phongo_regex_compare_objects;

src/BSON/Timestamp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
/* PHP Core stuff */
2626
#include <php.h>
2727
#include <php_ini.h>
28-
#include <ext/json/php_json.h>
2928
#include <ext/standard/info.h>
3029
#include <Zend/zend_interfaces.h>
3130
#include <ext/spl/spl_iterators.h>
@@ -498,7 +497,7 @@ PHP_MINIT_FUNCTION(Timestamp)
498497
php_phongo_timestamp_ce->create_object = php_phongo_timestamp_create_object;
499498
PHONGO_CE_FINAL(php_phongo_timestamp_ce);
500499

501-
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, php_json_serializable_ce);
500+
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, php_phongo_json_serializable_ce);
502501
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, php_phongo_type_ce);
503502
zend_class_implements(php_phongo_timestamp_ce TSRMLS_CC, 1, zend_ce_serializable);
504503

0 commit comments

Comments
 (0)