Passed
Push — develop ( 73b673...d6f831 )
by Nikolay
05:32
created

PBXConfModulesProvider::getCoreConfModules()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 19
rs 9.5222
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-2020 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
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
use MikoPBX\Common\Models\PbxExtensionModules;
25
use MikoPBX\Core\System\Util;
26
use MikoPBX\Modules\Config\ConfigClass;
27
use Phalcon\Di;
28
use Phalcon\Di\DiInterface;
29
use Phalcon\Di\ServiceProviderInterface;
30
31
use function MikoPBX\Common\Config\appPath;
32
33
/**
34
 * Main database connection is created based in the parameters defined in the configuration file
35
 */
36
class PBXConfModulesProvider implements ServiceProviderInterface
37
{
38
    public const SERVICE_NAME = 'pbxConfModules';
39
40
    /**
41
     * Registers pbxConfModules service provider
42
     *
43
     * @param \Phalcon\Di\DiInterface $di
44
     */
45
    public function register(DiInterface $di): void
46
    {
47
        $di->setShared(
48
            self::SERVICE_NAME,
49
            function (){
50
                   return self::getExternalConfModules();
51
            }
52
        );
53
    }
54
55
56
    /**
57
     * Creates array of external installed modules
58
     * @return array
59
     */
60
    public static function getExternalConfModules():array
61
    {
62
        $arrObjects = [];
63
        $modules = PbxExtensionModules::getEnabledModulesArray();
64
        foreach ($modules as $value) {
65
            $className      = str_replace('Module', '', $value['uniqid']);
66
            $fullClassName = "\\Modules\\{$value['uniqid']}\\Lib\\{$className}Conf";
67
            if (class_exists($fullClassName)) {
68
                $object = new $fullClassName();
69
                if ($object instanceof ConfigClass){
70
                    $arrObjects[] = $object;
71
                }
72
            }
73
        }
74
75
        return  $arrObjects;
76
    }
77
78
    /**
79
     * Recreates modules service after enable or disable them
80
     */
81
    public static function recreateModulesProvider(): void
82
    {
83
        $di = Di::getDefault();
84
        $di->remove(self::SERVICE_NAME);
85
        $di->register(new self());
86
    }
87
88
    /**
89
     * Calls additional module method by name and returns array of results
90
     *
91
     * @param string $methodName
92
     * @param array  $arguments
93
     *
94
     * @return array
95
     */
96
    public static function hookModulesMethodWithArrayResult(string $methodName, array $arguments = []): array
97
    {
98
        $result            = [];
99
        $di = Di::getDefault();
100
        $additionalModules = $di->getShared(self::SERVICE_NAME);
101
        foreach ($additionalModules as $configClassObj) {
102
            if ( ! method_exists($configClassObj, $methodName)) {
103
                continue;
104
            }
105
            try {
106
                $moduleMethodResponse = call_user_func_array([$configClassObj, $methodName], $arguments);
107
            } catch (\Throwable $e) {
108
                global $errorLogger;
109
                $errorLogger->captureException($e);
110
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
111
                continue;
112
            }
113
            if ( ! empty($moduleMethodResponse)) {
114
                if (is_a($configClassObj, ConfigClass::class)) {
115
                    $result[$configClassObj->moduleUniqueId] = $moduleMethodResponse;
116
                } else {
117
                    $result[] = $moduleMethodResponse;
118
                }
119
            }
120
        }
121
122
        return $result;
123
    }
124
125
    /**
126
     * Calls additional module method by name and returns plain text result
127
     *
128
     * @param string $methodName
129
     * @param array  $arguments
130
     *
131
     * @return string
132
     */
133
    public static function hookModulesMethod(string $methodName, array $arguments = []): string
134
    {
135
        $stringResult      = '';
136
        $di = Di::getDefault();
137
        $additionalModules = $di->getShared(PBXConfModulesProvider::SERVICE_NAME);
138
        foreach ($additionalModules as $configClassObj) {
139
            if ( ! method_exists($configClassObj, $methodName)) {
140
                continue;
141
            }
142
            try {
143
                $includeString = call_user_func_array([$configClassObj, $methodName], $arguments);
144
                if ( ! empty($includeString)) {
145
                    $includeString = $configClassObj->confBlockWithComments($includeString);
146
                    if (
147
                        substr($stringResult, -1) !== "\t"
148
                        &&
149
                        substr($includeString, 0, 4) === 'same'
150
                    ) {
151
                        $stringResult .= "\t" . $includeString;
152
                    } else {
153
                        $stringResult .= $includeString;
154
                    }
155
                }
156
            } catch (\Throwable $e) {
157
                global $errorLogger;
158
                $errorLogger->captureException($e);
159
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
160
                continue;
161
            }
162
        }
163
164
        return $stringResult;
165
    }
166
167
168
    /**
169
     * Calls additional module method by name without returns
170
     *
171
     * @param string $methodName
172
     * @param array  $arguments
173
     *
174
     * @return void
175
     */
176
    public static function hookModulesProcedure(string $methodName, array $arguments = []): void
177
    {
178
        $di = Di::getDefault();
179
        $additionalModules = $di->getShared(PBXConfModulesProvider::SERVICE_NAME);
180
        foreach ($additionalModules as $configClassObj) {
181
            if ( ! method_exists($configClassObj, $methodName)) {
182
                continue;
183
            }
184
            try {
185
                call_user_func_array([$configClassObj, $methodName], $arguments);
186
            } catch (\Throwable $e) {
187
                global $errorLogger;
188
                $errorLogger->captureException($e);
189
                Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR);
190
                continue;
191
            }
192
        }
193
    }
194
195
}