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
|
|
|
namespace WebHelper\Parser\Parser; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Methods that can be applied depending of the kind of server after a parsed configuration file turns into an array. |
15
|
|
|
* |
16
|
|
|
* @author James <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class After |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Trim all blank lines. |
22
|
|
|
* |
23
|
|
|
* @param array $activeConfig config file exploded in an array of lines |
24
|
|
|
* |
25
|
|
|
* @return array an array cleaned of blank lines |
26
|
|
|
*/ |
27
|
14 |
|
public static function deleteBlankLines(array $activeConfig = []) |
28
|
|
|
{ |
29
|
14 |
|
$cleanedActiveConfig = []; |
30
|
|
|
|
31
|
14 |
|
foreach (array_map('trim', $activeConfig) as $line) { |
32
|
14 |
|
if ($line != '') { |
33
|
12 |
|
$cleanedActiveConfig[] = $line; |
34
|
12 |
|
} |
35
|
14 |
|
} |
36
|
|
|
|
37
|
14 |
|
return $cleanedActiveConfig; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Reassembles discontinued simple directives in one line. |
42
|
|
|
* |
43
|
|
|
* In an Apache server context, it may be encountered. |
44
|
|
|
* |
45
|
|
|
* @param array $activeConfig config file exploded in an array of lines |
46
|
|
|
* |
47
|
|
|
* @return array an array with continuing lines gathered as one |
48
|
|
|
*/ |
49
|
8 |
|
public static function continuingDirectives(array $activeConfig = []) |
50
|
|
|
{ |
51
|
8 |
|
$cleanedActiveConfig = []; |
52
|
|
|
|
53
|
|
|
//Continuing directives with "\" at the very end of a line are reassembled |
54
|
8 |
|
foreach ($activeConfig as $line) { |
55
|
8 |
|
if (!self::setLineIfPrevious($line)) { |
56
|
8 |
|
$cleanedActiveConfig[] = $line; |
57
|
8 |
|
} |
58
|
8 |
|
} |
59
|
|
|
|
60
|
8 |
|
return $cleanedActiveConfig; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Helps to gather continuing lines as one. |
65
|
|
|
* |
66
|
|
|
* @param string &$line a line to add to previous lines or matching a contuining end line marker |
67
|
|
|
*/ |
68
|
8 |
|
private static function setLineIfPrevious(&$line) |
69
|
|
|
{ |
70
|
8 |
|
static $previousLine = ''; |
71
|
|
|
|
72
|
8 |
|
if ($previousLine) { |
73
|
1 |
|
$line = $previousLine.' '.trim($line); |
74
|
1 |
|
$previousLine = ''; |
75
|
1 |
|
} |
76
|
|
|
|
77
|
8 |
|
if (preg_match('/(.+)\\\$/', $line, $container)) { |
78
|
1 |
|
$previousLine = $container[1]; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
8 |
|
return $previousLine; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|