1
|
|
|
<?php |
2
|
|
|
namespace PSB\Core\Pipeline; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use PSB\Core\Exception\PipelineBuildingException; |
6
|
|
|
use PSB\Core\Util\DependencyGraph\DependencyGraph; |
7
|
|
|
use PSB\Core\Util\DependencyGraph\GraphBuilderInterface; |
8
|
|
|
|
9
|
|
|
class StepDependencyGraphBuilder implements GraphBuilderInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var StepRegistration[] |
13
|
|
|
*/ |
14
|
|
|
private $steps; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var StepRegistration[] |
18
|
|
|
*/ |
19
|
|
|
private $idToStep = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The actual graph maintained as an array of arrays (node id to successor ids). |
23
|
|
|
* |
24
|
|
|
* @var [][] |
25
|
|
|
*/ |
26
|
|
|
private $idDirectedGraph = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param StepRegistration[] $steps |
30
|
|
|
*/ |
31
|
12 |
|
public function __construct($steps) |
32
|
|
|
{ |
33
|
12 |
|
$this->steps = $steps; |
34
|
12 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return DependencyGraph |
38
|
|
|
*/ |
39
|
11 |
|
public function build() |
40
|
|
|
{ |
41
|
|
|
// prepare the directed graph lists and id to step mapping |
42
|
11 |
|
foreach ($this->steps as $step) { |
43
|
11 |
|
$this->idDirectedGraph[$step->getStepId()] = []; |
44
|
11 |
|
$this->idToStep[$step->getStepId()] = $step; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// build the actual graph from before and after dependencies |
48
|
11 |
|
foreach ($this->steps as $step) { |
49
|
11 |
|
$this->addBeforesToDirectedGraph($step); |
50
|
10 |
|
$this->addAftersToDirectedGraph($step); |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
return new DependencyGraph($this->idToStep, $this->idDirectedGraph); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param StepRegistration $currentStep |
58
|
|
|
* |
59
|
|
|
* @throws PipelineBuildingException |
60
|
|
|
*/ |
61
|
11 |
View Code Duplication |
private function addBeforesToDirectedGraph(StepRegistration $currentStep) |
62
|
|
|
{ |
63
|
11 |
|
foreach ($currentStep->getBefores() as $before) { |
64
|
4 |
|
if (!isset($this->idToStep[$before->getDependsOnId()]) && $before->isEnforced()) { |
65
|
1 |
|
$allStepIds = implode(',', array_keys($this->idToStep)); |
66
|
1 |
|
throw new PipelineBuildingException( |
67
|
1 |
|
"Registration '{$before->getDependsOnId()}' specified in the insertbefore of the '{$currentStep->getStepId()}' step does not exist. Current step ids: $allStepIds." |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
if (isset($this->idToStep[$before->getDependsOnId()])) { |
72
|
2 |
|
$this->idDirectedGraph[$before->getDependantId()][] = $before->getDependsOnId(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
10 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param StepRegistration $currentStep |
79
|
|
|
* |
80
|
|
|
* @throws PipelineBuildingException |
81
|
|
|
*/ |
82
|
10 |
View Code Duplication |
private function addAftersToDirectedGraph(StepRegistration $currentStep) |
83
|
|
|
{ |
84
|
10 |
|
foreach ($currentStep->getAfters() as $after) { |
85
|
3 |
|
if (!isset($this->idToStep[$after->getDependsOnId()]) && $after->isEnforced()) { |
86
|
1 |
|
$allStepIds = implode(',', array_keys($this->idToStep)); |
87
|
1 |
|
throw new PipelineBuildingException( |
88
|
1 |
|
"Registration '{$after->getDependsOnId()}' specified in the insertafter of the '{$currentStep->getStepId()}' step does not exist. Current step ids: $allStepIds." |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
if (isset($this->idToStep[$after->getDependsOnId()])) { |
93
|
1 |
|
$this->idDirectedGraph[$after->getDependsOnId()][] = $after->getDependantId(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
9 |
|
} |
97
|
|
|
} |
98
|
|
|
|