Passed
Push — develop ( 9da61d...f1dabd )
by Портнов
04:58
created

extensionGenCreateChannelDialplan()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 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 MikoPBX\Modules\Config\SystemConfigInterface;
30
use Phalcon\Config;
31
use Phalcon\Di\Injectable;
32
33
/**
34
 * Base class for AsteriskConfig children
35
 *
36
 * @package MikoPBX\Core\Asterisk\Configs
37
 */
38
class AsteriskConfigClass extends Injectable implements AsteriskConfigInterface
39
{
40
    // The module hook applying priority
41
    protected int $priority = 1000;
42
43
    // Config file name i.e. extensions.conf
44
    protected string $description;
45
    private string $stageMessage = '';
46
47
    /**
48
     * Easy way to get or set the PbxSettings values
49
     *
50
     * @var \MikoPBX\Core\System\MikoPBXConfig
51
     */
52
    protected MikoPBXConfig $mikoPBXConfig;
53
54
    /**
55
     * Access to the /etc/inc/mikopbx-settings.json values
56
     *
57
     * @var \Phalcon\Config
58
     */
59
    protected Config $config;
60
61
    /**
62
     * Shows if it is boot process now or usual work
63
     *
64
     * @var bool
65
     */
66
    protected bool $booting;
67
68
    /**
69
     * Error and notice messages
70
     *
71
     * @var array
72
     */
73
    protected array $messages;
74
75
    /**
76
     * Array of PbxSettings values
77
     */
78
    protected array $generalSettings;
79
80
    /**
81
     * AsteriskConfigClass constructor.
82
     */
83
    public function __construct()
84
    {
85
        $this->config = $this->getDI()->getShared(ConfigProvider::SERVICE_NAME);
86
        $this->booting = $this->getDI()->getShared(RegistryProvider::SERVICE_NAME)->booting === true;
87
        $this->mikoPBXConfig = new MikoPBXConfig();
88
        $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings();
89
        $this->messages = [];
90
    }
91
92
    /**
93
     * Calls the specified method on each module object and returns the concatenated results as a string.
94
     *
95
     * @param string $methodName The name of the method to call.
96
     * @param array $arguments The arguments to pass to the method.
97
     *
98
     * @return string The concatenated results as a string.
99
     */
100
    public function hookModulesMethod(string $methodName, array $arguments = []): string
101
    {
102
        $stringResult = '';
103
        $internalModules = $this->di->getShared(AsteriskConfModulesProvider::SERVICE_NAME);
104
        $externalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME);
105
        $arrObjects = array_merge($internalModules, $externalModules);
106
107
        // Sort the merged array based on the priority value
108
        usort($arrObjects, function ($a, $b) use ($methodName) {
109
            return $a->getMethodPriority($methodName) - $b->getMethodPriority($methodName);
110
        });
111
112
        foreach ($arrObjects as $configClassObj) {
113
            if (!method_exists($configClassObj, $methodName)) {
114
                continue;
115
            }
116
            if (get_class($configClassObj) === get_class($this)) {
117
                continue; //prevent recursion
118
            }
119
            try {
120
                $includeString = call_user_func_array([$configClassObj, $methodName], $arguments);
121
                if (!empty($includeString)) {
122
                    if (property_exists($configClassObj, 'moduleUniqueId')) {
123
                        $includeString = $this->confBlockWithComments($includeString, $configClassObj->moduleUniqueId);
124
                    } else {
125
                        $includeString = $this->confBlockWithComments($includeString);
126
                    }
127
                    if (
128
                        substr($stringResult, -1) !== "\t"
129
                        &&
130
                        substr($includeString, 0, 4) === 'same'
131
                    ) {
132
                        $stringResult .= "\t" . $includeString;
133
                    } else {
134
                        $stringResult .= $includeString;
135
                    }
136
                }
137
            } catch (\Throwable $e) {
138
                global $errorLogger;
139
                $errorLogger->captureException($e);
140
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
141
                continue;
142
            }
143
        }
144
        return $stringResult;
145
    }
146
147
    /**
148
     * Adds comments and separators around the provided addition to create a configuration block.
149
     *
150
     * @param string $addition The content to add within the block.
151
     * @param string $externalModuleUniqueId The unique identifier of the external module (optional).
152
     *
153
     * @return string The content wrapped in a configuration block with comments.
154
     */
155
    protected function confBlockWithComments(string $addition, string $externalModuleUniqueId = ''): string
156
    {
157
        if (empty($externalModuleUniqueId)) {
158
            return rtrim($addition) . PHP_EOL;
159
        }
160
        $result = PHP_EOL . '; ***** BEGIN BY ' . $externalModuleUniqueId . PHP_EOL;
161
        $result .= rtrim($addition);
162
        $result .= PHP_EOL . '; ***** END BY ' . $externalModuleUniqueId . PHP_EOL;
163
        return $result;
164
    }
