|
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
|
91 |
|
public function addConfigProcessor(ConfigProcessorInterface $configProcessor, $priority = 0) |
|
25
|
|
|
{ |
|
26
|
91 |
|
$this->register($configProcessor, $priority); |
|
27
|
91 |
|
} |
|
28
|
|
|
|
|
29
|
91 |
|
public function register(ConfigProcessorInterface $configProcessor, $priority = 0) |
|
30
|
|
|
{ |
|
31
|
91 |
|
if ($this->isInitialized) { |
|
32
|
1 |
|
throw new \LogicException('Registering config processor after calling process() is not supported.'); |
|
33
|
|
|
} |
|
34
|
91 |
|
$this->processors[] = ['processor' => $configProcessor, 'priority' => $priority]; |
|
35
|
91 |
|
} |
|
36
|
|
|
|
|
37
|
91 |
|
public function getOrderedProcessors() |
|
38
|
|
|
{ |
|
39
|
91 |
|
$this->initialize(); |
|
40
|
|
|
|
|
41
|
91 |
|
return $this->orderedProcessors; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
91 |
|
public function process(LazyConfig $lazyConfig) |
|
45
|
|
|
{ |
|
46
|
91 |
|
foreach ($this->getOrderedProcessors() as $processor) { |
|
47
|
91 |
|
$lazyConfig = $processor->process($lazyConfig); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
91 |
|
return $lazyConfig; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
91 |
|
private function initialize() |
|
54
|
|
|
{ |
|
55
|
91 |
|
if (!$this->isInitialized) { |
|
56
|
|
|
// order processors by DESC priority |
|
57
|
91 |
|
$processors = $this->processors; |
|
58
|
|
|
\usort($processors, function ($processorA, $processorB) { |
|
59
|
90 |
|
if ($processorA['priority'] === $processorB['priority']) { |
|
60
|
|
|
return 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
90 |
|
return ($processorA['priority'] < $processorB['priority']) ? 1 : -1; |
|
64
|
91 |
|
}); |
|
65
|
|
|
|
|
66
|
91 |
|
$this->orderedProcessors = \array_column($processors, 'processor'); |
|
67
|
91 |
|
$this->isInitialized = true; |
|
68
|
|
|
} |
|
69
|
91 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|