sebdesign / laravel-state-machine

Winzou State Machine service provider for Laravel
MIT License
327 stars 57 forks source link

Composer 2 #50

Closed devngl closed 3 years ago

devngl commented 3 years ago

is there any known problem with Composer 2 and this library? After updating to version 2 this happened:

image

Looks like it's trying to use Winzou class instead of Sebdesign one. Went back to Composer 1 in the meantime.

Regards

sebdesign commented 3 years ago

As far as I know, it's working fine in composer 2. I'm using it in my machine as well as in travis.ci: https://travis-ci.org/github/sebdesign/laravel-state-machine/jobs/742520351#L225

What versions are you using? (PHP / laravel-state-machine)?

sebdesign commented 3 years ago

Can you show me the code that throws the error?

devngl commented 3 years ago

I'm using PHP 7.4.12 and Laravel 8.

This is the whole graph definition:

   'graphEvent' => [
        'graph' => 'graphEvent',

        'property_path' => 'state_id',

        'states' => [
            EventState::E_PROCESSING,
            EventState::E_REVIEW,
            EventState::E_PUBLISHED,
            EventState::E_PAUSED,
            EventState::E_CANCELLED,
        ],

        'transitions' => [
            // Ticketing team request room review
            'request_review' => [
                'from' => [EventState::E_PROCESSING],
                'to'   => EventState::E_REVIEW,
            ],

            // Room manager can cancel the event
            'cancel_review' => [
                'from' => [EventState::E_REVIEW],
                'to'   => EventState::E_PROCESSING,
            ],

            // Room manager can publish events
            'publish_with_reviews' => [
                'from' => [EventState::E_REVIEW, EventState::E_PAUSED, EventState::E_PROCESSING],
                'to'   => EventState::E_PUBLISHED,
            ],

            // Once this state is triggered the event won't be shown on sale point
            'pause' => [
                'from' => [EventState::E_PUBLISHED],
                'to'   => EventState::E_PAUSED,
            ],

            // No way back state. If an event gets cancelled it cannot be enabled again
            'cancel' => [
                'from' => [EventState::E_PUBLISHED, EventState::E_PAUSED],
                'to'   => EventState::E_CANCELLED,
            ],
        ],

        'callbacks' => [
            'guard'  => [
                'on_requesting_review' => [
                    'on'   => 'request_review',
                    'do'   => [EventTransitions::class, 'canBeReviewed'],
                    'args' => ['object'],
                ],

                'on_publishing' => [
                    'on'   => ['publish_with_reviews'],
                    'do'   => [EventTransitions::class, 'canBePublished'],
                    'args' => ['object'],
                ],

                'on_entering_activity_code' => [
                    'on'   => 'enter_activity_code',
                    'do'   => [EventTransitions::class, 'validateActivityCode'],
                    'args' => ['object'],
                ],

                'on_request_confirmation' => [
                    'on'   => 'request_confirmation',
                    'do'   => [EventTransitions::class, 'canRequestConfirmation'],
                    'args' => ['object'],
                ],
            ],

            // called before applying a transition
            'before' => [],

            // called after applying a transition
            'after'  => [
                'on_entering_review' => [
                    'to'   => EventState::E_REVIEW,
                    'do'   => [NotifyEventReview::class, 'handle'],
                    'args' => ['object', 'event']
                ],
            ],
        ],
    ]

EventTransitions.php is just a class full of static methods on this manner:

    public static function canBeReviewed(Event $event): bool
    {
        return $event->userCanRequestReview();
    }

In regard to what triggers the problem is just an API endpoint that does this:

$event = App\Models\Event::findOrFail($eventId);

try {
    $event->stateMachine()->apply('request-review');
    return new JsonResponse(null, 200);
} catch (Exception $e) {
    handleException($e);
}

stateMachine() is a method of the model:

public function stateMachine()
    {
        return StateMachine::get($this, 'graphEvent');
    }

My composer.lock has these versions:

"name": "sebdesign/laravel-state-machine",
"version": "v3.1.1",
[...]
"name": "winzou/state-machine",
"version": "0.4.0",

Once I perform the installation with composer 1 the problem is solved, so I guess composer 2 is doing something weird with DI.

sebdesign commented 3 years ago

Are you importing Sebdesign\SM\Facade as StateMachine, StateMachine, or SM\StateMachine\StateMachine in your model?

devngl commented 3 years ago
use StateMachine;

I've tried 3 ways and it keeps failing, only difference is that when I use SM\StateMachine\StateMachine the error is this one: Call to undefined method SM\StateMachine\StateMachine::get()

Which I guess is normal since I'm not calling the constructor myself.

sebdesign commented 3 years ago

That's strange, StateMachine is the alias for the Sebdesign\SM\Facade facade. It shoudn't be SM\StateMachine\StateMachine.

sebdesign commented 3 years ago

Hello, did you manage to find a solution?

sebdesign commented 3 years ago

Closing due to inactivity.