Commit Graph

638 Commits

Author SHA1 Message Date
Andreas Braun
a237193eca BSON class implementations (#1387)
* Extract bson to json conversion logic

* PHPC-326: Add class to represent raw BSON document

* PHPC-326: Add class to represent raw BSON array

* Typemaps now support raw BSON type

* Short-circuit encoding of BSON zvals

* PHPC-324: Add BSONIterator class

* Polyfill zend_array_is_list on PHP < 8.1

* Add offset accessors to BSON classes

* Add more tests around iterators and fetching offsets

* Remove compare handler for BSONIterator

* Rename BSONArray to ArrayList

* Rename BSONDocument to Document

* Rename BSONIterator to Iterator

* Implement Document::fromBSONString

* Test BSON document in bson-corpus tests

* Fix ArrayList handling of BSON arrays with wrong keys

* Address code review for bson-corpus tests

* Address code review in ArrayList

* Address code review for Document

* Address code review for Iterator

* Allow BSON iterators to be rewound

* Address code review in tests

* Fix comment explaining php_phongo_bson_append_object

* Remove obsolete php_phongo_bson_append_zval

* More review changes to tests

* Return base64 encoded data when debugging BSON classes

* Allow serialisation of Document and ArrayList

* Add test for iterating past the end of a structure

* Update exception message to contain class name

* Make BSON pclass handling test more complete

* Fix wrong handling of type map in nested structures

* Ensure objects are backed by actual memory

* Validate UTF-8 BSON data before returning it

* Allow ArrayList::toPHP() to accept a different root type map

* Ignore null return type for invalid BSON iterators on PHP < 8

* Throw when calling get on non-existing BSON offsets

* Remove unnecessary length from serialised output

* Refactor names around type map handling

* Use short array syntax in new tests

* Improve test descriptions

* Restructure BSON iterator tests

* Expand BSON class serialisation tests

* Throw exception when iterator is exhausted

The previous method relied on the type system of PHP to throw an exception when accessing the key of an exhausted iterator, but this does not work on non-debug versions of PHP.

* Update test description

* Rename ArrayList class to PackedArray
2022-12-16 08:48:09 +01:00
Jeremy Mikola
594cad9a30 PHPC-2083: Revise BSON handling of enum classes
Revert previous enum instantiation behavior and PersistableEnum trait from de5f1e5a93

Backed enums will be encoded as their case value. Non-backed cannot be encoded and no enums can be encoded at the root level.

Prohibit enums from implementing Persistable and Unserializable.
2022-11-10 20:21:36 +08:00
Jeremy Mikola
f0421fa1fe PHPC-2103: Report loaded crypt_shared version in phpinfo() (#1351)
* Match error message variations between libmongoc 1.23+ and older

This typo was corrected in mongodb/mongo-c-driver@1d7229f0e9 and will appear in the 1.23.0 release.

* PHPC-2103: Report loaded crypt_shared version in phpinfo()

* PHPC-2132: Fix pattern for matching phpinfo() values
2022-09-14 17:28:13 -04:00
Andreas Braun
2b03e66bbb PHPC-2128: Create stub file for functions (#1349)
* PHPC-2128: Create stub file for functions

* Fix wrong phpdoc comment formatting
2022-08-26 11:37:40 +02:00
Jeremy Mikola
de5f1e5a93 PHPC-2083: Allow enums to be instantiated during BSON decoding (#1317)
Enums serialize like PHP objects with a "name" property (BackedEnum instance will also have a "value") and thus become BSON documents. In order for a document to unserialize back to an enum, it must implement either Unserializable or Persistable. The bsonUnserialize() method serves no purpose for initialization, but it will still be invoked.

This introduces a PersistableEnum trait, which can be used for both Unserializable or Persistable implementations.

Co-authored-by: Sara Golemon <pollita@php.net>
2022-08-25 23:09:45 -04:00
Andreas Braun
6887226d34 PHPC-1709: Add typing information to arginfo (#1337)
* Add missing macros for declaring arguments

* Add macros to define disabled constructor and __wakeup

* Define arginfo via stubs for BSON classes

* Declare tentative return types in interfaces

* Add correct return type for __set_state methods

* Generate class entries for BSON classes

* Declare Binary class constants in stub file

* Use stubs in exception classes

* Add stubs for monitoring classes

* Fix wrong JsonSerializable class in BSON stubs

* Disable declaration-after-statement

This is necessary to let the generated arginfo files compile

* Use stubs for driver classes

* Fix deprecation messages in tests

* Add missing macro

* Fix tests relying on value injection

* Parse parameters in disabled constructor/wakup function

* Add note about arginfo files to contribution docs

* Add GitHub action to check generated arginfo files

* PHPC-2115: Use DateTimeInterface in UTCDateTime constructor signature

* Don't install mongodb extension in GitHub actions workflows

* Change indentation in stub files

* Use individual #if conditions for each stub method

* Make disabled constructor/wakeup methods static

* Fix bulkWrite parameter name

* Use %d to match property count in tests

* Ensure all classes with disabled serialisation declare __wakeup
2022-08-03 08:42:07 +02:00
Andreas Braun
e2f91446bc PHPC-2111: Replace zend_parse_parameters calls with macros (#1338)
* Replace zend_parse_parameters calls with macros

* Fix warning about uninitialized variable

* Fix wrong parameter type in Server::executeBulkWrite

* Add compat macro for Z_PARAM_ARRAY_OR_OBJECT
2022-07-26 12:06:55 +02:00
Jeremy Mikola
0960a204b3 PHPC-2037: Remove vim modeline comments 2022-01-26 20:31:48 -05:00
Jeremy Mikola
fc358d6f18 PHPC-2040: Break down php_phongo.c into smaller files
This also cleans up header includes throughout the project. The order follows: libbson, libmongoc, libmongocrypt, PHP, contrib (e.g. PHP array API), internal modules (e.g. php_phongo.h, phongo_bson.h), and class headers. Quoted strings are used for all but PHP includes, which are always sourced from system directories.

php_phongo.h is now responsible for including config.h and assigning the default logging domain.
2022-01-26 20:31:48 -05:00
Jeremy Mikola
c2410ab0e7 PHPC-2023 and PHPC-2030: Allow observation of events dispatched during client destruction (#1292)
* PHPC-2023: Allow observation of TopologyClosedEvent

Observation is only possible for non-persistent clients freed before RSHUTDOWN. This change also allows for reliable testing of ServerClosedEvent.

* PHPC-2030: Test observation of commands issued during client destruction

Command monitoring events can only be observed for non-persistent clients freed before RSHUTDOWN.
2022-01-14 10:08:12 -05:00
Jeremy Mikola
10b3a1fe70 PHPC-1150: SDAM Monitoring (#1289)
* PHPC-1805: Implement ServerDescription class

* PHPC-1910: Implement Server::getServerDescription()

* PHPC-1934: Check for uninitialized intern in ServerDescription get_properties_hash

* PHPC-1925: Change ServerDescription::getType() to return a string

* PHPC-1891: Implement TopologyDescription class

* PHPC-1892: Implement SDAMSubscriber interface

* PHPC-1893: Implement SDAM event classes

* Revise server and topology event classes and tests

Use phongo_objectid_init in getTopologyId() methods. Report topologyId as an ObjectId in debug info. Report TopologyDescription servers as ServerDescription in debug info.

Copy parameters in phongo_serverdescription_init and phongo_topologydescription_init for consistency with other functions (e.g. phongo_readconcern_init). This also creates phongo_serverdescription_init_ex for when we want to skip copying in Server::getServerDescription().

* PHPC-1953: Add tests for debug handlers

* PHPC-1959: Utilize Manager::addSubscriber() in tests

Skip TopologyClosedEvent test (pending PHPC-2023)

* PHPC-1958: Assert same topologyId for SDAM events

* PHPC-2025: Support load balancer in ServerDescription and TopologyDescription

* PHPC-2026: Handle 64-bit lastUpdateTime for 32-bit platforms

Co-authored-by: Tanli Su <46271307+tanlisu@users.noreply.github.com>
Co-authored-by: Andreas Braun <git@alcaeus.org>
Co-authored-by: Tanli Su <tanli.su@gmail.com>
2022-01-10 13:14:10 -05:00
Jeremy Mikola
ab44b0cbeb PHPC-1647: Use mongoc_client_new_from_uri_with_error
This allows PHPC to defer entirely to libmongoc for cross-option URI validation

Bump libmongoc to 1.21-dev

Build changes are ported from upstream CMake changes (see: CDRIVER-4249)
2022-01-05 13:10:45 -05:00
Jeremy Mikola
da0f73a677 PHPC-1912: CSFLE 1.0 KMIP Support (#1280)
* PHPC-2009: Support tlsOptions encryption option

This also adds some missing option validation tests for the autoEncryption driver option and Manager::createClientEncryption().

* PHPC-2010: Type validation for autoEncryption.extraOptions
2021-12-13 20:10:07 -05:00
Jeremy Mikola
a8a2210488 PHPC-1997: Migrate to new libbson atomic API (#1272)
* Bump libmongoc to 1.20-dev

* PHPC-1997: Migrate to new libbson atomic API
2021-11-05 14:17:17 -04:00
Jeremy Mikola
f9fe7efe03 PHPC-1985: Report unset round_trip_time as null in Server debug output (#1265) 2021-10-15 15:32:45 -04:00
Jeremy Mikola
594e83bad4 PHPC-1976: Add serviceId to command monitoring events 2021-10-15 13:44:32 -04:00
Jeremy Mikola
2fb1836358 PHPC-1979: Use mongoc_client_get_handshake_description for load balancers
This ensures that Server::getInfo() returns a usable hello response (e.g. for wire version checks). The debug info is also addressed for consistency.
2021-10-15 13:44:31 -04:00
Jeremy Mikola
797a5c81f7 PHPC-1980: Server::TYPE_LOAD_BALANCER constant 2021-10-15 13:44:31 -04:00
Jeremy Mikola
cfc6bbbe5d PHPC-1978: Expose mongoc_global_mock_service_id as INI option
This also consolidates INI functions (and trace logging) into a separate file and ensure that mongoc logging is disabled during GSHUTDOWN (avoiding a potential segfault).

Enable service ID mocking when testing load balancers with HAProxy.
2021-10-15 13:44:30 -04:00
Jeremy Mikola
6eaa91b87b PHPC-1982, PHPC-1983: Fix some Windows build warnings (#1263)
* PHPC-1982: Use bson_gettimeofday for Windows compatibility

* PHPC-1983: Fix arg type for ADD_ASSOC_INT64_AS_STRING
2021-10-05 10:49:47 -04:00
Andreas Braun
a612c370f6 PHPC-1756: Remove oppressive terminology from source code (#1221)
* Use latest version of libmongoc

* Remove deprecated terminology for primary

* Remove deprecated terminology for secondary

* Fix clang-format

* Remove check for legacy field

* Fix missing space in error expectation

* Harden test expectation around primary checks
2021-06-22 19:30:56 +02:00
Tanli Su
0da40423f3 PHPC-1721: Fix argument order for some ecalloc function calls (#1226)
Co-authored-by: Andreas Braun <alcaeus@users.noreply.github.com>
2021-06-22 10:13:09 -04:00
Alexander Golin
68019a258a PHPC-1792: Change all copyrights to be <year>-present (#1217) 2021-05-03 09:04:08 -04:00
Jeremy Mikola
324182a8e6 PHPC-1804: Manager::addSubscriber and removeSubscriber (#1213)
* Use zend_exception_ce instead of zend_exception_get_default()

zend_exception_get_default() has been deprecated since PHP 7.0. See: php/php-src@f9e9d3a437

* Check retval from php_phongo_set_monitoring_callbacks

This ensures that php_phongo_client_register is never called in the event this method fails (however unlikely).

* Index subscriber HashTable by numeric object handles

This also updates add/removeSubscriber to use the new argument parsing macros from deead9687e.

* PHPC-1804: Manager::addSubscriber and removeSubscriber

Implements per-client event subscribers. Moves existing APM code to a new phongo_apm.c module and adds logic to coordinate dispatching between global and per-client subscribers.
2021-04-09 08:55:44 -04:00
Andreas Braun
deead9687e PHPC-1716 Allow configuring server API version in manager (#1204)
* Introduce MongoDB\Driver\ServerApi

* Accept serverApi driver option

* Introduce create_test_manager factory to create manager

A centralised entry point is required to inject the API_VERSION env variable later.

* Add build variant to test with requireApiVersion=true

* Fix wrong configuration for auth variable

This changed when migrating from our own scripts to drivers-evergreen-tools and was not updated properly, causing all tests to run with auth disabled.

* Declare ZEND_PARSE_PARAMETERS_NONE macro

This macro is missing on PHP < 7.3

* Remove duplicated API param storage

* Add missing semicolons

* Add ZEND_PARSE_PARAMETERS_NON_EX macro

* Extract error handling functionality to separate macros

* Throw if internal mongoc_server_api_t is already initialised

* Use imported namespaces in tools file

* Fix type info for reflection

* Use American English spelling

* Only use typed serialize signature on PHP 8+

* Update PHONGO_PARSE_PARAMETERS_NONE macro for PHP < 7.3

* Remove usage of ZEND_STRL within zend_hash_str_add

This causes compile failures on PHP < 7.3 that I have yet to understand.

* Fix errors in new PHONGO_PARSE_PARAMETERS macros
2021-03-26 15:43:48 +01:00
Andreas Braun
37408e1ccf Merge branch 'v1.9'
* v1.9:
  PHPC-1739 Move from travis-ci to GitHub Actions (#1199)
  PHPC-1774 Fix truncation of PHP_VERSION constant in handshake metadata (#1202)
  PHPC-1775 Use correct size for strncpy in when initialising DBPointer (#1203)
2021-02-23 14:52:15 +01:00
Andreas Braun
ef28066d84 PHPC-1774 Fix truncation of PHP_VERSION constant in handshake metadata (#1202)
* PHPC-1774 Fix truncation of PHP_VERSION constant in handshake metadata

* Work around libmongoc issue that concatenates platform without whitespace
2021-02-23 13:50:26 +01:00
Jeremy Mikola
63a479103e PHPC-1720: Move mongoc_client_set_error_api after check for null client pointer (#1197) 2021-02-08 08:58:29 -05:00
Andreas Braun
032af9a8ef PHPC-1529: Reset libmongocrypt key vault client after forking (#1189)
* PHPC-1529: Reset libmongocrypt key vault client after forking

* Run ping command instead of find

* Update wrong comment

* Only run tests when libmongocrypt is present

* Add mongocryptd.pid to gitignore

* Fix test failures due to missing mongocryptd
2021-01-20 19:30:39 +01:00
Jeremy Mikola
69efd5cee4 PHPC-1737: Use zend_hash_graceful_reverse_destroy for persistent client HashTable (#1191)
Also improve comments for request-scoped HashTables
2021-01-20 07:42:11 -05:00
Jeremy Mikola
5959c50fcb PHPC-1367: Move libmongoc init/cleanup within scope of custom libbson vtable 2021-01-20 00:54:57 -05:00
Jeremy Mikola
19cf10110c PHPC-1366: Declare libbson vtable in MINIT instead of using a global 2021-01-20 00:54:57 -05:00
Jeremy Mikola
6e7eb34dab PHPC-1722: Destroy persistent clients in GSHUTDOWN
This ensures persistent clients in all threads are freed, not just those in the main thread. Doing so also required that we call mongoc_cleanup and bson_mem_restore_vtable in the final GSHUTDOWN (tracked by an atomic counter).
2021-01-20 00:54:57 -05:00
Jeremy Mikola
5e2f7b5461 PHPC-1645: Allow disabling of libmongoc client persistence (#1187)
* Replace client pointers on object structs with Manager references

* Request-scoped Manager registry for APM events

* Ensure Manager is registered regardless of persistent client

* Reset request-scoped clients when forking

* Test for freeing Manager during RSHUTDOWN via subscriber reference

Co-authored-by: Andreas Braun <git@alcaeus.org>
2021-01-12 08:27:32 -05:00
Andreas Braun
a0be179a70 PHPC-1691: Provide Iterator implementation for cursors 2020-10-29 19:38:14 +01:00
Andreas Braun
60febd0f28 PHPC-1699: Ensure all argument parsing errors throw InvalidArgumentException (#1173)
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
2020-10-28 08:20:32 +01:00
Andreas Braun
d785d5e15e PHPC-1689: Allow driver to compile with PHP 8
* PHPC-1689: Remove remainder of TSRMLS_* macros
* PHPC-1689: Add compatibility macro for PHP 8 object change
* PHPC-1689: Replace PHP_CHECK_GCC_ARG with AX_CHECK_COMPILE_FLAG
* PHPC-1689: phongo_compat_object_handler_type and macros
* PHPC-1689: clone_object handlers
* PHPC-1689: compare_object handlers
* PHPC-1689: replace zend_hash_init_ex with zend_hash_init
* PHPC-1689: get_debug_info and get_properties handlers
2020-10-13 15:24:02 +02:00
Jeremy Mikola
ec3471a9bc Merge branch 'v1.8' 2020-09-16 12:01:13 -04:00
Jeremy Mikola
1d70e1a3f5 PHPC-1683: Collect WCE error labels in libmongoc bulk write replies
Also adds additional type and error checking to phongo_exception_append_error_labels.
2020-09-16 11:59:54 -04:00
Andreas Braun
a42ece59cc PHPC-1167: Avoid dangling session pointer in bulk writes (#1156) 2020-07-31 14:13:51 +02:00
Jeremy Mikola
09a8b87b31 clang-format changes 2020-07-08 13:57:10 -04:00
Jeremy Mikola
36d3e70982 PHPC-1648: Validate directConnection in URI options array 2020-07-08 13:55:30 -04:00
Jeremy Mikola
354bcc9441 PHPC-1598: Fix get_gc handlers for classes with get_properties
The ReadConcern, ReadPreference, WriteConcern, and BSON classes have no properties beyond pointers to libmongoc/libbson structs; however, all of these classes use get_properties to report fields for var_export (PHPC-850, PHPC-460).

The standard get_gc handler defers to get_properties to collect other zvals for GC inspection. This is problematic for the aforementioned get_properties handlers, since a HashTable of zvals intended for debugging will be returned and those properties will already be freed by our free_object handler (via FREE_HASHTABLE).

Having each class define its own get_gc handler is the first step to fixing this issue. The BSON classes already defined their own get_gc handlers, but erroneously returned the internally cached properties directly.

The second step to fixing this issue is ensuring that get_gc delegates to zend_std_get_properties, as is done in various PHP core extensions. Doing so ensures that we do not leak other zvals that may be assigned as public properties by the application (covered by the second regression test), since zend_std_get_properties will return those for GC inspection.
2020-05-19 18:14:53 -04:00
Andreas Braun
607b91cefd PHPC-1533: Handle OCSP URI options 2020-05-19 20:21:50 +02:00
Jeremy Mikola
d97a66a545 PHPC-1313: Support Zstandard compression 2020-05-13 10:56:14 -04:00
Jeremy Mikola
b9627c91dd PHPC-1614: Consult SIZEOF_ZEND_LONG instead of SIZEOF_LONG 2020-05-11 17:43:08 -04:00
Andreas Braun
0fa7df90f4 Fix wrong exception when using external auth source 2020-04-29 12:55:53 +02:00
Andreas Braun
cc5d7697b7 PHPC-1591: Add tests for allowed cases when authSource is given but username is not 2020-04-22 12:39:34 +02:00
Andreas Braun
0f7fb930d8 PHPC-1494: Fix wrong platform version in handshake 2020-03-25 13:20:41 +01:00
Andreas Braun
895fcf4bbc PHPC-1494: Add driver option to pass additional handshake data 2020-03-20 14:26:38 +01:00