Passed
Push — develop ( 47a9ce...330ac8 )
by Портнов
04:36
created

CoreConfigClass::generatePeerPjAdditionalOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2021 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\Asterisk\Configs;
21
22
23
use MikoPBX\Common\Providers\ConfigProvider;
24
use MikoPBX\Common\Providers\PBXConfModulesProvider;
25
use MikoPBX\Common\Providers\RegistryProvider;
26
use MikoPBX\Core\System\MikoPBXConfig;
27
use MikoPBX\Core\System\Util;
28
use MikoPBX\Modules\Config\ConfigClass;
29
use Phalcon\Config;
30
use Phalcon\Di\Injectable;
31
use Throwable;
32
33
abstract class CoreConfigClass extends Injectable implements AsteriskConfigInterface
34
{
35
36
    /**
37
     * Config file name i.e. extensions.conf
38
     */
39
    protected string $description;
40
41
    /**
42
     * Easy way to get or set the PbxSettings values
43
     *
44
     * @var \MikoPBX\Core\System\MikoPBXConfig
45
     */
46
    protected MikoPBXConfig $mikoPBXConfig;
47
48
    /**
49
     * Access to the /etc/inc/mikopbx-settings.json values
50
     *
51
     * @var \Phalcon\Config
52
     */
53
    protected Config $config;
54
55
    /**
56
     * Shows if it is boot process now or usual work
57
     *
58
     * @var bool
59
     */
60
    protected bool $booting;
61
62
    /**
63
     * Error and notice messages
64
     *
65
     * @var array
66
     */
67
    protected array $messages;
68
69
    /**
70
     * Array of PbxSettings values
71
     */
72
    protected array $generalSettings;
73
74
    /**
75
     * ConfigClass constructor.
76
     */
77
    public function __construct()
78
    {
79
        $this->config          = $this->getDI()->getShared(ConfigProvider::SERVICE_NAME);
80
        $this->booting         = $this->getDI()->getShared(RegistryProvider::SERVICE_NAME)->booting === true;
81
        $this->mikoPBXConfig   = new MikoPBXConfig();
82
        $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings();
83
        $this->messages        = [];
84
    }
85
86
    /**
87
     * Calls additional module method by name and returns plain text result
88
     *
89
     * @param string $methodName
90
     * @param array  $arguments
91
     *
92
     * @return string
93
     */
94
    public function hookModulesMethod(string $methodName, array $arguments = []): string
95
    {
96
        $stringResult      = '';
97
        $additionalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME);
98
        foreach ($additionalModules as $configClassObj) {
99
            if ( ! method_exists($configClassObj, $methodName)) {
100
                continue;
101
            }
102
            if (get_class($configClassObj) === get_class($this)) {
103
                continue; //prevent recursion
104
            }
105
            try {
106
                $includeString = call_user_func_array([$configClassObj, $methodName], $arguments);
107
                if ( ! empty($includeString)) {
108
                    $includeString = $configClassObj->confBlockWithComments($includeString);
109
                    if (
110
                        substr($stringResult, -1) !== "\t"
111
                        &&
112
                        substr($includeString, 0, 4) === 'same'
113
                    ) {
114
                        $stringResult .= "\t" . $includeString;
115
                    } else {
116
                        $stringResult .= $includeString;
117
                    }
118
                }
119
            } catch (\Throwable $e) {
120
                global $errorLogger;
121
                $errorLogger->captureException($e);
122
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
123
                continue;
124
            }
125
        }
126
127
        return $stringResult;
128
    }
129
130
    /**
131
     * Calls additional module method by name and returns array of results
132
     *
133
     * @param string $methodName
134
     * @param array  $arguments
135
     *
136
     * @return array
137
     */
138
    public function hookModulesMethodWithArrayResult(string $methodName, array $arguments = []): array
139
    {
140
        $result            = [];
141
        $additionalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME);
142
        foreach ($additionalModules as $configClassObj) {
143
            if ( ! method_exists($configClassObj, $methodName)) {
144
                continue;
145
            }
146
            try {
147
                $moduleMethodResponse = call_user_func_array([$configClassObj, $methodName], $arguments);
148
            } catch (Throwable $e) {
149
                global $errorLogger;
150
                $errorLogger->captureException($e);
151
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
152
                continue;
153
            }
154
            if ( ! empty($moduleMethodResponse)) {
155
                if (is_a($configClassObj, ConfigClass::class)) {
156
                    $result[$configClassObj->moduleUniqueId] = $moduleMethodResponse;
157
                } else {
158
                    $result[] = $moduleMethodResponse;
159
                }
160
            }
161
        }
162
163
        return $result;
164
    }
165
166
    /**
167
     * Makes pretty module text block into config file
168
     *
169
     * @param string $addition
170
     *
171
     * @return string
172
     */
173
    protected function confBlockWithComments(string $addition): string
174
    {
175
        if (empty($addition)) {
176
            return '';
177
        }
178
179
        return rtrim($addition) . PHP_EOL;
180
    }
181
182
    /**
183
     * Generates core modules config files with cli messages before and after generation
184
     */
185
    public function generateConfig(): void
186
    {
187
        $this->echoGenerateConfig();
188
        $this->getSettings();
189
        $this->generateConfigProtected();
190
        $this->echoDone();
191
    }
192
193
    /**
194
     * Shows boot message which module was started
195
     */
196
    protected function echoGenerateConfig(): void
197
    {
198
        if ($this->booting === true && ! empty($this->description)) {
199
            echo "   |- generate config {$this->description}... ";
200
        }
201
    }
202
203
    /**
204
     * Prepares settings dataset for a PBX module
205
     */
206
    public function getSettings(): void
