Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #11882 [Workflow] Introducing the workflow component (fabpot,…
… lyrixx) This PR was merged into the 3.2-dev branch. Discussion ---------- [Workflow] Introducing the workflow component | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | not yet | Fixed tickets | n/a | License | MIT | Doc PR | n/a TODO: * [x] Add tests * [x] Add PHP doc * [x] Add Symfony fullstack integration (Config, DIC, command to dump the state-machine into graphiz format) So why another component? This component take another approach that what you can find on [Packagist](https://packagist.org/search/?q=state%20machine). Here, the workflow component is not tied to a specific object like with [Finite](https://github.com/yohang/Finite). It means that the component workflow is stateless and can be a symfony service. Some code: ```php #!/usr/bin/env php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Dumper\GraphvizDumper; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\MarkingStore\PropertyAccessorMarkingStore; use Symfony\Component\Workflow\MarkingStore\ScalarMarkingStore; use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Workflow; class Foo { public $marking; public function __construct($init = 'a') { $this->marking = $init; $this->marking = [$init => true]; } } $fooDefinition = new Definition(Foo::class); $fooDefinition->addPlaces([ 'a', 'b', 'c', 'd', 'e', 'f', 'g', ]); // name from to $fooDefinition->addTransition(new Transition('t1', 'a', ['b', 'c'])); $fooDefinition->addTransition(new Transition('t2', ['b', 'c'], 'd')); $fooDefinition->addTransition(new Transition('t3', 'd', 'e')); $fooDefinition->addTransition(new Transition('t4', 'd', 'f')); $fooDefinition->addTransition(new Transition('t5', 'e', 'g')); $fooDefinition->addTransition(new Transition('t6', 'f', 'g')); $graph = (new GraphvizDumper())->dump($fooDefinition); $ed = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch(), new \Monolog\Logger('app')); // $workflow = new Workflow($fooDefinition, new ScalarMarkingStore(), $ed); $workflow = new Workflow($fooDefinition, new PropertyAccessorMarkingStore(), $ed); $foo = new Foo(isset($argv[1]) ? $argv[1] : 'a'); $graph = (new GraphvizDumper())->dump($fooDefinition, $workflow->getMarking($foo)); dump([ 'AvailableTransitions' => $workflow->getAvailableTransitions($foo), 'CurrentMarking' => clone $workflow->getMarking($foo), 'can validate t1' => $workflow->can($foo, 't1'), 'can validate t3' => $workflow->can($foo, 't3'), 'can validate t6' => $workflow->can($foo, 't6'), 'apply t1' => clone $workflow->apply($foo, 't1'), 'can validate t2' => $workflow->can($foo, 't2'), 'apply t2' => clone $workflow->apply($foo, 't2'), 'can validate t1 bis' => $workflow->can($foo, 't1'), 'can validate t3 bis' => $workflow->can($foo, 't3'), 'can validate t6 bis' => $workflow->can($foo, 't6'), ]); ``` The workflown: ![workflow](https://cloud.githubusercontent.com/assets/408368/14183999/4a43483c-f773-11e5-9c8b-7f157e0cb75f.png) The output: ``` array:10 [ "AvailableTransitions" => array:1 [ 0 => Symfony\Component\Workflow\Transition {#4 -name: "t1" -froms: array:1 [ 0 => "a" ] -tos: array:2 [ 0 => "b" 1 => "c" ] } ] "CurrentMarking" => Symfony\Component\Workflow\Marking {#19 -places: array:1 [ "a" => true ] } "can validate t1" => true "can validate t3" => false "can validate t6" => false "apply t1" => Symfony\Component\Workflow\Marking {#22 -places: array:2 [ "b" => true "c" => true ] } "apply t2" => Symfony\Component\Workflow\Marking {#47 -places: array:1 [ "d" => true ] } "can validate t1 bis" => false "can validate t3 bis" => true "can validate t6 bis" => false ] ``` Commits ------- 078e27f [Workflow] Added initial set of files 17d59a7 added the first more-or-less working version of the Workflow component
- Loading branch information
Showing
44 changed files
with
2,555 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Extension; | ||
|
||
use Symfony\Component\Workflow\Registry; | ||
|
||
/** | ||
* WorkflowExtension. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class WorkflowExtension extends \Twig_Extension | ||
{ | ||
private $workflowRegistry; | ||
|
||
public function __construct(Registry $workflowRegistry) | ||
{ | ||
$this->workflowRegistry = $workflowRegistry; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
new \Twig_SimpleFunction('workflow_can', array($this, 'canTransition')), | ||
new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')), | ||
); | ||
} | ||
|
||
public function canTransition($object, $transition, $name = null) | ||
{ | ||
return $this->workflowRegistry->get($object, $name)->can($object, $transition); | ||
} | ||
|
||
public function getEnabledTransitions($object, $name = null) | ||
{ | ||
return $this->workflowRegistry->get($object, $name)->getEnabledTransitions($object); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'workflow'; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Symfony/Bundle/FrameworkBundle/Command/WorkflowDumpCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Workflow\Dumper\GraphvizDumper; | ||
use Symfony\Component\Workflow\Marking; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class WorkflowDumpCommand extends ContainerAwareCommand | ||
{ | ||
public function isEnabled() | ||
{ | ||
return $this->getContainer()->has('workflow.registry'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('workflow:dump') | ||
->setDefinition(array( | ||
new InputArgument('name', InputArgument::REQUIRED, 'A workflow name'), | ||
new InputArgument('marking', InputArgument::IS_ARRAY, 'A marking (a list of places)'), | ||
)) | ||
->setDescription('Dump a workflow') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command dumps the graphical representation of a | ||
workflow in DOT format | ||
%command.full_name% <workflow name> | dot -Tpng > workflow.png | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$workflow = $this->getContainer()->get('workflow.'.$input->getArgument('name')); | ||
$definition = $this->getProperty($workflow, 'definition'); | ||
|
||
$dumper = new GraphvizDumper(); | ||
|
||
$marking = new Marking(); | ||
foreach ($input->getArgument('marking') as $place) { | ||
$marking->mark($place); | ||
} | ||
|
||
$output->writeln($dumper->dump($definition, $marking)); | ||
} | ||
|
||
private function getProperty($object, $property) | ||
{ | ||
$reflectionProperty = new \ReflectionProperty(get_class($object), $property); | ||
$reflectionProperty->setAccessible(true); | ||
|
||
return $reflectionProperty->getValue($object); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.