yohang / Finite

A Simple PHP Finite State Machine
http://yohan.giarel.li/Finite
MIT License
1.31k stars 188 forks source link

No ability to get states array #110

Closed phillipsnick closed 8 years ago

phillipsnick commented 8 years ago

I currently use this library with Symfony and define some properties for each state. But it's impossible to access these.

finite_finite:
    product:
        class: StoreBundle\Entity\Product
        states:
            draft:
                type: initial
                properties:
                    label: 'Draft'
                    colour: 'c6c6c6' # Grey
            live:
                properties:
                    label: 'Live'
                    colour: '428bca' # Blue
            successful:
                properties:
                    label: 'Successful'
                    colour: '45B6AF' # Green
            archived:
                type: final
                properties:
                    label: 'Archived'
                    colour: '000000' # Black
        transitions:
            ...

Controller code:

$sm = $this->get('finite.factory')->get($product);

Without access to this I'd have to define all my states twice, which would only make maintenance more difficult.

I previously added a bit of a hack in to get round this as I was in a hurry: https://github.com/phillipsnick/Finite/commit/477f9ba152f3c6e837e1b7c093ea12bbcd39f4ab

Is there any reason why this isn't accessible?

RonRademaker commented 8 years ago

Why do you need to set the states on the object? The statemachine is responsible for managing the states and transitions, it can also manage your properties.

yohang commented 8 years ago

By default, properties are only accessible for the current state, see : http://finite.readthedocs.org/en/latest/examples/basic_graph.html#current-state.

If you really want to retrieves all properties (and all states), you can retrieve a specific state with getState($name) https://github.com/yohang/Finite/blob/master/src/Finite/StateMachine/StateMachine.php#L237 and the whole state names with getStates() https://github.com/yohang/Finite/blob/master/src/Finite/StateMachine/StateMachine.php#L264.

phillipsnick commented 8 years ago

Thanks, makes sense.