Passed
Push — develop ( e4768a...6d9d8a )
by Nikolay
04:23
created

AsteriskConfigClass::getAsteriskConfObjects()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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