Skip to content

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Apr 12, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

This PR improves the DX on two aspects:

  • allowing to use FWCN::glob patterns to list places - works with enums and consts
  • allowing to use backed enums to list places and reference them in transitions

Example with consts:

framework:
    workflows:
        my_workflow_name:
            places: 'App\Workflow\MyWorkflow::PLACE_*'
            transitions:
                one:
                    from: !php/const App\Workflow\MyWorkflow::PLACE_A
                    to: !php/const App\Workflow\MyWorkflow::PLACE_B

and

<?php

namespace App\Workflow;

class MyWorkflow
{
    const PLACE_A = 'a';
    const PLACE_B = 'b';
    const PLACE_C = 'c';
// [...]
}

Example with enums:

framework:
    workflows:
        my_workflow_name:
            places: !php/enum App\Workflow\Places
            transitions:
                one:
                    from: !php/enum App\Workflow\Places::A
                    to: !php/enum App\Workflow\Places::B

and

<?php

namespace App\Workflow;

enum Places: string
{
    case A = 'a';
    case B = 'b';
    case C = 'c';
}

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.
I'd like one more step: when this is used, the marking store should pass the enum instance to the object instead of the string.


enum Places: string
{
case A = 'a';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In another PR, I want add to introduce an attribute to configure the Metadata. What would be the name of such attribute? #[WorkflowMetadata] / #[AsWorkflowMetadata]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just #[Place]?

@lyrixx
Copy link
Member Author

lyrixx commented Apr 12, 2025

like one more step: when this is used, the marking store should pass the enum instance to the object instead of the string.

Of course! I commented the @tucksaun PR!

@alexandre-daubois
Copy link
Member

alexandre-daubois commented Apr 12, 2025

Maybe it's a naive question: would it be hard to also support UnitEnums? Recent implementations to support enums in workflow seem way simpler than what I tried a few years ago, so maybe UnitEnum wouldn't be a big deal now?

@lyrixx
Copy link
Member Author

lyrixx commented Apr 13, 2025

I think it would be hard. And with unit enum, i fail to see what you save in your db

@lyrixx
Copy link
Member Author

lyrixx commented Apr 14, 2025

Thinking about it, I'll need to rework a bit the PR. We'll need to store the fact it's an enum in the configuration, to be able to configure the marking store. ATM, we convert the enum in the configuration class, and it's too early and we loose the information

@tucksaun
Copy link
Contributor

@lyrixx nice work! This is similar to something I quickly tried and it seemed to work well.

We'll need to store the fact it's an enum in the configuration, to be able to configure the marking store.

Do we? I mean, we have to if we want to enforce integrity end-to-end. So does that mean we want to go with an enum-dedicated marking store that we configure automatically when one uses an enum like proposed here?

@nicolas-grekas
Copy link
Member

I think so yes: when places is configured to an enum class, we should only support storing enums.

@nicolas-grekas
Copy link
Member

We'll need to store the fact it's an enum in the configuration

@lyrixx don't miss this part ;)

@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
@kira0269
Copy link

kira0269 commented Aug 13, 2025

Hello everyone !

I have been following this "Enum support" topic for a while since I often use the Workflow component.

One argument someone gave me against enum support is that using an enum as closed list of places does not allow to extend it.

I really like this PR for what it brings: improve DX without reducing possibilities.

I'd like to point out a possible issue: what happen for workflows (not state machines) with multiple states at a time ?
Imagine the following situation:

enum PlacesA: string
{
    case PlaceOne = 'place_one';
    case PlaceTwo = 'place_two';
}

enum PlacesB: string
{
    case PlaceStart = 'place_one';
    case PlaceEnd = 'place_two';
}

$marking = new Marking();
$marking->mark(PlacesA::PlaceOne->value);  // $marking->getPlaces() = ['place_one' => 1]
$marking->mark(PlacesB::PlaceStart->value); // $marking->getPlaces() = ['place_one' => 2]

It's not possible since the subject must return a valid marking representation (like ['string' => 1, '...']).
Maybe implementing a new EnumMarkingStore and a EnumMarking could be the solution instead of trying to mix everything ?

WDYT ?

@nicolas-grekas nicolas-grekas changed the title [FrameworkBundle] Add support for configuring workflow places with a BackedEnum [FrameworkBundle] Add support for configuring workflow places with glob patterns matching consts/backed enums Sep 15, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR (and its description, have a look) to:

  • allow using FWCN::glob patterns to list places - works with enums and consts
  • allow using backed enums to list places and reference them in transitions

@nicolas-grekas
Copy link
Member

Thank you @lyrixx.

@nicolas-grekas nicolas-grekas merged commit 4d58fa7 into symfony:7.4 Sep 15, 2025
9 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants