This repository has been archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Directive.php
447 lines (408 loc) · 11.3 KB
/
Directive.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
namespace Nemiro\Apache
{
/*
* Copyright © Aleksey Nemiro, 2015. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Represents Apache section or directive.
*
* @author Aleksey Nemiro <[email protected]>
* @copyright © Aleksey Nemiro, 2015. All rights reserved.
*/
class Directive implements \ArrayAccess
{
/**
* The name of section or directive.
*
* @var \string
*/
public $Name;
/**
* The element parameters.
*
* @var \string[]
*/
public $Parameters;
/**
* The list of directives.
*
* @var DirectiveCollection
*/
public $Directives;
/**
* Initializes a new instance of the class with the specified parameters.
*
* @param \string $name The element name.
* @param \string|\string[] $parameters The list of parameters. Default: NULL.
* @param DirectiveCollection $childs The collection of children. Default: NULL.
*/
function __construct($name, $parameters = NULL, $childs = NULL)
{
if (is_null($name) || (gettype($name) != 'string' && gettype($name) != 'integer') || (string)$name == '')
{
throw new \InvalidArgumentException('Name is required. The name must be a string. Value can not be null or empty.');
}
if (!is_null($childs) && gettype($childs) != 'object' && get_class($childs) != 'Nemiro\Apache\DirectiveCollection')
{
throw new \InvalidArgumentException('The parameter $childs expected values of the Nemiro\Apache\DirectiveCollection type.');
}
$this->Name = $name;
$this->Directives = ($childs != NULL ? $childs : new DirectiveCollection());
if (!is_null($parameters))
{
$this->Parameters = array();
if (is_array($parameters))
{
$this->Parameters = $parameters;
}
else
{
$this->Parameters[] = $parameters;
}
}
}
/**
* Returns a string of parameters separated by a space.
*
* @return \null|\string
*/
public function ParametersAsString()
{
if (isset($this->Parameters) && count($this->Parameters) > 0)
{
// return implode(' ', $this->Parameters);
$result = '';
foreach ($this->Parameters as $parameter)
{
if ($parameter == '') continue;
if (preg_match('/[\s\<\>\#]+/', $parameter) === 1)
{
$parameter = '"'.$parameter.'"';
}
if ($result != '') $result .= ' ';
$result .= $parameter;
}
return $result;
}
else
{
return NULL;
}
}
/**
* Adds new parameter to the parameter collection of the current instance.
*
* @param \object|\array $value The value to add.
* @return void
*/
public function AddParameter($value)
{
if (!isset($this->Parameters) || $this->Parameters == NULL)
{
$this->Parameters = array();
}
if (is_array($value))
{
array_merge($this->Parameters, $value);
}
else
{
$this->Parameters[] = $value;
}
}
/**
* Adds a new directive or section to the collection of the current instance.
*
* @param \string|Directive|DirectiveGroup $nameOrInstance The directive name or an instance of the Directive or DirectiveGroup.
* @param \string|\string[] $parameters The list of parameters. If it the $nameOrInstance specified instance, and this parameter is not NULL, it will use this value.
* @param DirectiveCollection $childs The collection of children. If it the $nameOrInstance specified instance, and this parameter is not NULL, it will use this value.
* @return Directive|DirectiveGroup
*/
public function AddDirective($nameOrInstance, $parameters = NULL, $childs = NULL)
{
if (!is_null($childs) && gettype($childs) != 'object' && get_class($childs) != 'Nemiro\Apache\DirectiveCollection')
{
throw new \InvalidArgumentException('The parameter $childs expected values of the Nemiro\Apache\DirectiveCollection type.');
}
$is_group = FALSE;
if (gettype($nameOrInstance) == 'object' && (($class = get_class($nameOrInstance)) == 'Nemiro\Apache\Directive' || $class == 'Nemiro\Apache\DirectiveGroup'))
{
// is directive instance
if ($parameters == NULL)
{
$parameters = $nameOrInstance->Parameters;
}
if ($childs == NULL)
{
$childs = $nameOrInstance->Directives;
}
$is_group = $nameOrInstance->IsGroup();
$nameOrInstance = $nameOrInstance->Name;
}
if ($this->ContainsChild($nameOrInstance))
{
// is not unique derictive name and not group, transform to group
if (!$this->Directives[$nameOrInstance]->IsGroup())
{
$group = new DirectiveGroup($nameOrInstance);
$group->AddDirective($this->Directives[$nameOrInstance]);
$group->AddDirective(new Directive($nameOrInstance, $parameters, $childs));
$this->Directives[$nameOrInstance] = $group;
}
else
{
// is group
$this->Directives[$nameOrInstance]->Directives->Add(new Directive($this->Directives[$nameOrInstance]->ChildCount(), $parameters, $childs));
}
// return group
return $this->Directives[$nameOrInstance];
}
else
{
// new derictive
if ($is_group)
{
$g = $this->Directives->Add(new DirectiveGroup($nameOrInstance));
$g->Directives = $childs;
}
else
{
$this->Directives->Add(new Directive($nameOrInstance, $parameters, $childs));
}
// return added directive
return $this->LastChild();
}
}
/**
* Determines the group or single section or directive.
*
* @return \bool
*/
public function IsGroup()
{
return get_class($this) == 'Nemiro\Apache\DirectiveGroup';
}
/**
* Determines the section or not.
*
* Sections contain many nested sections and directives.
*
* @return \bool
*/
public function IsSection()
{
return !$this->IsDirective();
}
/**
* Determines the directive or not.
*
* Directives contain only parameters.
*
* @return \bool|\NULL
*/
public function IsDirective()
{
if ($this->IsGroup())
{
if ($this->HasChild())
{
return $this->FirstChild()->IsDirective();
}
else
{
return NULL;
}
}
else
{
return !$this->HasChild();
}
}
/**
* Returns true, if the directive has parameters. Otherwise, it returns false.
*
* @return \bool
*/
public function HasParameters()
{
return isset($this->Parameters) && count($this->Parameters) > 0;
}
/**
* Returns parameters count.
*
* @return \int
*/
public function ParametersCount()
{
return isset($this->Parameters) ? count($this->Parameters) : 0;
}
/**
* Returns true, if the directive has child elements. Otherwise, it returns false.
*
* @return \bool
*/
public function HasChild()
{
return $this->Directives->Count() > 0;
}
/**
* Returns child count.
*
* @return \int
*/
public function ChildCount()
{
return $this->Directives->Count();
}
/**
* Returns the first child of the current instance.
*
* @return Directive|DirectiveGroup
*/
public function FirstChild()
{
return $this->Directives->First();
}
/**
* Returns the last child of the current instance.
*
* @return Directive|DirectiveGroup
*/
public function LastChild()
{
return $this->Directives->Last();
}
/**
* Determines whether the child collection contains the element with specified name.
*
* @param \string $name The key to locate in the DirectiveCollection.
* @return \bool
*/
public function ContainsChild($name)
{
return $this->Directives->ContainsDirective($name);
}
/**
* Returns an array of directives.
*
* @return Directive[]
*/
public function ToArray()
{
if ($this->IsGroup())
{
return $this->Directives->Items;
}
else
{
return array($this);
}
}
/**
* Adds the current instance to separated instance of Directive.
*
* @param Directive|DirectiveGroup $parent The Directive instance in which you want to add the current instance.
* @return Directive|DirectiveGroup|\NULL
*/
public function AddTo($parent)
{
if (is_null($parent))
{
throw new \InvalidArgumentException('Parent can not be null.');
}
return $parent->AddDirective($this);
}
#region ArrayAccess Members
/**
* Whether a offset exists
* Whether or not an offset exists.
*
* @param \string|\int $offset An offset to check for.
* @return \bool
*/
function offsetExists($offset)
{
if (is_int($offset))
{
return isset($this->Parameters[$offset]);
}
else
{
return isset($this->Directives[$offset]);
}
}
/**
* Offset to retrieve
* Returns the value at specified offset.
*
* @param mixed $offset The offset to retrieve.
* @return Directive|DirectiveGroup
*/
function offsetGet($offset)
{
if (is_int($offset))
{
if ($this->IsGroup())
{
return isset($this->Directives[$offset]) ? $this->Directives[$offset] : NULL;
}
else
{
return isset($this->Parameters[$offset]) ? $this->Parameters[$offset] : NULL;
}
}
else
{
return isset($this->Directives[$offset]) ? $this->Directives[$offset] : NULL;
}
}
/**
* Offset to set
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
* @return void
*/
function offsetSet($offset, $value)
{
if (is_null($offset))
{
throw new \ErrorException('Unable to determine the type of value. Use an explicit assignment of values through the properties.');
}
else if (is_int($offset))
{
$this->Parameters[$offset] = $value;
}
else
{
$this->Directives[$offset] = $value;
}
}
/**
* Offset to unset
* Unsets an offset.
*
* @param mixed $offset The offset to unset.
* @return void
*/
function offsetUnset($offset)
{
unset($this->Directives[$offset]);
}
#endregion
}
}