207
    {
208
    }
209
210
    /**
211
     * Generates core modules config files
212
     */
213
    protected function generateConfigProtected(): void
214
    {
215
    }
216
217
    /**
218
     * Shows boot message which module generator was finished
219
     */
220
    protected function echoDone(): void
221
    {
222
        if ($this->booting === true && ! empty($this->description)) {
223
            echo "\033[32;1mdone\033[0m \n";
224
        }
225
    }
226
227
    /**
228
     * Prepares additional includes for [internal] context section in the extensions.conf file
229
     *
230
     * @return string
231
     */
232
    public function getIncludeInternal(): string
233
    {
234
        return '';
235
    }
236
237
    /**
238
     * Prepares additional rules for [internal] context section in the extensions.conf file
239
     *
240
     * @return string
241
     */
242
    public function extensionGenInternal(): string
243
    {
244
        return '';
245
    }
246
247
    /**
248
     * Prepares additional rules for [internal-users] context section in the extensions.conf file
249
     *
250
     * @return string
251
     */
252
    public function extensionGenInternalUsersPreDial(): string
253
    {
254
        return '';
255
    }
256
257
    /**
258
     * Prepares additional includes for [internal-transfer] context section in the extensions.conf file
259
     *
260
     * @return string
261
     */
262
    public function getIncludeInternalTransfer(): string
263
    {
264
        return '';
265
    }
266
267
    /**
268
     * Prepares additional rules for [internal-transfer] context section in the extensions.conf file
269
     *
270
     * @return string
271
     */
272
    public function extensionGenInternalTransfer(): string
273
    {
274
        return '';
275
    }
276
277
    /**
278
     * Prepares additional contexts sections in the extensions.conf file
279
     *
280
     * @return string
281
     */
282
    public function extensionGenContexts(): string
283
    {
284
        return '';
285
    }
286
287
    /**
288
     * Prepares additional hints for [internal-hints] context section in the extensions.conf file
289
     *
290
     * @return string
291
     */
292
    public function extensionGenHints(): string
293
    {
294
        return '';
295
    }
296
297
    /**
298
     * Prepares additional parameters for [globals] section in the extensions.conf file
299
     *
300
     * @return string
301
     */
302
    public function extensionGlobals(): string
303
    {
304
        return '';
305
    }
306
307
    /**
308
     * Prepares additional parameters for [featuremap] section in the features.conf file
309
     *
310
     * @return string returns additional Star codes
311
     */
312
    public function getFeatureMap(): string
313
    {
314
        return '';
315
    }
316
317
    /**
318
     * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file
319
     *
320
     * @return string
321
     */
322
    public function generatePublicContext(): string
323
    {
324
        return '';
325
    }
326
327
    /**
328
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
329
     * extensions.conf file
330
     *
331
     * @param string $rout_number
332
     *
333
     * @return string
334
     */
335
    public function generateIncomingRoutBeforeDial(string $rout_number): string
336
    {
337
        return '';
338
    }
339
340
    /**
341
     * Returns the messages variable
342
     *
343
     * @return array
344
     */
345
    public function getMessages(): array
346
    {
347
        return $this->messages;
348
    }
349
350
    /**
351
     * Returns models list of models which affect the current module settings
352
     *
353
     * @return array
354
     */
355
    public function getDependenceModels(): array
356
    {
357
        return [];
358
    }
359
360
    /**
361
     * Prepares additional parameters for each outgoing route context
362
     * before dial call in the extensions.conf file
363
     *
364
     * @param array $rout
365
     *
366
     * @return string
367
     */
368
    public function generateOutRoutContext(array $rout): string
369
    {
370
        return '';
371
    }
372
373
    /**
374
     * Override pjsip options for provider in the pjsip.conf file
375
     *
376
     * @param string $uniqid  the provider unique identifier
377
     * @param array  $options list of pjsip options
378
     *
379
     * @return array
380
     */
381
    public function overrideProviderPJSIPOptions(string $uniqid, array $options): array
382
    {
383
        return $options;
384
    }
385
386
    /**
387
     * Override pjsip options for peer in the pjsip.conf file
388
     *
389
     * @param string $extension the endpoint extension
390
     * @param array  $options   list of pjsip options
391
     *
392
     * @return array
393
     */
394
    public function overridePJSIPOptions(string $extension, array $options): array
395
    {
396
        return $options;
397
    }
398
399
    /**
400
     * Prepares additional parameters for each outgoing route context
401
     * after dial call in the extensions.conf file
402
     *
403
     * @param array $rout
404
     *
405
     * @return string
406
     */
407
    public function generateOutRoutAfterDialContext(array $rout): string
408
    {
409
        return '';
410
    }
411
412
    /**
413
     * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer
414
     *
415
     * @param array $peer information about peer
416
     *
417
     * @return string
418
     */
419
    public function generatePeerPjAdditionalOptions(array $peer): string
420
    {
421
        return '';
422
    }
423
424
    /**
425
     * Prepares additional AMI users data in the manager.conf file
426
     *
427
     * @return string
428
     */
429
    public function generateManagerConf(): string
430
    {
431
        return '';
432
    }
433
434
    /**
435
     * Prepares additional parameters for each incoming context
436
     * and incoming route after dial command in an extensions.conf file
437
     *
438
     * @param string $uniqId
439
     *
440
     * @return string
441
     */
442
    public function generateIncomingRoutAfterDialContext(string $uniqId): string
443
    {
444
        return '';
445
    }
446
447
    /**
448
     * Prepares additional peers data in the pjsip.conf file
449
     *
450
     * @return string
451
     */
452
    public function generatePeersPj(): string
453
    {
454
        return '';
455
    }
456
457
}