Passed
Push — develop ( 06fb87...d66898 )
by Nikolay
23:43
created

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