-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProcessConfigurationRegistry.php
More file actions
183 lines (163 loc) · 6.61 KB
/
Copy pathProcessConfigurationRegistry.php
File metadata and controls
183 lines (163 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (C) 2017-2021 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Registry;
use CleverAge\ProcessBundle\Exception\InvalidProcessConfigurationException;
use function array_key_exists;
use function array_keys;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Exception\MissingProcessException;
use Psr\Log\LogLevel;
/**
* Build and holds all the process configurations
*
* @author Valentin Clavreul <vclavreul@clever-age.com>
* @author Vincent Chalnot <vchalnot@clever-age.com>
*/
class ProcessConfigurationRegistry
{
/** @var array */
protected $rawConfiguration;
/** @var string */
protected $defaultErrorStrategy;
/** @var ProcessConfiguration[] */
protected $processConfigurations = [];
/**
* @param array $rawConfiguration
* @param string $defaultErrorStrategy
*/
public function __construct(array $rawConfiguration, string $defaultErrorStrategy)
{
$this->rawConfiguration = $rawConfiguration;
$this->defaultErrorStrategy = $defaultErrorStrategy;
}
/**
* @param string $processCode
*
* @return ProcessConfiguration
* @throws MissingProcessException
*
*/
public function getProcessConfiguration(string $processCode): ProcessConfiguration
{
if (!$this->hasProcessConfiguration($processCode)) {
throw MissingProcessException::create($processCode);
}
$this->resolveConfiguration($processCode);
return $this->processConfigurations[$processCode];
}
/**
* @return ProcessConfiguration[]
*/
public function getProcessConfigurations(): array
{
foreach (array_keys($this->rawConfiguration) as $processCode) {
$this->resolveConfiguration($processCode);
}
return $this->processConfigurations;
}
/**
* @param string $processCode
*
* @return bool
*/
public function hasProcessConfiguration(string $processCode): bool
{
return array_key_exists($processCode, $this->rawConfiguration);
}
/**
* @param string $processCode
*/
protected function resolveConfiguration(string $processCode): void
{
if (array_key_exists($processCode, $this->processConfigurations)) {
return;
}
$rawProcessConfiguration = $this->rawConfiguration[$processCode];
/** @var TaskConfiguration[] $taskConfigurations */
$taskConfigurations = [];
/** @noinspection ForeachSourceInspection */
foreach ($rawProcessConfiguration['tasks'] as $taskCode => $rawTaskConfiguration) {
if (\count($rawTaskConfiguration['errors']) > 0) {
if (\count($rawTaskConfiguration['error_outputs']) > 0) {
$m = "Don't define both 'errors' and 'error_outputs' for task {$taskCode}, these options ";
$m .= "are the same, 'errors' is deprecated, just use the new one 'error_outputs'";
throw new \LogicException($m);
}
$rawTaskConfiguration['error_outputs'] = $rawTaskConfiguration['errors'];
}
$taskConfigurations[$taskCode] = new TaskConfiguration(
$taskCode,
$rawTaskConfiguration['service'],
$rawTaskConfiguration['options'],
$rawTaskConfiguration['description'],
$rawTaskConfiguration['help'],
$rawTaskConfiguration['outputs'],
$rawTaskConfiguration['error_outputs'],
$rawTaskConfiguration['error_strategy'] ?? $this->defaultErrorStrategy,
$rawTaskConfiguration['log_errors'] ? $rawTaskConfiguration['log_level'] : LogLevel::DEBUG
);
}
$processConfig = new ProcessConfiguration(
$processCode,
$taskConfigurations,
$rawProcessConfiguration['options'],
$rawProcessConfiguration['entry_point'],
$rawProcessConfiguration['end_point'],
$rawProcessConfiguration['description'],
$rawProcessConfiguration['help'],
$rawProcessConfiguration['public']
);
// Set links between tasks
foreach ($taskConfigurations as $taskConfig) {
foreach ($taskConfig->getOutputs() as $nextTaskCode) {
$nextTaskConfig = $processConfig->getTaskConfiguration($nextTaskCode);
$taskConfig->addNextTaskConfiguration($nextTaskConfig);
$nextTaskConfig->addPreviousTaskConfiguration($taskConfig);
}
foreach ($taskConfig->getErrorOutputs() as $errorTaskCode) {
$errorTaskConfig = $processConfig->getTaskConfiguration($errorTaskCode);
$taskConfig->addErrorTaskConfiguration($errorTaskConfig);
$errorTaskConfig->addPreviousTaskConfiguration($taskConfig);
}
}
// Mark error branches
foreach ($taskConfigurations as $taskConfig) {
foreach ($taskConfig->getErrorTasksConfigurations() as $errorTaskConfig) {
$this->markErrorBranch($errorTaskConfig);
}
}
// Un-mark non-error branch (may be important for task that are in both branches)
foreach ($processConfig->getMainTaskGroup() as $taskCode) {
$task = $taskConfigurations[$taskCode];
if ($task->isRoot()) {
$this->markErrorBranch($task, false);
}
}
// #106 - entry point should not have an ancestor
if ($processConfig->getEntryPoint() && $processConfig->getEntryPoint()->getPreviousTasksConfigurations()) {
throw InvalidProcessConfigurationException::createEntryPointHasAncestors($processConfig, $processConfig->getEntryPoint());
}
$this->processConfigurations[$processCode] = $processConfig;
}
/**
* @param TaskConfiguration $taskConfig
* @param bool $isErrorBranch
*/
protected function markErrorBranch(TaskConfiguration $taskConfig, $isErrorBranch = true): void
{
if ($taskConfig->isInErrorBranch() !== $isErrorBranch) {
$taskConfig->setInErrorBranch($isErrorBranch);
foreach ($taskConfig->getNextTasksConfigurations() as $nextTasksConfig) {
$this->markErrorBranch($nextTasksConfig, $isErrorBranch);
}
}
}
}