|
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\Directive; |
|
13
|
|
|
|
|
14
|
|
|
use WebHelper\Parser\Server\ServerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Describes a block directive instance or a context. |
|
18
|
|
|
* |
|
19
|
|
|
* a BlockDirective is an ordered list of other directives set in a context. |
|
20
|
|
|
* |
|
21
|
|
|
* @author James <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class BlockDirective extends Directive implements DirectiveInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var array orderd list of sub directives */ |
|
26
|
|
|
private $directives = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Adds a Directive at the end of the list. |
|
30
|
|
|
* |
|
31
|
|
|
* @param DirectiveInterface $directive a directive to add |
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function add(DirectiveInterface $directive) |
|
34
|
|
|
{ |
|
35
|
9 |
|
$this->directives[] = $directive; |
|
36
|
|
|
|
|
37
|
9 |
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Confirms if the directive contains a specified directive. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name the directive for which to check existence |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function hasDirective($name) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$inSubDirective = false; |
|
50
|
|
|
|
|
51
|
1 |
|
foreach ($this->directives as $directive) { |
|
52
|
1 |
|
if ($directive->getName() == $name) { |
|
53
|
1 |
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$inSubDirective = $this->hasInnerDirective($name, $inSubDirective, $directive); |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $inSubDirective; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Looks into sub directives to confirm if the actual contains a specified directive. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name the directive for which to check existence |
|
66
|
|
|
* @param bool $inSubDirective the actual state |
|
67
|
|
|
* @param DirectiveInterface $directive the sub directive to look into |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
|
70
|
|
|
*/ |
|
71
|
1 |
|
private function hasInnerDirective($name, $inSubDirective, DirectiveInterface $directive) |
|
72
|
|
|
{ |
|
73
|
1 |
|
if (!$directive->isSimple()) { |
|
74
|
1 |
|
$inSubDirective = $inSubDirective || $directive->hasDirective($name); |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return $inSubDirective; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Confirms if the directive is simple. |
|
82
|
|
|
* |
|
83
|
|
|
* Block directive can have sub directives |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool true if the directive is simple, false otherwise |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function isSimple() |
|
88
|
|
|
{ |
|
89
|
1 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Dumps the directive respecting a server syntax. |
|
94
|
|
|
* |
|
95
|
|
|
* @param ServerInterface $server a server instance |
|
96
|
|
|
* @param int $spaces the indentation spaces |
|
97
|
|
|
* |
|
98
|
|
|
* @return string the dumped directive |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function dump(ServerInterface $server, $spaces = 0) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$config = ''; |
|
103
|
|
|
|
|
104
|
1 |
|
foreach ($this->directives as $directive) { |
|
105
|
1 |
|
$config .= $directive->dump($server, $spaces + ($this->isMainContext() ? 0 : 4)); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $this->dumpBlock($server->getDumperStartDirective(), $spaces). |
|
109
|
1 |
|
$config. |
|
110
|
1 |
|
$this->dumpBlock($server->getDumperEndDirective(), $spaces); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Dumps a starting/ending block directive. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $directiveString the start/end directive string |
|
117
|
|
|
* @param int $spaces the indentation spaces |
|
118
|
|
|
* |
|
119
|
|
|
* @return string the dumped directive |
|
120
|
|
|
*/ |
|
121
|
1 |
|
private function dumpBlock($directiveString, $spaces = 0) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$config = ''; |
|
124
|
1 |
|
$value = $this->getValue() ? ' '.$this->getValue() : ''; |
|
125
|
|
|
|
|
126
|
1 |
|
if (!$this->isMainContext()) { |
|
127
|
1 |
|
$config = str_repeat(' ', $spaces).sprintf( |
|
128
|
1 |
|
$directiveString, |
|
129
|
1 |
|
$this->getName(), |
|
130
|
|
|
$value |
|
131
|
1 |
|
).PHP_EOL; |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
return $config; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Confirms if a Block directive is a 'main' context. |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool true if the name of the block directive is 'main', false otherwise |
|
141
|
|
|
*/ |
|
142
|
1 |
|
private function isMainContext() |
|
143
|
|
|
{ |
|
144
|
1 |
|
return 'main' == $this->getName(); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|