Compile PHP enums to PHP 7/8 lookalikes, PHP 8.1 native#106
Merged
Conversation
However, since XP core reflection does not support constant annotations, we cannot access case annotations. This will be covered by the reflection library!
Member
Author
thekid
added a commit
to thekid/cas
that referenced
this pull request
Mar 14, 2021
This package is deprecated, see xp-framework/compiler#106
Member
Author
...and with support for arbitrary expressions as initializers (see #104), we can simply refactor code as follows: @@ -3,8 +3,8 @@
use xml\{Tree, Node};
/** CAS service response - in XML and JSON */
-abstract enum ServiceResponse {
- XML {
+abstract class ServiceResponse {
+ public static $XML= new class() extends self {
public function success($user) {
$n= new Node('cas:authenticationSuccess')->withChild(new Node('cas:user', $user['username']));
if (isset($user['attributes'])) {
@@ -29,8 +29,8 @@ abstract enum ServiceResponse {
$response->send($tree->getSource(INDENT_DEFAULT), 'text/xml');
}
- },
- JSON {
+ };
+ public static $JSON= new class() extends self {
public function success($user) {
$success= ['user' => $user['username']];
if (isset($user['attributes'])) {(In this case, the class already had a static factory method and wasn't using |
Member
Author
PHP master build donwloaded from https://github.com/shivammathur/php-builder-windows/actions: # Verify enum support
$ XP_RT=master xp -w 'enum SortOrder { case ASC; case DESC; } return SortOrder::ASC'
SortOrder {
name => "ASC"
}
# Run XP core tests
core $ XP_RT=master xp test src/test/config/unittest/*ini
# ...
♥: 4205/4259 run (54 skipped), 4205 succeeded, 0 failed
Memory used: 22559.28 kB (43149.24 kB peak)
Time taken: 4.005 seconds
# Run XP compiler test
compiler $ XP_RT=master xp test src/test/php
# ...
♥: 563/564 run (1 skipped), 563 succeeded, 0 failed
Memory used: 9119.41 kB (9177.58 kB peak)
Time taken: 0.251 seconds |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Enums
Implement feature request #100. XP Compiler and XP Framework (10.8.0+) now support:
WeekDay::MON)enumsyntax and compiles it to XP enums, will be deprecated! Its abstract enum feature is unsupported by PHP 8.1 enums, and may be reintroduced later via the tagged unions RFC.Behind the scenes
What the user writes (and what will be emitted for PHP 8.1 once the enum PR is merged):
What is emitted for PHP < 8.1:
Backed enums also include a value member as well as from() and tryFrom() methods.