Passed
Push — develop ( e4768a...6d9d8a )
by Nikolay
04:23
created

recreateModulesProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 5
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-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\Core\Providers;
23
24
use MikoPBX\Core\Asterisk\Configs\AsteriskConfigClass;
25
use Phalcon\Di;
26
use Phalcon\Di\DiInterface;
27
use Phalcon\Di\ServiceProviderInterface;
28
29
use function MikoPBX\Common\Config\appPath;
30
31
/**
32
 * Returns an array of Asterisk configuration objects sorted by priority.
33
 */
34
class AsteriskConfModulesProvider implements ServiceProviderInterface
35
{
36
    public const SERVICE_NAME = 'asteriskConfModules';
37
38
    /**
39
     * Registers asteriskConfModules service provider
40
     * Returns an array of Asterisk configuration objects sorted by priority.
41
     *
42
     * @param \Phalcon\Di\DiInterface $di
43
     */
44
    public function register(DiInterface $di): void
45
    {
46
        $di->setShared(
47
            self::SERVICE_NAME,
48
            function ($methodName=''){
49
                $arrObjects = [];
50
                $configsDir = appPath('src/Core/Asterisk/Configs');
51
                $modulesFiles = glob("{$configsDir}/*.php", GLOB_NOSORT);
52
                foreach ($modulesFiles as $file) {
53
                    $className        = pathinfo($file)['filename'];
54
                    if ($className === 'CoreConfigClass'){
55
                        continue;
56
                    }
57
                    $fullClassName = "\\MikoPBX\\Core\\Asterisk\\Configs\\{$className}";
58
                    if (class_exists($fullClassName)) {
59
                        $object = new $fullClassName();
60
                        if ($object instanceof AsteriskConfigClass){
61
                            $arrObjects[] = $object;
62
                        }
63
                    }
64
                }
65
                // Sort the array based on the priority value
66
                usort($arrObjects, function($a, $b) use ($methodName){
67
                    return $a->getMethodPriority($methodName) - $b->getMethodPriority($methodName);
68
                });
69
                return  $arrObjects;
70
            }
71
        );
72
    }
73
74
    /**
75
     * Recreates modules service after enable or disable them
76
     */
77
    public static function recreateModulesProvider(): void
78
    {
79
        $di = Di::getDefault();
80
        $di->remove(self::SERVICE_NAME);
81
        $di->register(new self());
82
    }
83
84
}