Passed
Push — develop ( d66898...d52602 )
by Nikolay
12:48
created

hookModulesMethodWithArrayResult()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 26
rs 9.0444
c 0
b 0
f 0
cc 6
nc 6
nop 2
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\PBXConfModulesProvider;
24
use MikoPBX\Core\System\MikoPBXConfig;
25
use MikoPBX\Core\System\Util;
26
use MikoPBX\Modules\Config\ConfigClass;
27
use Phalcon\Config;
28
use Phalcon\Di\Injectable;
29
use Throwable;
30
31
abstract class CoreConfigClass extends Injectable
32
{
33
    public const EXTENSION_GEN_HINTS = 'extensionGenHints';
34
35
    public const GENERATE_PUBLIC_CONTEXT = 'generatePublicContext';
36
37
    public const EXTENSION_GEN_INTERNAL_TRANSFER = 'extensionGenInternalTransfer';
38
39
    public const GET_INCLUDE_INTERNAL_TRANSFER = 'getIncludeInternalTransfer';
40
41
    public const EXTENSION_GLOBALS = 'extensionGlobals';
42
43
    public const EXTENSION_GEN_CONTEXTS = 'extensionGenContexts';
44
45
    public const GET_INCLUDE_INTERNAL = 'getIncludeInternal';
46
47
    public const EXTENSION_GEN_INTERNAL = 'extensionGenInternal';
48
49
    public const GENERATE_INCOMING_ROUT_BEFORE_DIAL = 'generateIncomingRoutBeforeDial';
50
51
    public const GENERATE_INCOMING_ROUT_AFTER_DIAL_CONTEXT = 'generateIncomingRoutAfterDialContext';
52
53
    public const GET_FEATURE_MAP = 'getFeatureMap';
54
55
    public const GENERATE_MODULES_CONF = 'generateModulesConf';
56
57
    public const GENERATE_MANAGER_CONF = 'generateManagerConf';
58
59
    public const GENERATE_PEERS_PJ = 'generatePeersPj';
60
61
    public const GENERATE_PEER_PJ_ADDITIONAL_OPTIONS = 'generatePeerPjAdditionalOptions';
62
63
    public const GENERATE_OUT_ROUT_CONTEXT = 'generateOutRoutContext';
64
65
    public const GENERATE_OUT_ROUT_AFTER_DIAL_CONTEXT = 'generateOutRoutAfterDialContext';
66
67
    public const OVERRIDE_PJSIP_OPTIONS = 'overridePJSIPOptions';
68
69
    public const OVERRIDE_PROVIDER_PJSIP_OPTIONS = 'overrideProviderPJSIPOptions';
70
71
    public const GENERATE_CONFIG = 'generateConfig';
72
73
    public const GET_DEPENDENCE_MODELS ='getDependenceModels';
74
75
76
    /**
77
     * Config file name i.e. extensions.conf
78
     */
79
    protected string $description;
80
81
    /**
82
     * Easy way to get or set the PbxSettings values
83
     *
84
     * @var \MikoPBX\Core\System\MikoPBXConfig
85
     */
86
    protected MikoPBXConfig $mikoPBXConfig;
87
88
    /**
89
     * Access to the /etc/inc/mikopbx-settings.json values
90
     *
91
     * @var \Phalcon\Config
92
     */
93
    protected Config $config;
94
95
    /**
96
     * Shows if it is boot process now or usual work
97
     *
98
     * @var bool
99
     */
100
    protected bool $booting;
101
102
    /**
103
     * Error and notice messages
104
     *
105
     * @var array
106
     */
107
    protected array $messages;
108
109
    /**
110
     * Array of PbxSettings values
111
     */
112
    protected array $generalSettings;
113
114
    /**
115
     * ConfigClass constructor.
116
     */
117
    public function __construct()
118
    {
119
        $this->config          = $this->getDI()->getShared('config');
120
        $this->booting         = $this->getDI()->getShared('registry')->booting === true;
121
        $this->mikoPBXConfig   = new MikoPBXConfig();
122
        $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings();
123
        $this->messages        = [];
124
    }
125
126
    /**
127
     * Calls additional module method by name and returns plain text result
128
     *
129
     * @param string $methodName
130
     * @param array  $arguments
131
     *
132
     * @return string
133
     */
134
    public function hookModulesMethod(string $methodName, $arguments = []): string
135
    {
136
        $stringResult      = '';
137
        $additionalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME);
138
        foreach ($additionalModules as $configClassObj) {
139
            if ( ! method_exists($configClassObj, $methodName)) {
140
                continue;
141
            }
142
            if ($configClassObj instanceof $this) {
143
                continue;
144
            }
145
            try {
146
                $includeString = call_user_func_array([$configClassObj, $methodName], $arguments);
147
            } catch (\Throwable $e) {
148
                global $errorLogger;
149
                $errorLogger->captureException($e);
150
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
151
                continue;
152
            }
153
            if ( ! empty($includeString)) {
154
                $stringResult .= $this->confBlockWithComments($includeString);
155
            }
156
        }
157
158
        return $stringResult;
159
    }
