1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of byrokrat\giroapp. |
5
|
|
|
* |
6
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace byrokrat\giroapp\Xml; |
25
|
|
|
|
26
|
|
|
use byrokrat\giroapp\Filesystem\FileInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Compile XmlMandate objects using a parser and compiler passes |
30
|
|
|
*/ |
31
|
|
|
class XmlMandateCompiler |
32
|
|
|
{ |
33
|
|
|
/** @var XmlMandateParser */ |
34
|
|
|
private $parser; |
35
|
|
|
|
36
|
|
|
/** @var array<CompilerPassInterface> */ |
37
|
|
|
private $compilerPasses = []; |
38
|
|
|
|
39
|
|
|
public function __construct(XmlMandateParser $parser) |
40
|
|
|
{ |
41
|
|
|
$this->parser = $parser; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function addCompilerPass(CompilerPassInterface $compilerPass): void |
45
|
|
|
{ |
46
|
|
|
$this->compilerPasses[] = $compilerPass; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array<XmlMandate> |
51
|
|
|
*/ |
52
|
|
|
public function compileFile(FileInterface $file): array |
53
|
|
|
{ |
54
|
|
|
$xmlObject = XmlObject::fromString($file->getContent()); |
55
|
|
|
|
56
|
|
|
if (!$xmlObject) { |
57
|
|
|
throw new \RuntimeException("Unable to parse XML from file {$file->getFilename()}"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->compileMandates($xmlObject); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array<XmlMandate> |
65
|
|
|
*/ |
66
|
|
|
public function compileMandates(XmlObject $xmlRoot): array |
67
|
|
|
{ |
68
|
|
|
$raw = $this->parser->parseXml($xmlRoot); |
69
|
|
|
$processed = []; |
70
|
|
|
|
71
|
|
|
foreach ($raw as $mandate) { |
72
|
|
|
foreach ($this->compilerPasses as $pass) { |
73
|
|
|
$mandate = $pass->processMandate($mandate); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$processed[] = $mandate; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $processed; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|