1 | <?php |
||
17 | class Config implements ConfigInterface |
||
18 | { |
||
19 | /** |
||
20 | * By default legacy login on RouterOS pre-6.43 is not supported |
||
21 | */ |
||
22 | public const LEGACY = false; |
||
23 | |||
24 | /** |
||
25 | * Default port number |
||
26 | */ |
||
27 | public const PORT = 8728; |
||
28 | |||
29 | /** |
||
30 | * Default ssl port number |
||
31 | */ |
||
32 | public const PORT_SSL = 8729; |
||
33 | |||
34 | /** |
||
35 | * Do not use SSL by default |
||
36 | */ |
||
37 | public const SSL = false; |
||
38 | |||
39 | /** |
||
40 | * Max timeout for answer from router |
||
41 | */ |
||
42 | public const TIMEOUT = 10; |
||
43 | |||
44 | /** |
||
45 | * Count of reconnect attempts |
||
46 | */ |
||
47 | public const ATTEMPTS = 10; |
||
48 | |||
49 | /** |
||
50 | * Delay between attempts in seconds |
||
51 | */ |
||
52 | public const ATTEMPTS_DELAY = 1; |
||
53 | |||
54 | /** |
||
55 | * Delay between attempts in seconds |
||
56 | */ |
||
57 | public const SSH_PORT = 22; |
||
58 | |||
59 | /** |
||
60 | * List of allowed parameters of config |
||
61 | */ |
||
62 | public const ALLOWED = [ |
||
63 | 'host' => 'string', // Address of Mikrotik RouterOS |
||
64 | 'user' => 'string', // Username |
||
65 | 'pass' => 'string', // Password |
||
66 | 'port' => 'integer', // RouterOS API port number for access (if not set use default or default with SSL if SSL enabled) |
||
67 | 'ssl' => 'boolean', // Enable ssl support (if port is not set this parameter must change default port to ssl port) |
||
68 | 'legacy' => 'boolean', // Support of legacy login scheme (true - pre 6.43, false - post 6.43) |
||
69 | 'timeout' => 'integer', // Max timeout for answer from RouterOS |
||
70 | 'attempts' => 'integer', // Count of attempts to establish TCP session |
||
71 | 'delay' => 'integer', // Delay between attempts in seconds |
||
72 | 'ssh_port' => 'integer', // Number of SSH port |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Array of parameters (with some default values) |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | private $_parameters = [ |
||
81 | 'legacy' => self::LEGACY, |
||
82 | 'ssl' => self::SSL, |
||
83 | 'timeout' => self::TIMEOUT, |
||
84 | 'attempts' => self::ATTEMPTS, |
||
85 | 'delay' => self::ATTEMPTS_DELAY, |
||
86 | 'ssh_port' => self::SSH_PORT, |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * Config constructor. |
||
91 | * |
||
92 | * @param array $parameters List of parameters which can be set on object creation stage |
||
93 | * |
||
94 | * @throws \RouterOS\Exceptions\ConfigException |
||
95 | * @since 0.6 |
||
96 | */ |
||
97 | 9 | public function __construct(array $parameters = []) |
|
103 | |||
104 | /** |
||
105 | * @inheritDoc |
||
106 | * |
||
107 | * @throws \RouterOS\Exceptions\ConfigException when name of configuration key is invalid or not allowed |
||
108 | */ |
||
109 | 9 | public function set(string $name, $value): ConfigInterface |
|
126 | |||
127 | /** |
||
128 | * Return port number (get from defaults if port is not set by user) |
||
129 | * |
||
130 | * @param string $parameter |
||
131 | * |
||
132 | * @return bool|int |
||
133 | */ |
||
134 | 8 | private function getPort(string $parameter) |
|
145 | |||
146 | /** |
||
147 | * @inheritDoc |
||
148 | * |
||
149 | * @throws \RouterOS\Exceptions\ConfigException when parameter is not allowed |
||
150 | */ |
||
151 | public function delete(string $name): ConfigInterface |
||
163 | |||
164 | /** |
||
165 | * @inheritDoc |
||
166 | * |
||
167 | * @throws \RouterOS\Exceptions\ConfigException when parameter is not allowed |
||
168 | */ |
||
169 | 8 | public function get(string $name) |
|
178 | |||
179 | /** |
||
180 | * @inheritDoc |
||
181 | */ |
||
182 | 9 | public function getParameters(): array |
|
186 | } |
||
187 |