Passed
Push — develop ( 73b673...d6f831 )
by Nikolay
05:32
created

AsteriskConfigClass::getFeatureMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Phalcon\Config;
29
use Phalcon\Di\Injectable;
30
use function MikoPBX\Common\Config\appPath;
31
32
class AsteriskConfigClass extends Injectable implements AsteriskConfigInterface
33
{
34
35
    /**
36
     * Config file name i.e. extensions.conf
37
     */
38
    protected string $description;
39
    private   string $stageMessage = '';
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
     * CoreConfigClass 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
    /**
88
     * Creates array of AsteriskConfObjects
89
     * @return array
90
     */
91
    public static function getAsteriskConfObjects():array
92
    {
93
        $arrObjects = [];
94
        $configsDir = appPath('src/Core/Asterisk/Configs');
95
        $modulesFiles = glob("{$configsDir}/*.php", GLOB_NOSORT);
96
        foreach ($modulesFiles as $file) {
97
            $className        = pathinfo($file)['filename'];
98
            if ($className === 'CoreConfigClass'){
99
                continue;
100
            }
101
            $fullClassName = "\\MikoPBX\\Core\\Asterisk\\Configs\\{$className}";
102
            if (class_exists($fullClassName)) {
103
                $object = new $fullClassName();
104
                if ($object instanceof AsteriskConfigClass){
105
                    $arrObjects[] = $object;
106
                }
107
            }
108
        }
109
        return  $arrObjects;
110
    }
111
112
    /**
113
     * Calls core and enabled additional module method by name and returns plain text result
114
     *
115
     * @param string $methodName
116
     * @param array  $arguments
117
     *
118
     * @return string
119
     */
120
    public function hookModulesMethod(string $methodName, array $arguments = []): string
121
    {
122
        $stringResult      = '';
123
        foreach (self::getAsteriskConfObjects() as $configClassObj) {
124
            if ( ! method_exists($configClassObj, $methodName)) {
125
                continue;
126
            }
127
            if (get_class($configClassObj) === get_class($this)) {
128
                continue; //prevent recursion
129
            }
130
            try {
131
                $includeString = call_user_func_array([$configClassObj, $methodName], $arguments);
132
                if ( ! empty($includeString)) {
133
                    $includeString = $configClassObj->confBlockWithComments($includeString);
134
                    if (
135
                        substr($stringResult, -1) !== "\t"
136
                        &&
137
                        substr($includeString, 0, 4) === 'same'
138
                    ) {
139
                        $stringResult .= "\t" . $includeString;
140
                    } else {
141
                        $stringResult .= $includeString;
142
                    }
143
                }
144
            } catch (\Throwable $e) {
145
                global $errorLogger;
146
                $errorLogger->captureException($e);
147
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
148
                continue;
149
            }
150
        }
151
152
        // HOOK external enabled modules method
153
        $stringResult .= PBXConfModulesProvider::hookModulesMethod($methodName, $arguments);
154
155
        return $stringResult;
156
    }
157
158
159
    /**
160
     * Makes pretty module text block into config file
161
     *
162
     * @param string $addition
163
     *
164
     * @return string
165
     */
166
    protected function confBlockWithComments(string $addition): string
167
    {
168
        if (empty($addition)) {
169
            return '';
170
        }
171
172
        return rtrim($addition) . PHP_EOL;
173
    }
174
175
    /**
176
     * Generates core modules config files with cli messages before and after generation
177
     */
178
    public function generateConfig(): void
179
    {
180
        $this->echoGenerateConfig();
181
        $this->getSettings();
182
        $this->generateConfigProtected();
183
        $this->echoDone();
184
    }
185
186
    /**
187
     * Shows boot message which module was started
188
     */
189
    protected function echoGenerateConfig(): void
190
    {
191
        if ($this->booting === true && ! empty($this->description)) {
192
            $this->stageMessage = "   |- generate config {$this->description}...";
193
            Util::echoWithSyslog($this->stageMessage);
194
        }
195
    }
196
197
    /**
198
     * Prepares settings dataset for a PBX module
199
     */
200
    public function getSettings(): void
201
    {
202
    }
203
204
    /**
205
     * Generates core modules config files
206
     */
207
    protected function generateConfigProtected(): void
208
    {
209
    }
210
211
    /**
212
     * Shows boot message which module generator was finished
213
     */
214
    protected function echoDone(): void
215
    {
216
        if ($this->booting === true && ! empty($this->description)) {
217
            Util::echoResult($this->stageMessage);
218
            $this->stageMessage = '';
219
        }
220
    }
221
222
    /**
223
     * Prepares additional includes for [internal] context section in the extensions.conf file
224
     *
225
     * @return string
226
     */
227
    public function getIncludeInternal(): string
228
    {
229
        return '';
230
    }
231
232
    /**
233
     * Prepares additional rules for [internal] context section in the extensions.conf file
234
     *
235
     * @return string
236
     */
237
    public function extensionGenInternal(): string
238
    {
239
        return '';
240
    }
241
242
    /**
243
     * Prepares additional rules for [internal-users] context section in the extensions.conf file
244
     *
245
     * @return string
246
     */
247
    public function extensionGenInternalUsersPreDial(): string
248
    {
249
        return '';
250
    }
251
252
    /**
253
     * Prepares additional rules for [all_peers] context section in the extensions.conf file
254
     *
255
     * @return string
256
     */
257
    public function extensionGenAllPeersContext(): string
258
    {
259
        return '';
260
    }
261
262
    /**
263
     * Prepares additional includes for [internal-transfer] context section in the extensions.conf file
264
     *
265
     * @return string
266
     */
267
    public function getIncludeInternalTransfer(): string
268
    {
269
        return '';
270
    }
271
272
    /**
273
     * Prepares additional rules for [internal-transfer] context section in the extensions.conf file
274
     *
275
     * @return string
276
     */
277
    public function extensionGenInternalTransfer(): string
278
    {
279
        return '';
280
    }
281
282
    /**
283
     * Prepares additional contexts sections in the extensions.conf file
284
     *
285
     * @return string
286
     */
287
    public function extensionGenContexts(): string
288
    {
289
        return '';
290
    }
291
292
    /**
293
     * Prepares additional hints for [internal-hints] context section in the extensions.conf file
294
     *
295
     * @return string
296
     */
297
    public function extensionGenHints(): string
298
    {
299
        return '';
300
    }
301
302
    /**
303
     * Prepares additional parameters for [globals] section in the extensions.conf file
304
     *
305
     * @return string
306
     */
307
    public function extensionGlobals(): string
308
    {
309
        return '';
310
    }
311
312
    /**
313
     * Prepares additional parameters for [featuremap] section in the features.conf file
314
     *
315
     * @return string returns additional Star codes
316
     */
317
    public function getFeatureMap(): string
318
    {
319
        return '';
320
    }
321
322
    /**
323
     * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file
324
     *
325
     * @return string
326
     */
327
    public function generatePublicContext(): string
328
    {
329
        return '';
330
    }
331
332
    /**
333
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
334
     * extensions.conf file
335
     *
336
     * @param string $rout_number
337
     *
338
     * @return string
339
     */
340
    public function generateIncomingRoutBeforeDialSystem(string $rout_number): string
341
    {
342
        return '';
343
    }
344
345
    /**
346
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
347
     * extensions.conf file
348
     *
349
     * @param string $rout_number
350
     *
351
     * @return string
352
     */
353
    public function generateIncomingRoutBeforeDialPreSystem(string $rout_number): string
354
    {
355
        return '';
356
    }
357
358
    /**
359
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
360
     * extensions.conf file
361
     *
362
     * @param string $rout_number
363
     *
364
     * @return string
365
     */
366
    public function generateIncomingRoutBeforeDial(string $rout_number): string
367
    {
368
        return '';
369
    }
370
371
    /**
372
     * Returns the messages variable
373
     *
374
     * @return array
375
     */
376
    public function getMessages(): array
377
    {
378
        return $this->messages;
379
    }
380
381
    /**
382
     * Returns models list of models which affect the current module settings
383
     *
384
     * @return array
385
     */
386
    public function getDependenceModels(): array
387
    {
388
        return [];
389
    }
390
391
    /**
392
     * Prepares additional parameters for each outgoing route context
393
     * before dial call in the extensions.conf file
394
     *
395
     * @param array $rout
396
     *
397
     * @return string
398
     */
399
    public function generateOutRoutContext(array $rout): string
400
    {
401
        return '';
402
    }
403
404
    /**
405
     * Override pjsip options for provider in the pjsip.conf file
406
     *
407
     * @param string $uniqid  the provider unique identifier
408
     * @param array  $options list of pjsip options
409
     *
410
     * @return array
411
     */
412
    public function overrideProviderPJSIPOptions(string $uniqid, array $options): array
413
    {
414
        return $options;
415
    }
416
417
    /**
418
     * Override pjsip options for peer in the pjsip.conf file
419
     *
420
     * @param string $extension the endpoint extension
421
     * @param array  $options   list of pjsip options
422
     *
423
     * @return array
424
     */
425
    public function overridePJSIPOptions(string $extension, array $options): array
426
    {
427
        return $options;
428
    }
429
430
    /**
431
     * Prepares additional parameters for each outgoing route context
432
     * after dial call in the extensions.conf file
433
     *
434
     * @param array $rout
435
     *
436
     * @return string
437
     */
438
    public function generateOutRoutAfterDialContext(array $rout): string
439
    {
440
        return '';
441
    }
442
443
    /**
444
     * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer
445
     *
446
     * @param array $peer information about peer
447
     *
448
     * @return string
449
     */
450
    public function generatePeerPjAdditionalOptions(array $peer): string
451
    {
452
        return '';
453
    }
454
455
    /**
456
     * Prepares additional AMI users data in the manager.conf file
457
     *
458
     * @return string
459
     */
460
    public function generateManagerConf(): string
461
    {
462
        return '';
463
    }
464
465
    /**
466
     * Prepares additional parameters for each incoming context
467
     * and incoming route after dial command in an extensions.conf file
468
     *
469
     * @param string $uniqId
470
     *
471
     * @return string
472
     */
473
    public function generateIncomingRoutAfterDialContext(string $uniqId): string
474
    {
475
        return '';
476
    }
477
478
    /**
479
     * Prepares additional peers data in the pjsip.conf file
480
     *
481
     * @return string
482
     */
483
    public function generatePeersPj(): string
484
    {
485
        return '';
486
    }
487
488
}