1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XL2TP; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use XL2TP\Interfaces\ConfigInterface; |
8
|
|
|
use XL2TP\Interfaces\GeneratorInterface; |
9
|
|
|
use XL2TP\Interfaces\SectionInterface; |
10
|
|
|
use XL2TP\Interfaces\Sections\GlobalInterface; |
11
|
|
|
use XL2TP\Interfaces\Sections\LacInterface; |
12
|
|
|
use XL2TP\Interfaces\Sections\LnsInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Config |
16
|
|
|
* |
17
|
|
|
* @property GlobalInterface $global |
18
|
|
|
* @property LnsInterface $lns |
19
|
|
|
* @property LacInterface $lac |
20
|
|
|
* @method GlobalInterface global() |
21
|
|
|
* @method LnsInterface lns(string $suffix = null) |
22
|
|
|
* @method LacInterface lac(string $suffix = null) |
23
|
|
|
* |
24
|
|
|
* @package XL2TP |
25
|
|
|
*/ |
26
|
|
|
class Config implements ConfigInterface, GeneratorInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* List of preconfigured sections |
30
|
|
|
* |
31
|
|
|
* @var array<string, SectionInterface> |
32
|
|
|
*/ |
33
|
|
|
public $sections = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get section of configuration by provided name |
37
|
|
|
* |
38
|
|
|
* @param string $section Name of section |
39
|
|
|
* @param string|null $suffix Additional suffix of section name |
40
|
|
|
* |
41
|
|
|
* @return SectionInterface |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
5 |
|
public function section(string $section, string $suffix = null): SectionInterface |
45
|
|
|
{ |
46
|
5 |
|
if (empty($suffix) && mb_strtolower(trim($section)) !== 'global') { |
47
|
4 |
|
$suffix = 'default'; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
$hash = md5($section . $suffix); |
51
|
5 |
|
if (!isset($this->sections[$hash]) || !$this->sections[$hash] instanceof SectionInterface) { |
52
|
5 |
|
$this->sections[$hash] = new Section($section, $suffix); |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
return $this->sections[$hash]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* If required section is set |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
1 |
|
public function __isset(string $name): bool |
66
|
|
|
{ |
67
|
1 |
|
$nameWithSpaces = Helpers::decamelize($name); |
68
|
1 |
|
$words = explode(' ', $nameWithSpaces); |
69
|
1 |
|
$section = $words[0]; |
70
|
1 |
|
$suffix = $words[1] ?? null; |
71
|
|
|
|
72
|
1 |
|
$hash = md5($section . $suffix); |
73
|
|
|
|
74
|
1 |
|
return isset($this->sections[$hash]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Bad method call, not allowed here |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @param string $value |
82
|
|
|
*/ |
83
|
1 |
|
public function __set(string $name, string $value) |
84
|
|
|
{ |
85
|
1 |
|
throw new BadMethodCallException('Provided method is not allowed'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get section by name |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* |
93
|
|
|
* @return SectionInterface |
94
|
|
|
* @throws InvalidArgumentException |
95
|
|
|
*/ |
96
|
5 |
|
public function __get(string $name) |
97
|
|
|
{ |
98
|
5 |
|
$nameWithSpaces = Helpers::decamelize($name); |
99
|
5 |
|
$words = explode(' ', $nameWithSpaces); |
100
|
5 |
|
$section = $words[0]; |
101
|
5 |
|
$suffix = $words[1] ?? null; |
102
|
|
|
|
103
|
5 |
|
if (!array_key_exists($section, Section::RELATIONS)) { |
104
|
1 |
|
throw new InvalidArgumentException('Required section "' . $section . '" is not allowed'); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
return $this->section($section, $suffix); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get section by name |
112
|
|
|
* |
113
|
|
|
* @param string $section |
114
|
|
|
* @param array $arguments |
115
|
|
|
* |
116
|
|
|
* @return SectionInterface |
117
|
|
|
*/ |
118
|
1 |
|
public function __call(string $section, array $arguments) |
119
|
|
|
{ |
120
|
1 |
|
$suffix = $arguments[0] ?? null; |
121
|
|
|
|
122
|
1 |
|
if (!array_key_exists($section, Section::RELATIONS)) { |
123
|
1 |
|
throw new InvalidArgumentException('Required section "' . $section . '" is not allowed'); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $this->section($section, $suffix); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Generate L2TP configuration by parameters from memory |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
1 |
|
public function generate(): string |
135
|
|
|
{ |
136
|
1 |
|
$generator = new Generator($this); |
137
|
1 |
|
return $generator->generate(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|