160
161
162
    /**
163
     * Calls additional module method by name and returns array of results
164
     *
165
     * @param string $methodName
166
     * @param array  $arguments
167
     *
168
     * @return array
169
     */
170
    public function hookModulesMethodWithArrayResult(string $methodName, array $arguments = []): array
171
    {
172
        $result = [];
173
        $additionalModules = $this->di->getShared(PBXConfModulesProvider::SERVICE_NAME);
174
        foreach ($additionalModules as $configClassObj) {
175
            if ( ! method_exists($configClassObj, $methodName)) {
176
                continue;
177
            }
178
            try {
179
                $moduleMethodResponse = call_user_func_array([$configClassObj, $methodName], $arguments);
180
            } catch (Throwable $e) {
181
                global $errorLogger;
182
                $errorLogger->captureException($e);
183
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
184
                continue;
185
            }
186
            if ( ! empty($moduleMethodResponse)) {
187
                if (is_a($configClassObj, ConfigClass::class)){
188
                    $result[$configClassObj->moduleUniqueId] = $moduleMethodResponse;
189
                } else {
190
                    $result[] = $moduleMethodResponse;
191
                }
192
193
            }
194
        }
195
        return $result;
196
    }
197
198
    /**
199
     * Makes pretty module text block into config file
200
     *
201
     * @param string $addition
202
     *
203
     * @return string
204
     */
205
    protected function confBlockWithComments(string $addition): string
206
    {
207
        return $addition;
208
    }
209
210
    /**
211
     * Generates core modules config files with cli messages before and after generation
212
     */
213
    public function generateConfig(): void
214
    {
215
        $this->echoGenerateConfig();
216
        $this->getSettings();
217
        $this->generateConfigProtected();
218
        $this->echoDone();
219
    }
220
221
    /**
222
     * Shows boot message which module was started
223
     */
224
    protected function echoGenerateConfig(): void
225
    {
226
        if ($this->booting === true && ! empty($this->description)) {
227
            echo "   |- generate config {$this->description}... ";
228
        }
229
    }
230
231
    /**
232
     * Prepares settings dataset for a PBX module
233
     */
234
    public function getSettings(): void
235
    {
236
    }
237
238
    /**
239
     * Generates core modules config files
240
     */
241
    protected function generateConfigProtected(): void
242
    {
243
    }
244
245
    /**
246
     * Shows boot message which module generator was finished
247
     */
248
    protected function echoDone(): void
249
    {
250
        if ($this->booting === true && ! empty($this->description)) {
251
            echo "\033[32;1mdone\033[0m \n";
252
        }
253
    }
254
255
    /**
256
     * Prepares additional includes for [internal] context section in the extensions.conf file
257
     *
258
     * @return string
259
     */
260
    public function getIncludeInternal(): string
261
    {
262
        return '';
263
    }
264
265
    /**
266
     * Prepares additional rules for [internal] context section in the extensions.conf file
267
     *
268
     * @return string
269
     */
270
    public function extensionGenInternal(): string
271
    {
272
        return '';
273
    }
274
275
    /**
276
     * Prepares additional includes for [internal-transfer] context section in the extensions.conf file
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
     *
288
     * @return string
289
     */
