1 | <?php |
||
15 | class SectionParserCollection implements \IteratorAggregate |
||
16 | { |
||
17 | private const |
||
18 | VARIABLES = 'variables', |
||
19 | INCLUDES = 'includes', |
||
20 | EXTERNALS = 'externals', |
||
21 | GROUPS = 'groups'; |
||
22 | |||
23 | private array |
||
|
|||
24 | $parsers; |
||
25 | |||
26 | 280 | public function __construct() |
|
27 | { |
||
28 | 280 | $this->parsers = [ |
|
29 | 280 | self::VARIABLES => new VariableParser(), |
|
30 | 280 | self::INCLUDES => null, |
|
31 | 280 | self::EXTERNALS => null, |
|
32 | 280 | self::GROUPS => null, |
|
33 | ]; |
||
34 | 280 | } |
|
35 | |||
36 | 280 | public function enableIncludeSupport(): void |
|
37 | { |
||
38 | 280 | if($this->parsers[self::INCLUDES] === null) |
|
39 | { |
||
40 | 280 | $this->parsers[self::INCLUDES] = new IncludeParser(); |
|
41 | } |
||
42 | 280 | } |
|
43 | |||
44 | 280 | public function enableExternalSupport(Filesystem $fs): void |
|
45 | { |
||
46 | 280 | if($this->parsers[self::EXTERNALS] === null) |
|
47 | { |
||
48 | 280 | $this->parsers[self::EXTERNALS] = new ExternalParser(new Parser($fs)); |
|
49 | } |
||
50 | 280 | } |
|
51 | |||
52 | 270 | public function enableGroupSupport(): void |
|
53 | { |
||
54 | 270 | if($this->parsers[self::GROUPS] === null) |
|
55 | { |
||
56 | 270 | $this->parsers[self::GROUPS] = new GroupParser(); |
|
57 | } |
||
58 | 270 | } |
|
59 | |||
60 | 250 | public function variables(): VariableParser |
|
61 | { |
||
62 | 250 | return $this->parsers[self::VARIABLES]; |
|
63 | } |
||
64 | |||
65 | 249 | public function includes(): ?IncludeParser |
|
66 | { |
||
67 | 249 | return $this->parsers[self::INCLUDES]; |
|
68 | } |
||
69 | |||
70 | 247 | public function externals(): ?ExternalParser |
|
71 | { |
||
72 | 247 | return $this->parsers[self::EXTERNALS]; |
|
73 | } |
||
74 | |||
75 | 46 | public function groups(): ?GroupParser |
|
76 | { |
||
77 | 46 | return $this->parsers[self::GROUPS]; |
|
78 | } |
||
79 | |||
80 | 273 | public function has(string $sectionName): bool |
|
81 | { |
||
82 | 273 | return isset($this->parsers[$sectionName]); |
|
83 | } |
||
84 | |||
85 | 273 | public function get(string $sectionName): SectionParser |
|
86 | { |
||
87 | 273 | if(! $this->has($sectionName)) |
|
88 | { |
||
89 | 1 | throw new \RuntimeException('Unknown section name ' . $sectionName); |
|
90 | } |
||
91 | |||
92 | 272 | return $this->parsers[$sectionName]; |
|
93 | } |
||
94 | |||
95 | 280 | public function getIterator(): \Iterator |
|
96 | { |
||
97 | 280 | return new \ArrayIterator( |
|
98 | 280 | array_filter($this->parsers) |
|
99 | ); |
||
100 | } |
||
101 | } |
||
102 | |||
103 |