Passed
Push — develop ( 225864...73f8c7 )
by Nikolay
08:07 queued 03:40
created

Directories::getConfigValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 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\System;
21
22
use Phalcon\Di;
23
24
class Directories extends Di\Injectable
25
{
26
    const CORE_CF_DIR = 'core.cfDir';
27
    const CORE_VAR_ETC_DIR = 'core.varEtcDir';
28
    const CORE_MODULES_DIR = 'core.modulesDir';
29
    const CORE_TEMP_DIR = 'core.tempDir';
30
    const CORE_CONF_BACKUP_DIR = 'core.confBackupDir';
31
    const CORE_LOGS_DIR = 'core.logsDir';
32
    const CORE_MEDIA_MOUNT_POINT_DIR = 'core.mediaMountPoint';
33
    const CORE_FAIL2AN_DB_DIR = 'core.fail2banDbDir';
34
    const AST_VAR_LIB_DIR = 'asterisk.astvarlibdir';
35
    const AST_ETC_DIR = 'asterisk.astetcdir';
36
    const AST_MOD_DIR = 'asterisk.astmoddir';
37
    const AST_MOH_DIR = 'asterisk.mohdir';
38
    const AST_AGI_BIN_DIR = 'asterisk.astagidir';
39
    const AST_DB_DIR = 'asterisk.astdbdir';
40
    const AST_LOG_DIR = 'asterisk.astlogdir';
41
    const AST_SPOOL_DIR = 'asterisk.astspooldir';
42
    const AST_MEDIA_DIR = 'asterisk.mediadir';
43
    const AST_MONITOR_DIR = 'asterisk.monitordir';
44
    const AST_CUSTOM_SOUND_DIR = 'asterisk.customSoundDir';
45
    const AST_LUA_DIALPLAN_DIR = 'asterisk.luaDialplanDir';
46
    const WWW_UPLOAD_DIR = 'www.uploadDir';
47
    const WWW_DOWNLOAD_CACHE_DIR = 'www.downloadCacheDir';
48
    const APP_ASSETS_CACHE_DIR = 'adminApplication.assetsCacheDir';
49
    const APP_VOLT_CACHE_DIR = 'adminApplication.voltCacheDir';
50
    const APP_VIEW_CACHE_DIR = 'adminApplication.viewCacheDir';
51
52
    const DEFAULT_DIRS = [
53
        self::CORE_CF_DIR => '/cf',
54
        self::CORE_VAR_ETC_DIR => '/var/etc',
55
        self::CORE_MODULES_DIR => '/mountpoint/mikopbx/custom_modules',
56
        self::CORE_TEMP_DIR => '/mountpoint/mikopbx/tmp',
57
        self::CORE_CONF_BACKUP_DIR => '/mountpoint/mikopbx/backup/db',
58
        self::CORE_LOGS_DIR => '/mountpoint/mikopbx/log',
59
        self::CORE_MEDIA_MOUNT_POINT_DIR => '/mountpoint',
60
        self::CORE_FAIL2AN_DB_DIR => '/mountpoint/mikopbx/fail2ban',
61
        self::AST_VAR_LIB_DIR => '/offload/asterisk',
62
        self::AST_ETC_DIR => '/etc/asterisk',
63
        self::AST_MOD_DIR => '/offload/asterisk/modules',
64
        self::AST_AGI_BIN_DIR => '/var/lib/asterisk/agi-bin',
65
        self::AST_DB_DIR    => '/mountpoint/mikopbx/persistence',
66
        self::AST_LOG_DIR => '/mountpoint/mikopbx/astlogs/asterisk',
67
        self::AST_SPOOL_DIR => 'mountpoint/mikopbx/astspool',
68
        self::AST_MEDIA_DIR => '/mountpoint/mikopbx/media',
69
        self::AST_MONITOR_DIR => '/mountpoint/mikopbx/astspool/monitor',
70
        self::AST_MOH_DIR => '/mountpoint/mikopbx/media/moh',
71
        self::AST_CUSTOM_SOUND_DIR => '/mountpoint/mikopbx/media/custom',
72
        self::AST_LUA_DIALPLAN_DIR => '/etc/asterisk/extensions-lua',
73
        self::WWW_UPLOAD_DIR => '/mountpoint/mikopbx/tmp/www_cache/upload_cache',
74
        self::WWW_DOWNLOAD_CACHE_DIR => '/mountpoint/mikopbx/tmp/www_cache/files_cache',
75
        self::APP_ASSETS_CACHE_DIR => '/var/tmp/www_cache',
76
        self::APP_VOLT_CACHE_DIR => '/var/tmp/www_cache/volt',
77
        self::APP_VIEW_CACHE_DIR => '/var/tmp/www_cache/view',
78
    ];
79
80
    protected static array $dirCache = [];
81
82
    /**
83
     *  Returns value from config file
84
     *
85
     * @param string $configPath
86
     * @param string $default
87
     * @return string
88
     */
89
    public static function getConfigValue(string $configPath, string $default): string
90
    {
91
        $di = Di::getDefault();
92
        if ($di !== null) {
93
            return $di->getConfig()->path($configPath, $default);
94
        }
95
96
        return $default;
97
    }
98
99
    /**
100
     * Returns directory path by key
101
     *
102
     * @return string The monitor directory path.
103
     */
104
    public static function getDir(string $dirConstant): string
105
    {
106
        if (!isset(self::$dirCache[$dirConstant])) {
107
            self::$dirCache[$dirConstant] = self::getConfigValue($dirConstant, self::DEFAULT_DIRS[$dirConstant]);
108
        }
109
110
        return self::$dirCache[$dirConstant];
111
    }
112
113
114
    /**
115
     * Reset cached variables
116
     *
117
     * @return void
118
     */
119
    public static function reset(): void
120
    {
121
        self::$dirCache = [];
122
    }
123
}