PHP code example of letots / workflow-extension-bundle

1. Go to this page and download the library: Download letots/workflow-extension-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

letots / workflow-extension-bundle example snippets


#[AsWorkflow(name: 'order', type: AsWorkflow::TYPE_STATE_MACHINE)]
class OrderWorkflow extends AbstractWorkflow
{
    public const PLACE_NEW = 'new';

    #[Place(initial: true)]
    public const PLACE_DRAFT = 'draft';

    #[Transition(from: self::PLACE_DRAFT, to: self::PLACE_NEW)]
    public const TRANSITION_PUBLISH = 'publish';
}

public function __construct(
    private WorkflowInterface $order,
) {}

#[Transition(from: self::PLACE_DRAFT, to: self::PLACE_NEW)]
public const TRANSITION_PUBLISH = 'publish';

#[Transition(from: self::PLACE_REVIEW, to: self::PLACE_NEW)]
public const TRANSITION_PUBLISH_FROM_REVIEW = 'publish';

#[AsWorkflow(name: 'order', metadata: ['title' => 'Order workflow'])]
class OrderWorkflow extends AbstractWorkflow
{
    #[Place(initial: true, metadata: ['title' => 'Draft'])]
    public const PLACE_DRAFT = 'draft';

    #[Transition(from: self::PLACE_DRAFT, to: self::PLACE_NEW, metadata: ['title' => 'Publish'])]
    public const TRANSITION_PUBLISH = 'publish';
}

#[AsWorkflow(name: 'order', auditTrail: true)]
class OrderWorkflow extends AbstractWorkflow
{
}

#[AsEventListener(event: 'workflow.order.guard')]
public function onGuard(GuardEvent $event): void
{
    if ($event->getTransitionName() === 'publish' && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
        $event->setBlocked(true, 'Not allowed.');
    }
}

#[AsWorkflow(name: 'order', supportStrategy: Order::class)]
class OrderWorkflow extends AbstractWorkflow
{
}