forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessConfigurationsManager.php
116 lines (103 loc) · 3.89 KB
/
ProcessConfigurationsManager.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Manager;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry;
use CleverAge\ProcessBundle\Validator\ConstraintLoader;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraint;
/**
* @phpstan-type UiOptions array{
* 'source': ?string,
* 'target': ?string,
* 'ui_launch_mode': ?string,
* 'run_confirmation_modal': bool,
* 'entrypoint_type': string,
* 'constraints': Constraint[],
* 'run': 'null|bool',
* 'default': array{'input': mixed, 'context': array{array{'key': 'int|text', 'value':'int|text'}}}
* }
*/
final readonly class ProcessConfigurationsManager
{
public function __construct(private ProcessConfigurationRegistry $registry)
{
}
/** @return ProcessConfiguration[] */
public function getPublicProcesses(): array
{
return array_filter($this->getConfigurations(), fn (ProcessConfiguration $cfg) => $cfg->isPublic());
}
/** @return ProcessConfiguration[] */
public function getPrivateProcesses(): array
{
return array_filter($this->getConfigurations(), fn (ProcessConfiguration $cfg) => !$cfg->isPublic());
}
/**
* @return UiOptions|null
*/
public function getUiOptions(string $processCode): ?array
{
if (false === $this->registry->hasProcessConfiguration($processCode)) {
return null;
}
$configuration = $this->registry->getProcessConfiguration($processCode);
return $this->resolveUiOptions($configuration->getOptions())['ui'];
}
/**
* @param array<int|string, mixed> $options
*
* @return array{'ui': UiOptions}
*/
private function resolveUiOptions(array $options): array
{
$resolver = new OptionsResolver();
$resolver->setDefault('ui', function (OptionsResolver $uiResolver): void {
$uiResolver->setDefaults(
[
'source' => null,
'target' => null,
'entrypoint_type' => 'text',
'ui_launch_mode' => 'modal',
'constraints' => [],
'run' => null,
'default' => function (OptionsResolver $defaultResolver) {
$defaultResolver->setDefault('input', null);
$defaultResolver->setDefault('context', function (OptionsResolver $contextResolver) {
$contextResolver->setPrototype(true);
$contextResolver->setRequired(['key', 'value']);
});
},
]
);
$uiResolver->setDeprecated(
'run',
'cleverage/ui-process-bundle',
'2',
'run ui option is deprecated. Use public option instead to hide a process from UI'
);
$uiResolver->setAllowedValues('entrypoint_type', ['text', 'file']);
$uiResolver->setNormalizer('constraints', fn (Options $options, array $values): array => (new ConstraintLoader())->buildConstraints($values));
$uiResolver->setAllowedValues('ui_launch_mode', ['modal', null, 'form']);
});
/**
* @var array{'ui': UiOptions} $options
*/
$options = $resolver->resolve($options);
return $options;
}
/** @return ProcessConfiguration[] */
private function getConfigurations(): array
{
return $this->registry->getProcessConfigurations();
}
}