Skip to content

[Workflow] added a TransitionsCollectionBuilder so DefinitionBuilder doesn't need Transition objects #20498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/Symfony/Component/Workflow/DefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@
class DefinitionBuilder
{
private $places = array();
private $transitions = array();
private $transitionsCollection;
private $initialPlace;

/**
* @param string[] $places
* @param Transition[] $transitions
* @param string[] $places
* @param (Transition|array)[] $transitions Nested values can be either instances of Transition or
* arrays with three values: the transition name, and two
* to pass string or arrays of string for froms and todos
*/
public function __construct(array $places = array(), array $transitions = array())
{
$this->addPlaces($places);
$this->addTransitions($transitions);
$this->transitionsCollection = new TransitionsCollectionBuilder();
$this->transitionsCollection->addTransitions($transitions);
}

/**
* @return Definition
*/
public function build()
{
return new Definition($this->places, $this->transitions, $this->initialPlace);
return new Definition($this->places, $this->transitionsCollection->getTransitions(), $this->initialPlace);
}

/**
Expand All @@ -50,7 +53,7 @@ public function build()
public function reset()
{
$this->places = array();
$this->transitions = array();
$this->transitionsCollection->reset();
$this->initialPlace = null;
}

Expand Down Expand Up @@ -79,15 +82,23 @@ public function addPlaces(array $places)
}
}

/**
* @param (Transition|array)[] $transitions Nested values can be either instances of Transition or
* arrays with three values: the transition name, and two
* to pass string or arrays of string for froms and todos
*/
public function addTransitions(array $transitions)
{
foreach ($transitions as $transition) {
$this->addTransition($transition);
}
$this->transitionsCollection->addTransitions($transitions);
}

public function addTransition(Transition $transition)
/**
* @param Transition|string $transition
* @param string[]|string|null $froms
* @param string[]|string|null $tos
*/
public function addTransition($transition, $froms = null, $tos = null)
{
$this->transitions[] = $transition;
$this->transitionsCollection->addTransition($transition, $froms, $tos);
}
}
30 changes: 27 additions & 3 deletions src/Symfony/Component/Workflow/Tests/DefinitionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public function testSetInitialPlace()
$this->assertEquals('b', $definition->getInitialPlace());
}

public function testAddTransition()
public function testAddTransitions()
{
$places = range('a', 'b');

$transition0 = new Transition('name0', $places[0], $places[1]);
$transition1 = new Transition('name1', $places[0], $places[1]);

$builder = new DefinitionBuilder($places, array($transition0));
$builder->addTransition($transition1);

$builder->addTransitions(array(
$transition1,
));

$definition = $builder->build();

Expand All @@ -40,6 +43,27 @@ public function testAddTransition()
$this->assertSame($transition1, $definition->getTransitions()[1]);
}

public function testAddTransition()
{
$name0 = 'name0';
$name2 = 'name2';
$places = range('a', 'c');

$transition0 = array($name0, $places[0], $places[1]);
$transition1 = new Transition('name1', $places[0], $places[1]);
$builder = new DefinitionBuilder($places, array($transition0));

$builder->addTransition($transition1);
$builder->addTransition($name2, $places[1], $places[2]);

$definition = $builder->build();

$this->assertCount(3, $definition->getTransitions());
$this->assertEquals(new Transition($name0, $places[0], $places[1]), $definition->getTransitions()[0]);
$this->assertSame($transition1, $definition->getTransitions()[1]);
$this->assertEquals(new Transition($name2, $places[1], $places[2]), $definition->getTransitions()[2]);
}

