1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Definition; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\ConfigProcessor\ConfigProcessorInterface; |
6
|
|
|
|
7
|
|
|
final class ConfigProcessor implements ConfigProcessorInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ConfigProcessorInterface[] |
11
|
|
|
*/ |
12
|
|
|
private $orderedProcessors; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $processors; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $isInitialized = false; |
23
|
|
|
|
24
|
98 |
|
public function addConfigProcessor(ConfigProcessorInterface $configProcessor, $priority = 0) |
25
|
|
|
{ |
26
|
98 |
|
$this->register($configProcessor, $priority); |
27
|
98 |
|
} |
28
|
|
|
|
29
|
98 |
|
public function register(ConfigProcessorInterface $configProcessor, $priority = 0) |
30
|
|
|
{ |
31
|
98 |
|
if ($this->isInitialized) { |
32
|
1 |
|
throw new \LogicException('Registering config processor after calling process() is not supported.'); |
33
|
|
|
} |
34
|
98 |
|
$this->processors[] = ['processor' => $configProcessor, 'priority' => $priority]; |
35
|
98 |
|
} |
36
|
|
|
|
37
|
97 |
|
public function getOrderedProcessors() |
38
|
|
|
{ |
39
|
97 |
|
$this->initialize(); |
40
|
|
|
|
41
|
97 |
|
return $this->orderedProcessors; |
42
|
|
|
} |
43
|
|
|
|
44
|
97 |
|
public function process(LazyConfig $lazyConfig) |
45
|
|
|
{ |
46
|
97 |
|
foreach ($this->getOrderedProcessors() as $processor) { |
47
|
97 |
|
$lazyConfig = $processor->process($lazyConfig); |
48
|
|
|
} |
49
|
|
|
|
50
|
97 |
|
return $lazyConfig; |
51
|
|
|
} |
52
|
|
|
|
53
|
97 |
|
private function initialize() |
54
|
|
|
{ |
55
|
97 |
|
if (!$this->isInitialized) { |
56
|
|
|
// order processors by DESC priority |
57
|
97 |
|
$processors = $this->processors; |
58
|
|
|
\usort($processors, function ($processorA, $processorB) { |
59
|
96 |
|
if ($processorA['priority'] === $processorB['priority']) { |
60
|
|
|
return 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
96 |
|
return ($processorA['priority'] < $processorB['priority']) ? 1 : -1; |
64
|
97 |
|
}); |
65
|
|
|
|
66
|
97 |
|
$this->orderedProcessors = \array_column($processors, 'processor'); |
67
|
97 |
|
$this->isInitialized = true; |
68
|
|
|
} |
69
|
97 |
|
} |
70
|
|
|
} |
71
|
|
|
|