Lib_modules::getModContDirName()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 1
dl 0
loc 13
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
use CI;
4
use Modules;
5
6
if (!defined('BASEPATH')) {
7
    exit('No direct script access allowed');
8
}
9
10
class Lib_modules
11
{
12
13
    /**
14
     *
15
     * @var array
16
     */
17
    private $modules = [];
18
19
    /**
20
     *
21
     * @var array
22
     */
23
    private $modulesAppRelPath = [];
24
25
    public function __construct() {
26
27
        $this->setModulesLocations();
28
        $this->loadModules();
29
    }
30
31
    /**
32
     * Adding dirs that may contain modules
33
     */
34
    private function setModulesLocations() {
35
36
        if (FALSE !== $modulesLocations = CI::$APP->config->item('modules_locations')) {
37
            $locationsToSet = [];
38
            $countModulesLocations = count($modulesLocations);
39
            for ($i = 0; $i < $countModulesLocations; $i++) {
40
                $withinPath = trim($modulesLocations[$i], '/') . '/';
41
                $locationsToSet[APPPATH . $withinPath] = "../{$withinPath}";
42
            }
43
            Modules::$locations = $locationsToSet;
44
        }
45
    }
46
47
    /**
48
     * Loading all modules names and their paths
49
     */
50
    private function loadModules() {
51
52
        CI::$APP->load->helper('file');
53
        foreach (Modules::$locations as $path => $relPath) {
54
            $modulesInLocation = get_dir_file_info($path);
55
            foreach ($modulesInLocation as $name => $info) {
56
                $fullModulePath = $path . $name . '/';
57
                if (is_dir($fullModulePath)) {
58
                    $this->modules[$name] = $fullModulePath;
59
                    $this->modulesAppRelPath[$name] = trim(str_replace(APPPATH, '', $path), '/');
60
                }
61
            }
62
        }
63
    }
64
65
    // ------------ useful methods section -------------
66
67
    /**
68
     *
69
     * @param string $moduleName
70
     * @return boolean|string
71
     */
72
    public function getModulePath($moduleName) {
73
74
        if (!is_string($moduleName) || empty($moduleName)) {
75
            throw new \InvalidArgumentException('Module name must be string');
76
        }
77
        if (!isset($this->modules[$moduleName])) {
78
            return false;
79
        }
80
        return $this->modules[$moduleName];
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getModulesPaths() {
87
88
        return $this->modules;
89
    }
90
91
    /**
92
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
93
     */
94
    public function getModulesNames() {
95
96
        return array_keys($this->modules);
97
    }
98
99
    /**
100
     *
101
     * @param string $moduleName
102
     * @return bool
103
     */
104
    public function moduleExists($moduleName) {
105
106
        if (!is_string($moduleName) || empty($moduleName)) {
107
            return false;
108
        }
109
        return array_key_exists($moduleName, $this->modules);
110
    }
111
112
    /**
113
     * Get module containing dir name
114
     *
115
     * @param string $moduleName
116
     * @return string
117
     */
118
    public function getModContDirName($moduleName) {
119
120
        if (!SHOP_INSTALLED && $moduleName == 'shop') {
121
            return;
122
        }
123
        if (!is_string($moduleName) || empty($moduleName)) {
124
            throw new \InvalidArgumentException('Module name must be string');
125
        }
126
        if (!array_key_exists($moduleName, $this->modulesAppRelPath)) {
127
            throw new \UnexpectedValueException("Module [{$moduleName}] not found");
128
        }
129
        return $this->modulesAppRelPath[$moduleName];
130
    }
131
132
}