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