美文网首页
The Workflow Component

The Workflow Component

作者: vanhukset | 来源:发表于2017-08-03 19:59 被阅读0次

    资料全部来自于https://symfony.com/doc/master/components/workflow.html

    流程图片


    image.png

    place:流程中的状态
    transition:状态转变的行为,动作名称 用来连接2个流程状态

    A set of places and transitions creates a definition. A workflow needs a Definition
    and a way to write the states to the objects (i.e. an instance of a MarkingStoreInterface
    ).

    use Symfony\Component\Workflow\DefinitionBuilder;
    use Symfony\Component\Workflow\Transition;
    use Symfony\Component\Workflow\Workflow;
    use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore;
    
    $definitionBuilder = new DefinitionBuilder();
    $definition = $definitionBuilder->addPlaces(['draft', 'review', 'rejected', 'published'])
        // Transitions are defined with a unique name, an origin place and a destination place
        ->addTransition(new Transition('to_review', 'draft', 'review'))
        ->addTransition(new Transition('publish', 'review', 'published'))
        ->addTransition(new Transition('reject', 'review', 'rejected'))
        ->build()
    ;
    
    $marking = new SingleStateMarkingStore('currentState');
    $workflow = new Workflow($definition, $marking);
    

    将定义的流程放到了 Registry仓库中去了

    use Symfony\Component\Workflow\Registry;
    use Acme\Entity\BlogPost;
    use Acme\Entity\Newsletter;
    
    $blogWorkflow = ...
    $newsletterWorkflow = ...
    
    $registry = new Registry();
    $registry->add($blogWorkflow, BlogPost::class); //blogpost类型的对象 可以获取到blogworkflow
    $registry->add($newsletterWorkflow, Newsletter::class);
    

    使用

    // ...
    $post = new BlogPost();
    $workflow = $registry->get($post);
    
    $workflow->can($post, 'publish'); // False
    $workflow->can($post, 'to_review'); // True
    
    $workflow->apply($post, 'to_review'); //对post 执行to_review  转变  状态到了 review
    $workflow->can($post, 'publish'); // True
    $workflow->getEnabledTransitions($post); // ['publish', 'reject']  //
    

    相关文章

      网友评论

          本文标题:The Workflow Component

          本文链接:https://www.haomeiwen.com/subject/ipmulxtx.html