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