Workflow.php in State Machine 8
File
src/Plugin/Workflow/Workflow.php
View source
<?php
namespace Drupal\state_machine\Plugin\Workflow;
use Drupal\state_machine\Guard\GuardFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Workflow extends PluginBase implements WorkflowInterface, ContainerFactoryPluginInterface {
protected $guardFactory;
protected $states = [];
protected $transitions = [];
public function __construct(array $configuration, $plugin_id, $plugin_definition, GuardFactoryInterface $guard_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->guardFactory = $guard_factory;
foreach ($plugin_definition['states'] as $id => $state_definition) {
$this->states[$id] = new WorkflowState($id, $state_definition['label']);
}
foreach ($plugin_definition['transitions'] as $id => $transition_definition) {
$label = $transition_definition['label'];
$from_states = [];
foreach ($transition_definition['from'] as $from_state) {
$from_states[$from_state] = $this->states[$from_state];
}
$to_state = $this->states[$transition_definition['to']];
$this->transitions[$id] = new WorkflowTransition($id, $label, $from_states, $to_state);
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('state_machine.guard_factory'));
}
public function getId() {
return $this->pluginDefinition['id'];
}
public function getLabel() {
return $this->pluginDefinition['label'];
}
public function getGroup() {
return $this->pluginDefinition['group'];
}
public function getStates() {
return $this->states;
}
public function getState($id) {
return isset($this->states[$id]) ? $this->states[$id] : NULL;
}
public function getTransitions() {
return $this->transitions;
}
public function getTransition($id) {
return isset($this->transitions[$id]) ? $this->transitions[$id] : NULL;
}
public function getPossibleTransitions($state_id) {
if (empty($state_id)) {
return $this->transitions;
}
$possible_transitions = [];
foreach ($this->transitions as $id => $transition) {
if (array_key_exists($state_id, $transition
->getFromStates())) {
$possible_transitions[$id] = $transition;
}
}
return $possible_transitions;
}
public function getAllowedTransitions($state_id, EntityInterface $entity) {
$allowed_transitions = [];
foreach ($this
->getPossibleTransitions($state_id) as $transition_id => $transition) {
if ($this
->isTransitionAllowed($transition, $entity)) {
$allowed_transitions[$transition_id] = $transition;
}
}
return $allowed_transitions;
}
public function findTransition($from_state_id, $to_state_id) {
foreach ($this
->getPossibleTransitions($from_state_id) as $transition) {
if ($transition
->getToState()
->getId() == $to_state_id) {
return $transition;
}
}
return NULL;
}
protected function isTransitionAllowed(WorkflowTransition $transition, EntityInterface $entity) {
foreach ($this->guardFactory
->get($this
->getGroup()) as $guard) {
if ($guard
->allowed($transition, $this, $entity) === FALSE) {
return FALSE;
}
}
return TRUE;
}
}