-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Workflow] Add transition blockers #26076
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,21 +11,52 @@ | |
|
|
||
| namespace Symfony\Component\Workflow\Event; | ||
|
|
||
| use Symfony\Component\Workflow\Marking; | ||
| use Symfony\Component\Workflow\Transition; | ||
| use Symfony\Component\Workflow\TransitionBlocker; | ||
| use Symfony\Component\Workflow\TransitionBlockerList; | ||
|
|
||
| /** | ||
| * @author Fabien Potencier <[email protected]> | ||
| * @author Grégoire Pineau <[email protected]> | ||
| */ | ||
| class GuardEvent extends Event | ||
| { | ||
| private $blocked = false; | ||
| private $transitionBlockerList; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function __construct($subject, Marking $marking, Transition $transition, $workflowName = 'unnamed') | ||
| { | ||
| parent::__construct($subject, $marking, $transition, $workflowName); | ||
|
|
||
| $this->transitionBlockerList = new TransitionBlockerList(); | ||
| } | ||
|
|
||
| public function isBlocked(): bool | ||
| { | ||
| return 0 !== count($this->transitionBlockerList); | ||
| } | ||
|
|
||
| public function setBlocked(bool $blocked): void | ||
|
lyrixx marked this conversation as resolved.
|
||
| { | ||
| if (!$blocked) { | ||
| $this->transitionBlockerList = new TransitionBlockerList(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $this->transitionBlockerList->add(TransitionBlocker::createUnknownReason($this->getTransition()->getName())); | ||
| } | ||
|
|
||
| public function isBlocked() | ||
| public function getTransitionBlockerList(): TransitionBlockerList | ||
| { | ||
| return $this->blocked; | ||
| return $this->transitionBlockerList; | ||
| } | ||
|
|
||
| public function setBlocked($blocked) | ||
| public function addTransitionBlocker(TransitionBlocker $transitionBlocker): void | ||
| { | ||
| $this->blocked = (bool) $blocked; | ||
| $this->transitionBlockerList->add($transitionBlocker); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Workflow\Exception; | ||
|
|
||
| use Symfony\Component\Workflow\TransitionBlockerList; | ||
|
|
||
| /** | ||
| * Thrown by the workflow when a transition is not enabled. | ||
| */ | ||
| class BlockedTransitionException extends LogicException | ||
| { | ||
| private $transitionBlockerList; | ||
|
|
||
| public function __construct(string $message, TransitionBlockerList $transitionBlockerList) | ||
| { | ||
| parent::__construct($message); | ||
|
|
||
| $this->transitionBlockerList = $transitionBlockerList; | ||
| } | ||
|
|
||
| public function getTransitionBlockerList(): TransitionBlockerList | ||
| { | ||
| return $this->transitionBlockerList; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Workflow\Exception; | ||
|
|
||
| /** | ||
| * Thrown by Workflow when an undefined transition is applied on a subject. | ||
| */ | ||
| class UndefinedTransitionException extends LogicException | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Workflow; | ||
|
|
||
| /** | ||
| * A reason why a transition cannot be performed for a subject. | ||
| */ | ||
| class TransitionBlocker | ||
| { | ||
| const REASON_TRANSITION_NOT_DEFINED = '80f2a8e9-ee53-408a-9dd8-cce09e031db8'; | ||
| const REASON_TRANSITION_NOT_APPLICABLE = '19beefc8-6b1e-4716-9d07-a39bd6d16e34'; | ||
| const REASON_TRANSITION_UNKNOWN = 'e8b5bbb9-5913-4b98-bfa6-65dbd228a82a'; | ||
|
|
||
| private $message; | ||
| private $code; | ||
|
|
||
| /** | ||
| * @var array This is useful if you would like to pass around the condition values, that | ||
|
Member
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. I would move this doc to the constructor phpdoc instead as this var is private. |
||
| * blocked the transition. E.g. for a condition "distance must be larger than | ||
| * 5 miles", you might want to pass around the value of 5. | ||
| */ | ||
| private $parameters; | ||
|
|
||
| public function __construct(string $message, string $code, array $parameters = array()) | ||
|
Member
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. We should probably document what code is and the different possibilities. |
||
| { | ||
| $this->message = $message; | ||
| $this->code = $code; | ||
| $this->parameters = $parameters; | ||
| } | ||
|
|
||
| /** | ||
| * Create a blocker, that says the transition cannot be made because it is undefined | ||
| * in a workflow. | ||
| * | ||
| * @param string $transitionName | ||
| * @param string $workflowName | ||
| * | ||
| * @return static | ||
| */ | ||
| public static function createNotDefined(string $transitionName, string $workflowName): self | ||
| { | ||
| $message = sprintf('Transition "%s" is not defined in workflow "%s".', $transitionName, $workflowName); | ||
| $parameters = array( | ||
| 'transitionName' => $transitionName, | ||
| 'workflowName' => $workflowName, | ||
| ); | ||
|
|
||
| return new static($message, self::REASON_TRANSITION_NOT_DEFINED, $parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Create a blocker, that says the transition cannot be made because the subject | ||
| * is in wrong place (i.e. status). | ||
| * | ||
| * @param string $transitionName | ||
| * | ||
| * @return static | ||
| */ | ||
| public static function createNotApplicable(string $transitionName): self | ||
| { | ||
| $message = sprintf('Transition "%s" cannot be made, because the subject is not in the required place.', $transitionName); | ||
| $parameters = array( | ||
| 'transitionName' => $transitionName, | ||
| ); | ||
|
|
||
| return new static($message, self::REASON_TRANSITION_NOT_APPLICABLE, $parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Create a blocker, that says the transition cannot be made because of unknown | ||
| * reason. | ||
| * | ||
| * This blocker code is chiefly for preserving backwards compatibility. | ||
| * | ||
| * @param string $transitionName | ||
| * | ||
| * @return static | ||
| */ | ||
| public static function createUnknownReason(string $transitionName): self | ||
| { | ||
| $message = sprintf('Transition "%s" cannot be made, because of unknown reason.', $transitionName); | ||
| $parameters = array( | ||
| 'transitionName' => $transitionName, | ||
| ); | ||
|
|
||
| return new static($message, self::REASON_TRANSITION_UNKNOWN, $parameters); | ||
| } | ||
|
|
||
| public function getMessage(): string | ||
| { | ||
| return $this->message; | ||
| } | ||
|
|
||
| public function getCode(): string | ||
| { | ||
| return $this->code; | ||
| } | ||
|
|
||
| public function getParameters(): array | ||
| { | ||
| return $this->parameters; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.