1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\HaPHProxy; |
4
|
|
|
|
5
|
|
|
use Jalle19\HaPHProxy\Parameter\Parameter; |
6
|
|
|
use Jalle19\HaPHProxy\Section\NamedSection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Writer |
10
|
|
|
* @package Jalle19\HaPHProxy |
11
|
|
|
* @author Sam Stenvall <[email protected]> |
12
|
|
|
* @license GNU General Public License 2.0+ |
13
|
|
|
*/ |
14
|
|
|
class Writer |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
const DEFAULT_PREFACE = '# Generated with Jalle19/haphproxy'; |
18
|
|
|
const DEFAULT_INDENT = ' '; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Configuration |
22
|
|
|
*/ |
23
|
|
|
private $configuration; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $preface = self::DEFAULT_PREFACE; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $indent = self::DEFAULT_INDENT; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Writer constructor. |
38
|
|
|
* |
39
|
|
|
* @param Configuration $configuration |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Configuration $configuration) |
42
|
|
|
{ |
43
|
|
|
$this->configuration = $configuration; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Configuration $configuration |
49
|
|
|
*/ |
50
|
|
|
public function setConfiguration($configuration) |
51
|
|
|
{ |
52
|
|
|
$this->configuration = $configuration; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $preface |
58
|
|
|
*/ |
59
|
|
|
public function setPreface($preface) |
60
|
|
|
{ |
61
|
|
|
$this->preface = $preface; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $indent |
67
|
|
|
*/ |
68
|
|
|
public function setIndent($indent) |
69
|
|
|
{ |
70
|
|
|
$this->indent = $indent; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string the configuration as a string |
76
|
|
|
*/ |
77
|
|
|
public function dump() |
78
|
|
|
{ |
79
|
|
|
$configuration = $this->preface . PHP_EOL; |
80
|
|
|
|
81
|
|
|
foreach ($this->configuration->getSections() as $section) { |
82
|
|
|
$configuration .= $section->getType(); |
83
|
|
|
|
84
|
|
|
if ($section instanceof NamedSection) { |
85
|
|
|
$configuration .= ' ' . $section->getName(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$configuration .= PHP_EOL; |
89
|
|
|
|
90
|
|
|
foreach ($section->getParameters() as $parameter) { |
91
|
|
|
$configuration .= $this->indent . $this->writeParameter($parameter) . PHP_EOL; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$configuration .= PHP_EOL; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $configuration; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Parameter $parameter |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
private function writeParameter(Parameter $parameter) |
107
|
|
|
{ |
108
|
|
|
$value = $parameter->getValue(); |
109
|
|
|
|
110
|
|
|
if ($value === null) { |
111
|
|
|
return $parameter->getName(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $parameter->getName() . ' ' . $parameter->getValue(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|