The Workflow Component

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

流程图片


The Workflow Component_第1张图片
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)