165
166
    /**
167
     * Generates core modules config files with cli messages before and after generation
168
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateconfig
169
     *
170
     * @return void
171
     */
172
    public function generateConfig(): void
173
    {
174
        $this->echoGenerateConfig(); // Display "Generating configuration" message
175
        $this->getSettings(); // Retrieve the required settings
176
        $this->generateConfigProtected(); // Generate the protected configuration content
177
        $this->echoDone(); // Display "Configuration generated successfully" message
178
    }
179
180
    /**
181
     * Displays a message indicating the start of the configuration generation process.
182
     */
183
    protected function echoGenerateConfig(): void
184
    {
185
        if ($this->booting === true && !empty($this->description)) {
186
            $this->stageMessage = "   |- generate config {$this->description}...";
187
            Util::echoWithSyslog($this->stageMessage);  // Output the message and log it in syslog
188
        }
189
    }
190
191
    /**
192
     * Prepares settings dataset for a PBX module
193
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other
194
     *
195
     * @return void
196
     */
197
    public function getSettings(): void
198
    {
199
    }
200
201
    /**
202
     * Generates core modules config files
203
     */
204
    protected function generateConfigProtected(): void
205
    {
206
    }
207
208
    /**
209
     * Displays a message indicating the successful completion of the configuration generation process.
210
     */
211
    protected function echoDone(): void
212
    {
213
        if ($this->booting === true && !empty($this->description)) {
214
            Util::echoResult($this->stageMessage); // Output the completion message
215
            $this->stageMessage = '';
216
        }
217
    }
218
219
    /**
220
     * Prepares additional includes for [internal] context section in the extensions.conf file
221
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getincludeinternal
222
     *
223
     * @return string
224
     */
225
    public function getIncludeInternal(): string
226
    {
227
        return '';
228
    }
229
230
    /**
231
     * Generates the modules.conf file.
232
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatemodulesconf
233
     *
234
     * @return string The generated modules.conf file content.
235
     */
236
    public function generateModulesConf(): string
237
    {
238
        return '';
239
    }
240
241
    /**
242
     * Prepares additional rules for [internal] context section in the extensions.conf file
243
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongeninternal
244
     *
245
     * @return string
246
     */
247
    public function extensionGenInternal(): string
248
    {
249
        return '';
250
    }
251
252
    /**
253
     * Prepares additional rules for [internal-users] context section in the extensions.conf file
254
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongeninternaluserspredial
255
     *
256
     * @return string
257
     */
258
    public function extensionGenInternalUsersPreDial(): string
259
    {
260
        return '';
261
    }
262
263
    /**
264
     * Prepares additional rules for [all_peers] context section in the extensions.conf file
265
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongenallpeerscontext
266
     *
267
     * @return string
268
     */
269
    public function extensionGenAllPeersContext(): string
270
    {
271
        return '';
272
    }
273
274
    /**
275
     * Prepares additional includes for [internal-transfer] context section in the extensions.conf file
276
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getincludeinternaltransfer
277
     *
278
     * @return string
279
     */
280
    public function getIncludeInternalTransfer(): string
281
    {
282
        return '';
283
    }
284
285
    /**
286
     * Prepares additional rules for [internal-transfer] context section in the extensions.conf file
287
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongeninternaltransfer
288
     *
289
     * @return string
290
     */
291
    public function extensionGenInternalTransfer(): string
292
    {
293
        return '';
294
    }
295
296
    /**
297
     * Prepares additional contexts sections in the extensions.conf file
298
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongencontexts
299
     *
300
     * @return string
301
     */
302
    public function extensionGenContexts(): string
303
    {
304
        return '';
305
    }
306
307
    /**
308
     * Prepares additional hints for [internal-hints] context section in the extensions.conf file
309
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongenhints
310
     *
311
     * @return string
312
     */
313
    public function extensionGenHints(): string
314
    {
315
        return '';
316
    }
317
318
    /**
319
     * Adds priorities ащк [dial_create_chan] context section in the extensions.conf file
320
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensiongenhints
321
     *
322
     * @return string
323
     */
324
    public function extensionGenCreateChannelDialplan(): string
325
    {
326
        return '';
327
    }
328
329
    /**
330
     * Prepares additional parameters for [globals] section in the extensions.conf file
331
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#extensionglobals
332
     *
333
     * @return string
334
     */
335
    public function extensionGlobals(): string
336
    {
337
        return '';
338
    }
339
340
    /**
341
     * Prepares additional parameters for [featuremap] section in the features.conf file
342
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getfeaturemap
343
     *
344
     * @return string returns additional Star codes
345
     */
346
    public function getFeatureMap(): string
347
    {
348
        return '';
349
    }
