PHP code example of tyhand / workflow-bundle

1. Go to this page and download the library: Download tyhand/workflow-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/ */

    

tyhand / workflow-bundle example snippets


// app/AppKernel.php
public function registerBundles()
{
    $bundles = [
        // ...
        new Tyhand\WorkflowBundle\TyHandWorkflowBundle()
    ];
}


use TyHand\WorkflowBundle\Workflow\Context\ContextInterface;

class MyEntity implements ContextInterface
{
    //...
}

use TyHand\WorkflowBundle\Workflow\AbstractWorkflowDefinition;
use TyHand\WorkflowBundle\Workflow\Builder\WorkflowflowBuilder;

class MyWorkflow extends AbstractWorkflowDefinition
{
    //...
}

// ...
class MyWorkflow extends AbstractWorkflowDefinition
{
    public function getName()
    {
        return 'my_workflow';
    }

    public function getContextClass()
    {
        return 'AppBundle\MyEntity';
    }

    // ...
}

\\...
class MyWorkflow extends AbstractWorkflowDefinition
{
    // ...

    public function build(WorkflowBuilder $builder)
    {
        return $builder
            ->activeLimit(1) // The number of times a single context can be active in the workflow
            ->totalLimit(1) // The maximum number of times a single context can go through a workflow
            ->initial('state_a') // The initial state
            // States follow here
            // ...
        ;
    }
}

public function build(WorkflowBuilder $builder)
{
    return $builder
        ->activeLimit(1) // The number of times a single context can be active in the workflow
        ->totalLimit(1) // The maximum number of times a single context can go through a workflow
        ->initial('state_a') // The initial state
        ->startState('state_a')
            ->addEvent('move_state', 'state_b') // EVENT EXIT (when move_state workflow event is thrown, move to state b)
        ->end()
        ->startState('state_b')
            ->startCondition() // CONDITIONAL EXIT
                ->conditionalFunction(function ($context) {
                    return 5 < $context->getBar();
                })
                ->ifTrue('state_c1') // GOTO state_c1 if bar < 5
                ->ifFalse('state_c2') // GOTO state_c2 if bar >= 5
            ->end()
        ->end()
        ->startState('state_c1')
            ->addAction(function ($context) { // ACTION
                $context->incrementFoo();
            })
            ->startCondition() // CONDITIONAL EXIT (with no ifFalse)
                ->conditionalFunction(function($context) {
                    return 10 === $context->getFoo();
                })
                ->ifTrue('state_d')
            ->end()
            ->startCondition() // CONDITIONS called in order
                ->conditionalFunction(function($context) {
                    return 2 < $context->getBar();
                })
                ->ifTrue('state_d')
            ->end()
            ->setTimeLimit(3600, 'state_d') // TIME LIMIT EXIT In 3600 second (1 hour) goto state d
        ->end()
        ->startState('state_c2')
            ->addAction(function ($context) { // ACTION
                $context->decrementFoo();
            })
            ->addEvent('move_state', 'state_d') // EVENT EXIT goto d if workflow event move state is thrown
        ->end()
        ->startState('state_d') // terminal state
        ->end()
    ;
}

// WORKFLOW BUILDER METHOD
// ...
->startState('complete')
    ->addAction(function ($context) use ($eventDispatcher, $entityManager) {
        $eventDispatcher->dispatcher(
            MyObjectEvent::PROCESS_COMPLETE,
            new MyObjectEvent($entityManager, $context)
        );
    })
->end()

    // Create a new instance at the initial state
    $instance = $this->get('tyhand_workflow.manager')->getWorkflow('my_workflow')->start($myEntity);

    // Persist and flush the new instance entity
    $this->getDoctrine()->getManager()->persist($instance);
    $this->getDoctrine()->getManager()->flush($instance);

    $this->get('event_dispatcher')->dispatch(
        \TyHand\WorkflowBundle\Event\WorkflowEvent::WORKFLOW_EVENT,
        new \TyHand\WorkflowBundle\Event\WorkflowEvent($myEntity, 'my_workflow', 'my_event')
    );