1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of WebHelper Parser. |
5
|
|
|
* |
6
|
|
|
* (c) James <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WebHelper\Parser; |
13
|
|
|
|
14
|
|
|
use WebHelper\Parser\Server\ServerInterface; |
15
|
|
|
use WebHelper\Parser\Directive\DirectiveInterface; |
16
|
|
|
use WebHelper\Parser\Directive\InclusionDirective; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Web server configuration generic dumper. |
20
|
|
|
* |
21
|
|
|
* @author James <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Dumper |
24
|
|
|
{ |
25
|
|
|
/** @var Server\ServerInterface a server instance */ |
26
|
|
|
private $server; |
27
|
|
|
|
28
|
|
|
/** @var int number of spaces to indent */ |
29
|
|
|
private $indentation = 4; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Setter for the server instance. |
33
|
|
|
* |
34
|
|
|
* @see Server\ServerInterface Server Documentation |
35
|
|
|
* |
36
|
|
|
* @param Server\ServerInterface $server the server instance |
37
|
|
|
*/ |
38
|
1 |
|
public function setServer(ServerInterface $server) |
39
|
|
|
{ |
40
|
1 |
|
$this->server = $server; |
41
|
|
|
|
42
|
1 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Dumps recursively the active configuration as a file. |
47
|
|
|
* |
48
|
|
|
* @param DirectiveInterface $activeConfig the active configuration to dump |
49
|
|
|
* @param int $spaces the indentation spaces |
50
|
|
|
* |
51
|
|
|
* @return string the file output |
52
|
|
|
*/ |
53
|
1 |
|
public function dump(DirectiveInterface $activeConfig, $spaces = 0) |
54
|
|
|
{ |
55
|
1 |
|
$config = ''; |
56
|
|
|
|
57
|
1 |
|
foreach ($activeConfig as $directive) { |
|
|
|
|
58
|
1 |
|
if ($directive->isSimple() || $directive instanceof InclusionDirective) { |
59
|
1 |
|
$config .= str_repeat(' ', $spaces). |
60
|
1 |
|
sprintf( |
61
|
1 |
|
$this->server->getDumperSimpleDirective(), |
62
|
1 |
|
$directive->getName(), |
63
|
1 |
|
$directive->getValue() |
64
|
1 |
|
).PHP_EOL; |
65
|
1 |
|
} else { |
66
|
1 |
|
$config .= str_repeat(' ', $spaces). |
67
|
1 |
|
sprintf( |
68
|
1 |
|
$this->server->getDumperStartDirective(), |
69
|
1 |
|
$directive->getName(), |
70
|
1 |
|
$directive->getValue() |
71
|
1 |
|
).PHP_EOL; |
72
|
1 |
|
$config .= str_repeat(' ', $spaces).$this->dump($directive, $spaces + $this->indentation); |
73
|
1 |
|
$config .= str_repeat(' ', $spaces). |
74
|
1 |
|
sprintf( |
75
|
1 |
|
$this->server->getDumperEndDirective(), |
76
|
1 |
|
$directive->getName() |
77
|
1 |
|
).PHP_EOL; |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
return $config; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|