-
Notifications
You must be signed in to change notification settings - Fork 208
PHPC-2083: Allow enums to be instantiated during BSON decoding #1317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a902f10
PHPC-2083: Allow enums to be instantiated during BSON decoding
jmikola 4ced542
Update src/phongo_bson.c
jmikola e595392
PersistableEnum trait
jmikola 7152d5e
Copy enum cases with ZVAL_COPY to increment refcount
jmikola d6c019e
Fix code formatting
jmikola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2014-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <php.h> | ||
|
||
#include "php_phongo.h" | ||
#include "phongo_error.h" | ||
#include "PersistableEnum_arginfo.h" | ||
|
||
zend_class_entry* php_phongo_persistableenum_ce; | ||
|
||
PHP_METHOD(MongoDB_BSON_PersistableEnum, bsonSerialize) | ||
{ | ||
PHONGO_PARSE_PARAMETERS_NONE(); | ||
|
||
RETVAL_ZVAL(getThis(), 1, 0); | ||
convert_to_array(return_value); | ||
|
||
return; | ||
} | ||
|
||
PHP_METHOD(MongoDB_BSON_PersistableEnum, bsonUnserialize) | ||
{ | ||
zval* data; | ||
|
||
PHONGO_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_ARRAY(data) | ||
PHONGO_PARSE_PARAMETERS_END(); | ||
|
||
return; | ||
} /* }}} */ | ||
|
||
void php_phongo_persistableenum_init_ce(INIT_FUNC_ARGS) /* {{{ */ | ||
{ | ||
php_phongo_persistableenum_ce = register_class_MongoDB_BSON_PersistableEnum(); | ||
} /* }}} */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** | ||
* @generate-class-entries static | ||
* @generate-function-entries | ||
*/ | ||
|
||
namespace MongoDB\BSON; | ||
|
||
trait PersistableEnum | ||
{ | ||
public final function bsonSerialize(): array {} | ||
|
||
public final function bsonUnserialize(array $data): void {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* This is a generated file, edit the .stub.php file instead. | ||
* Stub hash: db87873800da8dfaf4b3235eed97254593c9b83d */ | ||
|
||
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_MongoDB_BSON_PersistableEnum_bsonSerialize, 0, 0, IS_ARRAY, 0) | ||
ZEND_END_ARG_INFO() | ||
|
||
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_MongoDB_BSON_PersistableEnum_bsonUnserialize, 0, 1, IS_VOID, 0) | ||
ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0) | ||
ZEND_END_ARG_INFO() | ||
|
||
|
||
ZEND_METHOD(MongoDB_BSON_PersistableEnum, bsonSerialize); | ||
ZEND_METHOD(MongoDB_BSON_PersistableEnum, bsonUnserialize); | ||
|
||
|
||
static const zend_function_entry class_MongoDB_BSON_PersistableEnum_methods[] = { | ||
ZEND_ME(MongoDB_BSON_PersistableEnum, bsonSerialize, arginfo_class_MongoDB_BSON_PersistableEnum_bsonSerialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
ZEND_ME(MongoDB_BSON_PersistableEnum, bsonUnserialize, arginfo_class_MongoDB_BSON_PersistableEnum_bsonUnserialize, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) | ||
ZEND_FE_END | ||
}; | ||
|
||
static zend_class_entry *register_class_MongoDB_BSON_PersistableEnum(void) | ||
{ | ||
zend_class_entry ce, *class_entry; | ||
|
||
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\BSON", "PersistableEnum", class_MongoDB_BSON_PersistableEnum_methods); | ||
class_entry = zend_register_internal_class_ex(&ce, NULL); | ||
class_entry->ce_flags |= ZEND_ACC_TRAIT; | ||
|
||
return class_entry; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--TEST-- | ||
Enums serialize as documents and are not unserialized by default | ||
--SKIPIF-- | ||
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> | ||
<?php skip_if_php_version('<', '8.1.0'); ?> | ||
--FILE-- | ||
<?php | ||
|
||
require_once __DIR__ . '/../utils/basic.inc'; | ||
|
||
enum MyEnum | ||
{ | ||
case foo; | ||
} | ||
|
||
enum MyBackedEnum: string | ||
{ | ||
case foo = 'bar'; | ||
} | ||
|
||
$tests = [ | ||
MyEnum::foo, | ||
MyBackedEnum::foo, | ||
['myEnum' => MyEnum::foo], | ||
['myBackedEnum' => MyBackedEnum::foo], | ||
]; | ||
|
||
foreach ($tests as $document) { | ||
$bson = fromPHP($document); | ||
echo "Test ", toJSON($bson), "\n"; | ||
hex_dump($bson); | ||
var_dump(toPHP($bson)); | ||
echo "\n"; | ||
} | ||
|
||
?> | ||
===DONE=== | ||
<?php exit(0); ?> | ||
--EXPECTF-- | ||
Test { "name" : "foo" } | ||
0 : 13 00 00 00 02 6e 61 6d 65 00 04 00 00 00 66 6f [.....name.....fo] | ||
10 : 6f 00 00 [o..] | ||
object(stdClass)#%d (%d) { | ||
["name"]=> | ||
string(3) "foo" | ||
} | ||
|
||
Test { "name" : "foo", "value" : "bar" } | ||
0 : 22 00 00 00 02 6e 61 6d 65 00 04 00 00 00 66 6f ["....name.....fo] | ||
10 : 6f 00 02 76 61 6c 75 65 00 04 00 00 00 62 61 72 [o..value.....bar] | ||
20 : 00 00 [..] | ||
object(stdClass)#%d (%d) { | ||
["name"]=> | ||
string(3) "foo" | ||
["value"]=> | ||
string(3) "bar" | ||
} | ||
|
||
Test { "myEnum" : { "name" : "foo" } } | ||
0 : 20 00 00 00 03 6d 79 45 6e 75 6d 00 13 00 00 00 [ ....myEnum.....] | ||
10 : 02 6e 61 6d 65 00 04 00 00 00 66 6f 6f 00 00 00 [.name.....foo...] | ||
object(stdClass)#%d (%d) { | ||
["myEnum"]=> | ||
object(stdClass)#%d (%d) { | ||
["name"]=> | ||
string(3) "foo" | ||
} | ||
} | ||
|
||
Test { "myBackedEnum" : { "name" : "foo", "value" : "bar" } } | ||
0 : 35 00 00 00 03 6d 79 42 61 63 6b 65 64 45 6e 75 [5....myBackedEnu] | ||
10 : 6d 00 22 00 00 00 02 6e 61 6d 65 00 04 00 00 00 [m."....name.....] | ||
20 : 66 6f 6f 00 02 76 61 6c 75 65 00 04 00 00 00 62 [foo..value.....b] | ||
30 : 61 72 00 00 00 [ar...] | ||
object(stdClass)#%d (%d) { | ||
["myBackedEnum"]=> | ||
object(stdClass)#%d (%d) { | ||
["name"]=> | ||
string(3) "foo" | ||
["value"]=> | ||
string(3) "bar" | ||
} | ||
} | ||
|
||
===DONE=== |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing these changes makes me appreciate the work people put into gen_stub.php for PHP core - it's just so much easier to define classes now than it was before when everything had to be done manually...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.