public function testAddPlace()
{
$builder = new DefinitionBuilder(array('a'), array());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\Dumper\GraphvizDumper;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\Transition;

class GraphvizDumperTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -63,12 +62,12 @@ public function provideComplexWorkflowDefinition()

$builder->addPlaces(range('a', 'g'));

$builder->addTransition(new Transition('t1', 'a', array('b', 'c')));
$builder->addTransition(new Transition('t2', array('b', 'c'), 'd'));
$builder->addTransition(new Transition('t3', 'd', 'e'));
$builder->addTransition(new Transition('t4', 'd', 'f'));
$builder->addTransition(new Transition('t5', 'e', 'g'));
$builder->addTransition(new Transition('t6', 'f', 'g'));
$builder->addTransition('t1', 'a', array('b', 'c'));
$builder->addTransition('t2', array('b', 'c'), 'd');
$builder->addTransition('t3', 'd', 'e');
$builder->addTransition('t4', 'd', 'f');
$builder->addTransition('t5', 'e', 'g');
$builder->addTransition('t6', 'f', 'g');

return $builder->build();
}
Expand All @@ -79,8 +78,8 @@ public function provideSimpleWorkflowDefinition()

$builder->addPlaces(range('a', 'c'));

$builder->addTransition(new Transition('t1', 'a', 'b'));
$builder->addTransition(new Transition('t2', 'b', 'c'));
$builder->addTransition('t1', 'a', 'b');
$builder->addTransition('t2', 'b', 'c');

return $builder->build();
}
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ protected function createComplexWorkflow()

$builder->addPlaces(range('a', 'g'));

$builder->addTransition(new Transition('t1', 'a', array('b', 'c')));
$builder->addTransition(new Transition('t2', array('b', 'c'), 'd'));
$builder->addTransition(new Transition('t3', 'd', 'e'));
$builder->addTransition(new Transition('t4', 'd', 'f'));
$builder->addTransition(new Transition('t5', 'e', 'g'));
$builder->addTransition(new Transition('t6', 'f', 'g'));
$builder->addTransition('t1', 'a', array('b', 'c'));
$builder->addTransition('t2', array('b', 'c'), 'd');
$builder->addTransition('t3', 'd', 'e');
$builder->addTransition('t4', 'd', 'f');
$builder->addTransition('t5', 'e', 'g');
$builder->addTransition('t6', 'f', 'g');

return $builder->build();

Expand Down
94 changes: 94 additions & 0 deletions src/Symfony/Component/Workflow/TransitionsCollectionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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\Component\Workflow;

use Symfony\Component\Workflow\Exception\InvalidArgumentException;
use Symfony\Component\Workflow\Exception\LogicException;

/**
* TransitionsCollectionBuilder.
*
* @author Jules Pietri <jules@heahprod.com>
*/
class TransitionsCollectionBuilder
{
/**
* @var Transition[]
*/
private $transitions = array();

/**
* @param Transition|string $transition
* @param string[]string|null $froms
* @param string[]string|null $tos
*/
public function addTransition($transition, $froms = null, $tos = null)
{
if ($transition instanceof Transition) {
$this->add($transition);

return;
}

$this->createTransition($transition, $froms, $tos);
}

/**
* @param array[] $transitions An array of arrays of three arguments to pass to "addTransition"
*/
public function addTransitions(array $transitions)
{
foreach ($transitions as $transition) {
if ($transition instanceof Transition) {
$this->add($transition);

continue;
}

if (!is_array($transition) || 3 !== count($transition)) {
throw new InvalidArgumentException(sprintf(
'Calling "%s" expected each entry to be an instance of "%s" or an array of three arguments to create a transition instance%s: "name", "froms", and "tos", but got %s.',
__METHOD__,
Transition::class,
isset($transition[0]) ? ' named "'.$transition[0].'"' : '',
is_array($transition) ? ' an array with '.count($transition).' entries' : gettype($transition)));
}
list($name, $froms, $tos) = $transition;
$this->addTransition($name, $froms, $tos);
}
}

public function getTransitions()
{
return $this->transitions;
}

public function reset()
{
return $this->transitions = array();
}

private function createTransition($name, $froms, $tos)
{
$this->add(new Transition($name, $froms, $tos));

}

private function add(Transition $newTransition) {
foreach ($this->transitions as $transition) {
if ($newTransition == $transition) {
throw new InvalidArgumentException(sprintf('The transition named "%s" has already been added.', $transition->getName()));
}
}
$this->transitions[] = $newTransition;
}
}