Completed
Push — development ( cfd391...deed4d )
by Andrij
12:03
created

AdminLoader::adminAutoload()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 3
nop 0
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php namespace CMSFactory;
2
3
class AdminLoader
4
{
5
6
    /**
7
     * @var AdminLoader
8
     */
9
    private static $instance;
10
11
    /**
12
     * @var bool
13
     */
14
    private static $adminAutoLoaded = false;
15
16
    /**
17
     * @var \CI_DB_active_record
18
     */
19
    private $db;
20
21
    public static function getInstance() {
22
        return self::$instance ?: self::$instance = new self(\CI::$APP->db);
23
    }
24
25
    public function __construct(\CI_DB_active_record $db) {
26
        $this->db = $db;
27
    }
28
29
    public function adminAutoload() {
30
        if (!self::$adminAutoLoaded) {
31
            /** Search module with autoload */
32
            $query = $this->db->select('name')->where('autoload', 1)->get('components');
33
34
            if ($query) {
35
                /** Run all Admin autoload method */
36
                $modules = $query->result_array();
37
                $modules = array_column($modules, 'name');
38
                array_unshift($modules, 'core');
39
40
                $modules = array_unique($modules);
41
42
                foreach ($modules as $moduleName) {
43
                    \Modules::load_file($moduleName, APPPATH . 'modules' . DIRECTORY_SEPARATOR . $moduleName . DIRECTORY_SEPARATOR);
44
                    $moduleName = ucfirst($moduleName);
45
                    if (class_exists($moduleName)) {
46
                        if (method_exists($moduleName, 'adminAutoload')) {
47
                            $moduleName::adminAutoload();
48
                        }
49
                    }
50
                }
51
            }
52
53
            self::$adminAutoLoaded = true;
54
        }
55
56
    }
57
58
}