forked from tobeorla/Nginx-Config-Processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.php
More file actions
207 lines (175 loc) · 4.64 KB
/
Scope.php
File metadata and controls
207 lines (175 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* This file is part of the Nginx Config Processor package.
*
* (c) Roman Piták <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace RomanPitak\Nginx\Config;
class Scope extends Printable
{
/** @var Directive $parentDirective */
private $parentDirective = null;
/** @var Directive[] $directives */
private $directives = array();
/** @var Printable[] $printables */
private $printables = array();
/**
* Write this Scope into a file.
*
* @param $filePath
* @throws Exception
*/
public function saveToFile($filePath)
{
$handle = @fopen($filePath, 'w');
if (false === $handle)
{
throw new Exception('Cannot open file "' . $filePath . '" for writing.');
}
$bytesWritten = @fwrite($handle, (string)$this);
if (false === $bytesWritten) {
fclose($handle);
throw new Exception('Cannot write into file "' . $filePath . '".');
}
$closed = @fclose($handle);
if (false === $closed) {
throw new Exception('Cannot close file handle for "' . $filePath . '".');
}
}
/*
* ========== Factories ==========
*/
/**
* Provides fluid interface.
*
* @return Scope
*/
public static function create()
{
return new self();
}
/**
* Create new Scope from the configuration string.
*
* @param \RomanPitak\Nginx\Config\String $configString
* @return Scope
* @throws Exception
*/
public static function fromString(String $configString)
{
$scope = new Scope();
while (false === $configString->eof()) {
if (true === $configString->isEmptyLine()) {
$scope->addPrintable(EmptyLine::fromString($configString));
}
$c = $configString->getChar();
if ('#' === $c) {
$scope->addPrintable(Comment::fromString($configString));
continue;
}
if (('a' <= $c) && ('z' >= $c)) {
$scope->addDirective(Directive::fromString($configString));
continue;
}
if ('}' === $configString->getChar()) {
break;
}
$configString->inc();
}
return $scope;
}
/**
* Create new Scope from a file.
*
* @param $filePath
* @return Scope
*/
public static function fromFile($filePath)
{
return self::fromString(new File($filePath));
}
/*
* ========== Getters ==========
*/
/**
* Get parent Directive.
*
* @return Directive|null
*/
public function getParentDirective()
{
return $this->parentDirective;
}
/*
* ========== Setters ==========
*/
/**
* Add a Directive to the list of this Scopes directives
*
* Adds the Directive and sets the Directives parent Scope to $this.
*
* @param Directive $directive
* @return $this
*/
public function addDirective(Directive $directive)
{
if ($directive->getParentScope() !== $this) {
$directive->setParentScope($this);
}
$this->directives[] = $directive;
$this->addPrintable($directive);
return $this;
}
/**
* Add printable element.
*
* @param Printable $printable
*/
private function addPrintable(Printable $printable)
{
$this->printables[] = $printable;
}
/**
* Set parent directive for this Scope.
*
* Sets parent directive for this Scope and also
* sets the $parentDirective->setChildScope($this)
*
* @param Directive $parentDirective
* @return $this
*/
public function setParentDirective(Directive $parentDirective)
{
$this->parentDirective = $parentDirective;
if ($parentDirective->getChildScope() !== $this) {
$parentDirective->setChildScope($this);
}
return $this;
}
/*
* ========== Printable ==========
*/
/**
* Pretty print with indentation.
*
* @param $indentLevel
* @param int $spacesPerIndent
* @return string
*/
public function prettyPrint($indentLevel, $spacesPerIndent = 4)
{
$rs = "";
foreach ($this->printables as $printable) {
$rs .= $printable->prettyPrint($indentLevel + 1, $spacesPerIndent);
}
return $rs;
}
public function __toString()
{
return $this->prettyPrint(-1);
}
}