-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[POC] [Workflow] Made StateMachine supports only single state marking #20491
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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\Exception; | ||
|
||
/** | ||
* An InvalidMarkingException is thrown when a Marking does not | ||
* match the current workflow. | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
class InvalidMarkingException extends LogicException | ||
{ | ||
/** | ||
* @param string $expectedClass | ||
* @param mixed $marking | ||
*/ | ||
public function __construct($expectedClass, $marking) | ||
{ | ||
$this->message = sprintf('Marking must be an instance of "%", but got "%"', $expectedClass, is_object($marking) ? get_class($marking) : gettype($marking)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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\Exception; | ||
|
||
/** | ||
* An InvalidMarkingStrategyException is thrown when the marking strategy | ||
* does not match the Marking instance. | ||
* | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
class InvalidMarkingStrategyException extends LogicException | ||
{ | ||
/** | ||
* @param string $markingStoreClass | ||
*/ | ||
public function __construct($markingStoreClass) | ||
{ | ||
$this->message = sprintf('The marking store has no strategy set. Did you forgot to call "%::__construct()"?', $markingStoreClass); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,41 +12,45 @@ | |
namespace Symfony\Component\Workflow; | ||
|
||
/** | ||
* Marking contains the place of every tokens. | ||
* A base Marking which contains the state of the | ||
* state of a Workflow or a StateMachine a representation | ||
* by places with tokens. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
class Marking | ||
abstract class Marking | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this class is really needed.. why not a pointer interface and 2 final implementations instead? |
||
{ | ||
private $places = array(); | ||
const STRATEGY_SINGLE_STATE = 'single_state'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These constants belong to the |
||
const STRATEGY_MULTIPLE_STATE = 'multiple_state'; | ||
|
||
protected $places = array(); | ||
|
||
/** | ||
* @param string[] $representation Keys are the place name and values should be 1 | ||
* @param string $place | ||
* | ||
* @return bool | ||
*/ | ||
public function __construct(array $representation = array()) | ||
{ | ||
foreach ($representation as $place => $nbToken) { | ||
$this->mark($place); | ||
} | ||
} | ||
|
||
public function mark($place) | ||
{ | ||
$this->places[$place] = 1; | ||
} | ||
|
||
public function unmark($place) | ||
{ | ||
unset($this->places[$place]); | ||
} | ||
|
||
public function has($place) | ||
final public function has($place) | ||
{ | ||
return isset($this->places[$place]); | ||
} | ||
|
||
public function getPlaces() | ||
/** | ||
* @return int[] An array of places as keys and token counts as values. | ||
*/ | ||
final public function getState() | ||
{ | ||
return $this->places; | ||
} | ||
|
||
/** | ||
* @param string $place | ||
*/ | ||
abstract public function mark($place); | ||
|
||
/** | ||
* @param string $place | ||
*/ | ||
abstract public function unmark($place); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?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\MarkingStore; | ||
|
||
use Symfony\Component\Workflow\Exception\InvalidArgumentException; | ||
use Symfony\Component\Workflow\Exception\InvalidMarkingException; | ||
use Symfony\Component\Workflow\Exception\InvalidMarkingStrategyException; | ||
use Symfony\Component\Workflow\Marking; | ||
use Symfony\Component\Workflow\MultipleStateMarking; | ||
use Symfony\Component\Workflow\SingleStateMarking; | ||
|
||
/** | ||
* A MarkingStore is the interface between the Workflow Component and a | ||
* plain old PHP object: the subject. | ||
* | ||
* It converts the Marking into something understandable by the subject and vice | ||
* versa. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
abstract class MarkingStore | ||
{ | ||
private $strategy; | ||
|
||
/** | ||
* @param string $strategy A Marking constant | ||
*/ | ||
public function __construct($strategy = Marking::STRATEGY_MULTIPLE_STATE) | ||
{ | ||
$strategies = array(Marking::STRATEGY_SINGLE_STATE, Marking::STRATEGY_MULTIPLE_STATE); | ||
if (!in_array($strategy, $strategies, true)) { | ||
throw new InvalidArgumentException(sprintf('Marking strategy must be one of "%s", but got "%".', implode('" or "', $strategies), $strategy)); | ||
} | ||
|
||
$this->strategy = $strategy; | ||
} | ||
|
||
/** | ||
* Returns the strategy used for marking. | ||
* | ||
* @return string A Marking constant | ||
*/ | ||
final public function getStrategy() | ||
{ | ||
return $this->strategy; | ||
} | ||
|
||
/** | ||
* Gets a Marking from a subject. | ||
* | ||
* @param object $subject A subject | ||
* | ||
* @return Marking The marking | ||
* | ||
* @throws InvalidMarkingException When the marking does not match the strategy | ||
* @throws InvalidMarkingStrategyException When the strategy in unknown | ||
*/ | ||
public function getMarking($subject) | ||
{ | ||
if (Marking::STRATEGY_MULTIPLE_STATE === $this->strategy) { | ||
$marking = $this->getMultipleStateMarking($subject); | ||
|
||
if (!$marking instanceof MultipleStateMarking) { | ||
throw new InvalidMarkingException(MultipleStateMarking::class, $marking); | ||
} | ||
|
||
return $marking; | ||
} | ||
|
||
if (Marking::STRATEGY_SINGLE_STATE === $this->strategy) { | ||
$marking = $this->getSingleStateMarking($subject); | ||
|
||
if (!$marking instanceof SingleStateMarking) { | ||
throw new InvalidMarkingException(SingleStateMarking::class, $marking); | ||
} | ||
|
||
return $marking; | ||
} | ||
|
||
throw new InvalidMarkingStrategyException(static::class); | ||
} | ||
|
||
/** | ||
* Sets a Marking to a subject. | ||
* | ||
* Must return an | ||
* | ||
* @param object $subject A subject | ||
* @param Marking $marking A marking | ||
* | ||
* @throws InvalidMarkingException When the marking does not match the strategy | ||
* @throws InvalidMarkingStrategyException When the strategy in unknown | ||
*/ | ||
public function setMarking($subject, Marking $marking) | ||
{ | ||
if (Marking::STRATEGY_MULTIPLE_STATE === $this->strategy) { | ||
if (!$marking instanceof MultipleStateMarking) { | ||
throw new InvalidMarkingException(MultipleStateMarking::class, $marking); | ||
} | ||
|
||
$this->setMultipleStateMarking($subject, $marking); | ||
|
||
return; | ||
} | ||
|
||
if (Marking::STRATEGY_SINGLE_STATE === $this->strategy) { | ||
if (!$marking instanceof SingleStateMarking) { | ||
throw new InvalidMarkingException(SingleStateMarking::class, $marking); | ||
} | ||
|
||
$this->setSingleStateMarking($subject, $marking); | ||
|
||
return; | ||
} | ||
|
||
throw new InvalidMarkingStrategyException(static::class); | ||
} | ||
|
||
/** | ||
* Gets a SingleStateMarking from a subject. | ||
* | ||
* @param object $subject A subject | ||
* | ||
* @return SingleStateMarking The marking | ||
*/ | ||
abstract protected function getSingleStateMarking($subject); | ||
|
||
/** | ||
* Sets a SingleStateMarking to a subject. | ||
* | ||
* @param object $subject | ||
* @param SingleStateMarking $marking | ||
*/ | ||
abstract protected function setSingleStateMarking($subject, SingleStateMarking $marking); | ||
|
||
/** | ||
* Gets a MultipleStateMarking from a subject. | ||
* | ||
* @param object $subject A subject | ||
* | ||
* @return MultipleStateMarking The marking | ||
*/ | ||
abstract protected function getMultipleStateMarking($subject); | ||
|
||
/** | ||
* Sets a MultipleStateMarking to a subject. | ||
* | ||
* @param object $subject | ||
* @param MultipleStateMarking $marking | ||
*/ | ||
abstract protected function setMultipleStateMarking($subject, MultipleStateMarking $marking); | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$markingStrategy = 'state_machine' === $type ? Marking::STRATEGY_SINGLE_STATE : Marking::STRATEGY_MULTIPLE_STATE;
?