290
    public function extensionGenInternalTransfer(): string
291
    {
292
        return '';
293
    }
294
295
    /**
296
     * Prepares additional contexts sections in the extensions.conf file
297
     *
298
     * @return string
299
     */
300
    public function extensionGenContexts(): string
301
    {
302
        return '';
303
    }
304
305
    /**
306
     * Prepares additional hints for [internal-hints] context section in the extensions.conf file
307
     *
308
     * @return string
309
     */
310
    public function extensionGenHints(): string
311
    {
312
        return '';
313
    }
314
315
    /**
316
     * Prepares additional parameters for [globals] section in the extensions.conf file
317
     *
318
     * @return string
319
     */
320
    public function extensionGlobals(): string
321
    {
322
        return '';
323
    }
324
325
    /**
326
     * Prepares additional parameters for [featuremap] section in the features.conf file
327
     *
328
     * @return string returns additional Star codes
329
     */
330
    public function getFeatureMap(): string
331
    {
332
        return '';
333
    }
334
335
    /**
336
     * Prepares additional parameters for [public-direct-dial] section in the extensions.conf file
337
     *
338
     * @return string
339
     */
340
    public function generatePublicContext(): 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 generateIncomingRoutBeforeDial(string $rout_number): string
354
    {
355
        return '';
356
    }
357
358
    /**
359
     * Returns the messages variable
360
     *
361
     * @return array
362
     */
363
    public function getMessages(): array
364
    {
365
        return $this->messages;
366
    }
367
368
    /**
369
     * Returns models list of models which affect the current module settings
370
     *
371
     * @return array
372
     */
373
    public function getDependenceModels(): array
374
    {
375
        return [];
376
    }
377
378
    /**
379
     * Prepares additional parameters for each outgoing route context
380
     * before dial call in the extensions.conf file
381
     *
382
     * @param array $rout
383
     *
384
     * @return string
385
     */
386
    public function generateOutRoutContext(array $rout): string
387
    {
388
        return '';
389
    }
390
391
    /**
392
     * Override pjsip options for provider in the pjsip.conf file
393
     *
394
     * @param string $uniqid  the provider unique identifier
395
     * @param array  $options list of pjsip options
396
     *
397
     * @return array
398
     */
399
    public function overrideProviderPJSIPOptions(string $uniqid, array $options): array
400
    {
401
        return $options;
402
    }
403
404
    /**
405
     * Override pjsip options for peer in the pjsip.conf file
406
     *
407
     * @param string $extension the endpoint extension
408
     * @param array  $options   list of pjsip options
409
     *
410
     * @return array
411
     */
412
    public function overridePJSIPOptions(string $extension, array $options): array
413
    {
414
        return $options;
415
    }
416
417
    /**
418
     * Prepares additional parameters for each outgoing route context
419
     * after dial call in the extensions.conf file
420
     *
421
     * @param array $rout
422
     *
423
     * @return string
424
     */
425
    public function generateOutRoutAfterDialContext(array $rout): string
426
    {
427
        return '';
428
    }
429
430
    /**
431
     * Prepares additional pjsip options on endpoint section in the pjsip.conf file for peer
432
     *
433
     * @param array $peer information about peer
434
     *
435
     * @return string
436
     */
437
    public function generatePeerPjAdditionalOptions(array $peer): string
438
    {
439
        return '';
440
    }
441
442
    /**
443
     * Prepares additional AMI users data in the manager.conf file
444
     *
445
     * @return string
446
     */
447
    public function generateManagerConf(): string
448
    {
449
        return '';
450
    }
451
452
    /**
453
     * Prepares additional parameters for each incoming context
454
     * and incoming route after dial command in an extensions.conf file
455
     *
456
     * @param string $uniqId
457
     *
458
     * @return string
459
     */
460
    public function generateIncomingRoutAfterDialContext(string $uniqId): string
461
    {
462
        return '';
463
    }
464
465
    /**
466
     * Prepares additional peers data in the pjsip.conf file
467
     *
468
     * @return string
469
     */
470
    public function generatePeersPj(): string
471
    {
472
        return '';
473
    }
474
475
}