Skip to content

Commit b374944

Browse files
Nyholmlyrixx
authored andcommitted
[Workflow] Added Definition builder
1 parent f92a103 commit b374944

File tree

8 files changed

+215
-55
lines changed

8 files changed

+215
-55
lines changed

Definition.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
/**
1818
* @author Fabien Potencier <fabien@symfony.com>
1919
* @author Grégoire Pineau <lyrixx@lyrixx.info>
20+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2021
*/
21-
class Definition
22+
final class Definition
2223
{
2324
private $places = array();
2425
private $transitions = array();
@@ -29,18 +30,28 @@ class Definition
2930
*
3031
* @param string[] $places
3132
* @param Transition[] $transitions
33+
* @param string|null $initialPlace
3234
*/
33-
public function __construct(array $places = array(), array $transitions = array())
35+
public function __construct(array $places, array $transitions, $initialPlace = null)
3436
{
3537
$this->addPlaces($places);
36-
$this->addTransitions($transitions);
38+
$this->setInitialPlace($initialPlace);
39+
foreach ($transitions as $transition) {
40+
$this->addTransition($transition);
41+
}
3742
}
3843

44+
/**
45+
* @return string|null
46+
*/
3947
public function getInitialPlace()
4048
{
4149
return $this->initialPlace;
4250
}
4351

52+
/**
53+
* @return string[]
54+
*/
4455
public function getPlaces()
4556
{
4657
return $this->places;
@@ -54,16 +65,20 @@ public function getTransitions()
5465
return $this->transitions;
5566
}
5667

57-
public function setInitialPlace($place)
68+
private function setInitialPlace($place)
5869
{
70+
if (null === $place) {
71+
return;
72+
}
73+
5974
if (!isset($this->places[$place])) {
6075
throw new LogicException(sprintf('Place "%s" cannot be the initial place as it does not exist.', $place));
6176
}
6277

6378
$this->initialPlace = $place;
6479
}
6580

66-
public function addPlace($place)
81+
private function addPlace($place)
6782
{
6883
if (!preg_match('{^[\w\d_-]+$}', $place)) {
6984
throw new InvalidArgumentException(sprintf('The place "%s" contains invalid characters.', $place));
@@ -76,21 +91,14 @@ public function addPlace($place)
7691
$this->places[$place] = $place;
7792
}
7893

79-
public function addPlaces(array $places)
94+
private function addPlaces(array $places)
8095
{
8196
foreach ($places as $place) {
8297
$this->addPlace($place);
8398
}
8499
}
85100

86-
public function addTransitions(array $transitions)
87-
{
88-
foreach ($transitions as $transition) {
89-
$this->addTransition($transition);
90-
}
91-
}
92-
93-
public function addTransition(Transition $transition)
101+
private function addTransition(Transition $transition)
94102
{
95103
$name = $transition->getName();
96104

DefinitionBuilder.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow;
13+
14+
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
15+
16+
/**
17+
* Builds a definition.
18+
*
19+
* @author Fabien Potencier <fabien@symfony.com>
20+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
22+
*/
23+
class DefinitionBuilder
24+
{
25+
private $places = array();
26+
private $transitions = array();
27+
private $initialPlace;
28+
29+
/**
30+
* @param string[] $places
31+
* @param Transition[] $transitions
32+
*/
33+
public function __construct(array $places = array(), array $transitions = array())
34+
{
35+
$this->addPlaces($places);
36+
$this->addTransitions($transitions);
37+
}
38+
39+
/**
40+
* @return Definition
41+
*/
42+
public function build()
43+
{
44+
return new Definition($this->places, $this->transitions, $this->initialPlace);
45+
}
46+
47+
/**
48+
* Clear all data in the builder.
49+
*/
50+
public function reset()
51+
{
52+
$this->places = array();
53+
$this->transitions = array();
54+
$this->initialPlace = null;
55+
}
56+
57+
public function setInitialPlace($place)
58+
{
59+
$this->initialPlace = $place;
60+
}
61+
62+
public function addPlace($place)
63+
{
64+
if (!preg_match('{^[\w\d_-]+$}', $place)) {
65+
throw new InvalidArgumentException(sprintf('The place "%s" contains invalid characters.', $place));
66+
}
67+
68+
if (!$this->places) {
69+
$this->initialPlace = $place;
70+
}
71+
72+
$this->places[$place] = $place;
73+
}
74+
75+
public function addPlaces(array $places)
76+
{
77+
foreach ($places as $place) {
78+
$this->addPlace($place);
79+
}
80+
}
81+
82+
public function addTransitions(array $transitions)
83+
{
84+
foreach ($transitions as $transition) {
85+
$this->addTransition($transition);
86+
}
87+
}
88+
89+
public function addTransition(Transition $transition)
90+
{
91+
$this->transitions[] = $transition;
92+
}
93+
}

Tests/DefinitionBuilderTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests;
4+
5+
use Symfony\Component\Workflow\DefinitionBuilder;
6+
use Symfony\Component\Workflow\Transition;
7+
8+
class DefinitionBuilderTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \Symfony\Component\Workflow\Exception\InvalidArgumentException
12+
*/
13+
public function testAddPlaceInvalidName()
14+
{
15+
$builder = new DefinitionBuilder(array('a"', 'b'));
16+
}
17+
18+
public function testSetInitialPlace()
19+
{
20+
$builder = new DefinitionBuilder(array('a', 'b'));
21+
$builder->setInitialPlace('b');
22+
$definition = $builder->build();
23+
24+
$this->assertEquals('b', $definition->getInitialPlace());
25+
}
26+
27+
public function testAddTransition()
28+
{
29+
$places = range('a', 'b');
30+
31+
$transition0 = new Transition('name0', $places[0], $places[1]);
32+
$transition1 = new Transition('name1', $places[0], $places[1]);
33+
$builder = new DefinitionBuilder($places, array($transition0));
34+
$builder->addTransition($transition1);
35+
36+
$definition = $builder->build();
37+
38+
$this->assertCount(2, $definition->getTransitions());
39+
$this->assertSame($transition0, $definition->getTransitions()[0]);
40+
$this->assertSame($transition1, $definition->getTransitions()[1]);
41+
}
42+
43+
public function testAddPlace()
44+
{
45+
$builder = new DefinitionBuilder(array('a'), array());
46+
$builder->addPlace('b');
47+
48+
$definition = $builder->build();
49+
50+
$this->assertCount(2, $definition->getPlaces());
51+
$this->assertEquals('a', $definition->getPlaces()['a']);
52+
$this->assertEquals('b', $definition->getPlaces()['b']);
53+
}
54+
}

Tests/DefinitionTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
1010
public function testAddPlaces()
1111
{
1212
$places = range('a', 'e');
13-
$definition = new Definition($places);
13+
$definition = new Definition($places, array());
1414

1515
$this->assertCount(5, $definition->getPlaces());
1616

@@ -23,15 +23,13 @@ public function testAddPlaces()
2323
public function testAddPlacesInvalidArgument()
2424
{
2525
$places = array('a"', 'e"');
26-
$definition = new Definition($places);
26+
$definition = new Definition($places, array());
2727
}
2828

2929
public function testSetInitialPlace()
3030
{
3131
$places = range('a', 'e');
32-
$definition = new Definition($places);
33-
34-
$definition->setInitialPlace($places[3]);
32+
$definition = new Definition($places, array(), $places[3]);
3533

3634
$this->assertEquals($places[3], $definition->getInitialPlace());
3735
}
@@ -42,9 +40,7 @@ public function testSetInitialPlace()
4240
*/
4341
public function testSetInitialPlaceAndPlaceIsNotDefined()
4442
{
45-
$definition = new Definition();
46-
47-
$definition->setInitialPlace('d');
43+
$definition = new Definition(array(), array(), 'd');
4844
}
4945

5046
public function testAddTransition()

Tests/Dumper/GraphvizDumperTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Symfony\Component\Workflow\Tests\Dumper;
44

5-
use Symfony\Component\Workflow\Definition;
5+
use Symfony\Component\Workflow\DefinitionBuilder;
66
use Symfony\Component\Workflow\Dumper\GraphvizDumper;
77
use Symfony\Component\Workflow\Marking;
88
use Symfony\Component\Workflow\Transition;
@@ -39,7 +39,7 @@ public function testWorkflowWithMarking($definition, $marking, $expected)
3939
public function provideWorkflowDefinitionWithMarking()
4040
{
4141
yield array(
42-
$this->createprovideComplexWorkflowDefinition(),
42+
$this->provideComplexWorkflowDefinition(),
4343
new Marking(array('b' => 1)),
4444
$this->createComplexWorkflowDumpWithMarking(),
4545
);
@@ -53,36 +53,36 @@ public function provideWorkflowDefinitionWithMarking()
5353

5454
public function provideWorkflowDefinitionWithoutMarking()
5555
{
56-
yield array($this->createprovideComplexWorkflowDefinition(), $this->provideComplexWorkflowDumpWithoutMarking());
56+
yield array($this->provideComplexWorkflowDefinition(), $this->provideComplexWorkflowDumpWithoutMarking());
5757
yield array($this->provideSimpleWorkflowDefinition(), $this->provideSimpleWorkflowDumpWithoutMarking());
5858
}
5959

60-
public function createprovideComplexWorkflowDefinition()
60+
public function provideComplexWorkflowDefinition()
6161
{
62-
$definition = new Definition();
62+
$builder = new DefinitionBuilder();
6363

64-
$definition->addPlaces(range('a', 'g'));
64+
$builder->addPlaces(range('a', 'g'));
6565

66-
$definition->addTransition(new Transition('t1', 'a', array('b', 'c')));
67-
$definition->addTransition(new Transition('t2', array('b', 'c'), 'd'));
68-
$definition->addTransition(new Transition('t3', 'd', 'e'));
69-
$definition->addTransition(new Transition('t4', 'd', 'f'));
70-
$definition->addTransition(new Transition('t5', 'e', 'g'));
71-
$definition->addTransition(new Transition('t6', 'f', 'g'));
66+
$builder->addTransition(new Transition('t1', 'a', array('b', 'c')));
67+
$builder->addTransition(new Transition('t2', array('b', 'c'), 'd'));
68+
$builder->addTransition(new Transition('t3', 'd', 'e'));
69+
$builder->addTransition(new Transition('t4', 'd', 'f'));
70+
$builder->addTransition(new Transition('t5', 'e', 'g'));
71+
$builder->addTransition(new Transition('t6', 'f', 'g'));
7272

73-
return $definition;
73+
return $builder->build();
7474
}
7575

7676
public function provideSimpleWorkflowDefinition()
7777
{
78-
$definition = new Definition();
78+
$builder = new DefinitionBuilder();
7979

80-
$definition->addPlaces(range('a', 'c'));
80+
$builder->addPlaces(range('a', 'c'));
8181

82-
$definition->addTransition(new Transition('t1', 'a', 'b'));
83-
$definition->addTransition(new Transition('t2', 'b', 'c'));
82+
$builder->addTransition(new Transition('t1', 'a', 'b'));
83+
$builder->addTransition(new Transition('t2', 'b', 'c'));
8484

85-
return $definition;
85+
return $builder->build();
8686
}
8787

8888
public function createComplexWorkflowDumpWithMarking()

Tests/RegistryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ protected function setUp()
1818

1919
$this->registry = new Registry();
2020

21-
$this->registry->add(new Workflow(new Definition(), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), Subject1::class);
22-
$this->registry->add(new Workflow(new Definition(), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), Subject2::class);
23-
$this->registry->add(new Workflow(new Definition(), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), Subject2::class);
21+
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), Subject1::class);
22+
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), Subject2::class);
23+
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), Subject2::class);
2424
}
2525

2626
protected function tearDown()

0 commit comments

Comments
 (0)