350
351
    /**
352
     * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file
353
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepubliccontext
354
     *
355
     * @return string
356
     */
357
    public function generatePublicContext(): string
358
    {
359
        return '';
360
    }
361
362
    /**
363
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
364
     * extensions.conf file
365
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedialpresystem
366
     *
367
     * @param string $rout_number
368
     *
369
     * @return string
370
     */
371
    public function generateIncomingRoutBeforeDialPreSystem(string $rout_number): string
372
    {
373
        return '';
374
    }
375
376
    /**
377
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
378
     * extensions.conf file
379
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedialsystem
380
     *
381
     * @param string $rout_number
382
     *
383
     * @return string
384
     */
385
    public function generateIncomingRoutBeforeDialSystem(string $rout_number): string
386
    {
387
        return '';
388
    }
389
390
    /**
391
     * Prepares additional parameters for each incoming context for each incoming route before dial in the
392
     * extensions.conf file
393
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutbeforedial
394
     *
395
     * @param string $rout_number
396
     *
397
     * @return string
398
     */
399
    public function generateIncomingRoutBeforeDial(string $rout_number): string
400
    {
401
        return '';
402
    }
403
404
    /**
405
     * Returns the messages variable
406
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other
407
     *
408
     * @return array
409
     */
410
    public function getMessages(): array
411
    {
412
        return $this->messages;
413
    }
414
415
    /**
416
     * Returns models list of models which affect the current module settings
417
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#other
418
     *
419
     * @return array
420
     */
421
    public function getDependenceModels(): array
422
    {
423
        return [];
424
    }
425
426
    /**
427
     * Prepares additional parameters for each outgoing route context
428
     * before dial call in the extensions.conf file
429
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateoutroutcontext
430
     *
431
     * @param array $rout
432
     *
433
     * @return string
434
     */
435
    public function generateOutRoutContext(array $rout): string
436
    {
437
        return '';
438
    }
439
440
    /**
441
     * Override pjsip options for provider in the pjsip.conf file
442
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#overrideproviderpjsipoptions
443
     *
444
     * @param string $uniqid  the provider unique identifier
445
     * @param array  $options list of pjsip options
446
     *
447
     * @return array
448
     */
449
    public function overrideProviderPJSIPOptions(string $uniqid, array $options): array
450
    {
451
        return $options;
452
    }
453
454
    /**
455
     * Override pjsip options for peer in the pjsip.conf file
456
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#overridepjsipoptions
457
     *
458
     * @param string $extension the endpoint extension
459
     * @param array  $options   list of pjsip options
460
     *
461
     * @return array
462
     */
463
    public function overridePJSIPOptions(string $extension, array $options): array
464
    {
465
        return $options;
466
    }
467
468
    /**
469
     * Prepares additional parameters for each outgoing route context
470
     * after dial call in the extensions.conf file
471
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateoutroutafterdialcontext
472
     *
473
     * @param array $rout
474
     *
475
     * @return string
476
     */
477
    public function generateOutRoutAfterDialContext(array $rout): string
478
    {
479
        return '';
480
    }
481
482
    /**
483
     * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer
484
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepeerpjadditionaloptions
485
     *
486
     * @param array $peer information about peer
487
     *
488
     * @return string
489
     */
490
    public function generatePeerPjAdditionalOptions(array $peer): string
491
    {
492
        return '';
493
    }
494
495
    /**
496
     * Prepares additional AMI users data in the manager.conf file
497
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatemanagerconf
498
     *
499
     * @return string
500
     */
501
    public function generateManagerConf(): string
502
    {
503
        return '';
504
    }
505
506
    /**
507
     * Prepares additional parameters for each incoming context
508
     * and incoming route after dial command in an extensions.conf file
509
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generateincomingroutafterdialcontext
510
     *
511
     *
512
     * @param string $uniqId
513
     *
514
     * @return string
515
     */
516
    public function generateIncomingRoutAfterDialContext(string $uniqId): string
517
    {
518
        return '';
519
    }
520
521
    /**
522
     * Prepares additional peers data in the pjsip.conf file
523
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#generatepeerspj
524
     *
525
     * @return string
526
     */
527
    public function generatePeersPj(): string
528
    {
529
        return '';
530
    }
531
532
    /**
533
     * Allows overriding the execution priority of a method when called through hookModulesMethod.
534
     * @see https://docs.mikopbx.com/mikopbx-development/module-developement/module-class#getmethodpriority
535
     *
536
     * @param string $methodName
537
     *
538
     * @return int
539
     */
540
    public function getMethodPriority(string $methodName = ''): int
541
    {
542
        switch ($methodName) {
543
            case AsteriskConfigInterface::GENERATE_CONFIG:
544
            default:
545
                $result = $this->priority;
546
        }
547
        return $result;
548
    }
549
550
}